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.gallery.page
18 
19 import android.os.Bundle
20 import androidx.compose.runtime.Composable
21 import androidx.compose.ui.tooling.preview.Preview
22 import androidx.navigation.NavType
23 import androidx.navigation.navArgument
24 import com.android.settingslib.spa.framework.common.SettingsPageProvider
25 import com.android.settingslib.spa.framework.compose.navigator
26 import com.android.settingslib.spa.framework.theme.SettingsTheme
27 import com.android.settingslib.spa.widget.preference.Preference
28 import com.android.settingslib.spa.widget.preference.PreferenceModel
29 import com.android.settingslib.spa.widget.scaffold.RegularScaffold
30 
31 private const val TITLE = "Sample page with arguments"
32 private const val STRING_PARAM_NAME = "stringParam"
33 private const val INT_PARAM_NAME = "intParam"
34 
35 object ArgumentPageProvider : SettingsPageProvider {
36     override val name = "Argument"
37 
38     override val parameter = listOf(
<lambda>null39         navArgument(STRING_PARAM_NAME) { type = NavType.StringType },
<lambda>null40         navArgument(INT_PARAM_NAME) { type = NavType.IntType },
41     )
42 
43     @Composable
Pagenull44     override fun Page(arguments: Bundle?) {
45         ArgumentPage(
46             stringParam = arguments!!.getString(STRING_PARAM_NAME, "default"),
47             intParam = arguments.getInt(INT_PARAM_NAME),
48         )
49     }
50 
51     @Composable
EntryItemnull52     fun EntryItem(stringParam: String, intParam: Int) {
53         Preference(object : PreferenceModel {
54             override val title = TITLE
55             override val summary = { "$STRING_PARAM_NAME=$stringParam, $INT_PARAM_NAME=$intParam" }
56             override val onClick = navigator("$name/$stringParam/$intParam")
57         })
58     }
59 }
60 
61 @Composable
ArgumentPagenull62 fun ArgumentPage(stringParam: String, intParam: Int) {
63     RegularScaffold(title = TITLE) {
64         Preference(object : PreferenceModel {
65             override val title = "String param value"
66             override val summary = { stringParam }
67         })
68 
69         Preference(object : PreferenceModel {
70             override val title = "Int param value"
71             override val summary = { intParam.toString() }
72         })
73 
74         ArgumentPageProvider.EntryItem(stringParam = "foo", intParam = intParam + 1)
75 
76         ArgumentPageProvider.EntryItem(stringParam = "bar", intParam = intParam + 1)
77     }
78 }
79 
80 @Preview(showBackground = true)
81 @Composable
ArgumentPagePreviewnull82 private fun ArgumentPagePreview() {
83     SettingsTheme {
84         ArgumentPage(stringParam = "foo", intParam = 0)
85     }
86 }
87