1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settingslib.spa.widget.ui
18 
19 import androidx.compose.foundation.layout.Box
20 import androidx.compose.foundation.layout.Column
21 import androidx.compose.foundation.layout.fillMaxSize
22 import androidx.compose.foundation.layout.padding
23 import androidx.compose.foundation.layout.width
24 import androidx.compose.material3.MaterialTheme
25 import androidx.compose.material3.Text
26 import androidx.compose.runtime.Composable
27 import androidx.compose.runtime.State
28 import androidx.compose.ui.Alignment
29 import androidx.compose.ui.Modifier
30 import androidx.compose.ui.text.style.TextOverflow
31 import androidx.compose.ui.tooling.preview.Preview
32 import androidx.compose.ui.unit.dp
33 import com.android.settingslib.spa.framework.theme.SettingsDimension
34 import com.android.settingslib.spa.framework.theme.SettingsTheme
35 import com.android.settingslib.spa.framework.theme.toMediumWeight
36 
37 @Composable
38 fun SettingsTitle(title: State<String>, useMediumWeight: Boolean = false) {
39     SettingsTitle(title.value, useMediumWeight)
40 }
41 
42 @Composable
43 fun SettingsTitle(title: String, useMediumWeight: Boolean = false) {
44     Text(
45         text = title,
46         color = MaterialTheme.colorScheme.onSurface,
47         style = MaterialTheme.typography.titleMedium.let {
48             when (useMediumWeight) {
49                 true -> it.toMediumWeight()
50                 else -> it
51             }
52         },
53     )
54 }
55 
56 @Composable
57 fun SettingsBody(
58     body: State<String>,
59     maxLines: Int = Int.MAX_VALUE,
60 ) {
61     SettingsBody(body = body.value, maxLines = maxLines)
62 }
63 
64 @Composable
65 fun SettingsBody(
66     body: String,
67     maxLines: Int = Int.MAX_VALUE,
68 ) {
69     if (body.isNotEmpty()) {
70         Text(
71             text = body,
72             color = MaterialTheme.colorScheme.onSurfaceVariant,
73             style = MaterialTheme.typography.bodyMedium,
74             overflow = TextOverflow.Ellipsis,
75             maxLines = maxLines,
76         )
77     }
78 }
79 
80 @Composable
81 fun PlaceholderTitle(title: String) {
82     Box(
83         modifier = Modifier.fillMaxSize().padding(SettingsDimension.itemPadding),
84         contentAlignment = Alignment.Center,
85     ) {
86         Text(
87             text = title,
88             color = MaterialTheme.colorScheme.onSurface,
89             style = MaterialTheme.typography.titleLarge,
90         )
91     }
92 }
93 
94 @Preview
95 @Composable
96 private fun BasePreferencePreview() {
97     SettingsTheme {
98         Column(Modifier.width(100.dp)) {
99             SettingsBody(
100                 body = "Long long long long long long text",
101             )
102             SettingsBody(
103                 body = "Long long long long long long text",
104                 maxLines = 1,
105             )
106         }
107     }
108 }
109