1 /*
2  * Copyright (C) 2023 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.gallery.preference
18 
19 import android.os.Bundle
20 import androidx.compose.material.icons.Icons
21 import androidx.compose.material.icons.outlined.DisabledByDefault
22 import androidx.compose.runtime.Composable
23 import androidx.compose.runtime.IntState
24 import androidx.compose.runtime.LaunchedEffect
25 import androidx.compose.runtime.getValue
26 import androidx.compose.runtime.mutableIntStateOf
27 import androidx.compose.runtime.produceState
28 import androidx.compose.runtime.remember
29 import androidx.compose.runtime.saveable.rememberSaveable
30 import androidx.compose.runtime.setValue
31 import androidx.compose.ui.res.stringResource
32 import androidx.compose.ui.tooling.preview.Preview
33 import com.android.settingslib.spa.framework.common.SettingsPageProvider
34 import com.android.settingslib.spa.framework.common.SpaEnvironmentFactory
35 import com.android.settingslib.spa.framework.compose.navigator
36 import com.android.settingslib.spa.framework.theme.SettingsTheme
37 import com.android.settingslib.spa.gallery.R
38 import com.android.settingslib.spa.widget.preference.ListPreferenceModel
39 import com.android.settingslib.spa.widget.preference.ListPreferenceOption
40 import com.android.settingslib.spa.widget.preference.Preference
41 import com.android.settingslib.spa.widget.preference.PreferenceModel
42 import com.android.settingslib.spa.widget.preference.RadioPreferences
43 import com.android.settingslib.spa.widget.scaffold.RegularScaffold
44 import com.android.settingslib.spa.widget.ui.Category
45 import com.android.settingslib.spa.widget.ui.SettingsIcon
46 import kotlinx.coroutines.delay
47 
48 object PreferencePageProvider : SettingsPageProvider {
49 
50     override val name = "Preference"
51     private const val PAGE_TITLE = "Sample Preference"
52 
53     @Composable
Pagenull54     override fun Page(arguments: Bundle?) {
55         RegularScaffold(PAGE_TITLE) {
56             Category {
57                 Preference(object : PreferenceModel {
58                     override val title = "Preference"
59                 })
60                 Preference(object : PreferenceModel {
61                     override val title = "Preference"
62                     override val summary = { "Simple summary" }
63                 })
64                 val summary = stringResource(R.string.single_line_summary_preference_summary)
65                 Preference(
66                     model = object : PreferenceModel {
67                         override val title =
68                             stringResource(R.string.single_line_summary_preference_title)
69                         override val summary = { summary }
70                     },
71                     singleLineSummary = true,
72                 )
73             }
74             Category {
75                 Preference(object : PreferenceModel {
76                     override val title = "Disabled"
77                     override val summary = { "Disabled summary" }
78                     override val enabled = { false }
79                     override val icon = @Composable {
80                         SettingsIcon(imageVector = Icons.Outlined.DisabledByDefault)
81                     }
82                 })
83             }
84             Category {
85                 Preference(object : PreferenceModel {
86                     override val title = "Preference"
87                     val asyncSummary by produceState(initialValue = " ") {
88                         delay(1000L)
89                         value = "Async summary"
90                     }
91                     override val summary = { asyncSummary }
92                 })
93 
94                 var count by remember { mutableIntStateOf(0) }
95                 Preference(object : PreferenceModel {
96                     override val title = "Click me"
97                     override val summary = { count.toString() }
98                     override val onClick: (() -> Unit) = { count++ }
99                 })
100 
101                 var ticks by remember { mutableIntStateOf(0) }
102                 LaunchedEffect(ticks) {
103                     delay(1000L)
104                     ticks++
105                 }
106                 Preference(object : PreferenceModel {
107                     override val title = "Ticker"
108                     override val summary = { ticks.toString() }
109                 })
110             }
111             val selectedId = rememberSaveable { mutableIntStateOf(0) }
112             RadioPreferences(
113                 object : ListPreferenceModel {
114                     override val title: String = "RadioPreferences"
115                     override val options: List<ListPreferenceOption> =
116                         listOf(
117                             ListPreferenceOption(id = 0, text = "option1"),
118                             ListPreferenceOption(id = 1, text = "option2"),
119                             ListPreferenceOption(id = 2, text = "option3"),
120                         )
121                     override val selectedId: IntState = selectedId
122                     override val onIdSelected: (Int) -> Unit = {
123                         selectedId.intValue = it
124                     }
125                 }
126             )
127         }
128     }
129 
130     @Composable
Entrynull131     fun Entry() {
132         Preference(model = object : PreferenceModel {
133             override val title = PAGE_TITLE
134             override val onClick = navigator(name)
135         })
136     }
137 }
138 
139 @Preview(showBackground = true)
140 @Composable
PreferencePagePreviewnull141 private fun PreferencePagePreview() {
142     SpaEnvironmentFactory.resetForPreview()
143     SettingsTheme {
144         PreferencePageProvider.Page(null)
145     }
146 }
147