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.dialog
18 
19 import android.os.Bundle
20 import androidx.compose.material3.Text
21 import androidx.compose.runtime.Composable
22 import androidx.compose.runtime.getValue
23 import androidx.compose.runtime.mutableStateOf
24 import androidx.compose.runtime.saveable.rememberSaveable
25 import androidx.compose.runtime.setValue
26 import com.android.settingslib.spa.framework.common.SettingsPageProvider
27 import com.android.settingslib.spa.framework.compose.navigator
28 import com.android.settingslib.spa.widget.dialog.AlertDialogButton
29 import com.android.settingslib.spa.widget.dialog.SettingsAlertDialogWithIcon
30 import com.android.settingslib.spa.widget.dialog.rememberAlertDialogPresenter
31 import com.android.settingslib.spa.widget.preference.Preference
32 import com.android.settingslib.spa.widget.preference.PreferenceModel
33 import com.android.settingslib.spa.widget.scaffold.RegularScaffold
34 import com.android.settingslib.spa.widget.ui.Category
35 
36 private const val TITLE = "Category: Dialog"
37 
38 object DialogMainPageProvider : SettingsPageProvider {
39     override val name = "DialogMain"
40 
41     @Composable
Pagenull42     override fun Page(arguments: Bundle?) {
43         RegularScaffold(TITLE) {
44             Category {
45                 AlertDialog()
46                 AlertDialogWithIcon()
47                 NavDialog()
48             }
49         }
50     }
51 
52     @Composable
Entrynull53     fun Entry() {
54         Preference(
55             object : PreferenceModel {
56                 override val title = TITLE
57                 override val onClick = navigator(name)
58             }
59         )
60     }
61 
getTitlenull62     override fun getTitle(arguments: Bundle?) = TITLE
63 }
64 
65 @Composable
66 private fun AlertDialog() {
67     val alertDialogPresenter =
68         rememberAlertDialogPresenter(
69             confirmButton = AlertDialogButton("Ok"),
70             dismissButton = AlertDialogButton("Cancel"),
71             title = "Title",
72             text = { Text("Text") },
73         )
74     Preference(
75         object : PreferenceModel {
76             override val title = "Show AlertDialog"
77             override val onClick = alertDialogPresenter::open
78         }
79     )
80 }
81 
82 @Composable
AlertDialogWithIconnull83 private fun AlertDialogWithIcon() {
84     var openDialog by rememberSaveable { mutableStateOf(false) }
85     val close = { openDialog = false }
86     val open = { openDialog = true }
87     if (openDialog) {
88         SettingsAlertDialogWithIcon(
89             title = "Title",
90             onDismissRequest = close,
91             confirmButton = AlertDialogButton("OK", onClick = close),
92             dismissButton = AlertDialogButton("Dismiss", onClick = close),
93         ) {}
94     }
95     Preference(
96         object : PreferenceModel {
97             override val title = "Show AlertDialogWithIcon"
98             override val onClick = open
99         }
100     )
101 }
102 
103 @Composable
NavDialognull104 private fun NavDialog() {
105     Preference(
106         object : PreferenceModel {
107             override val title = "Navigate to Dialog"
108             override val onClick = navigator(route = NavDialogProvider.name)
109         }
110     )
111 }
112