1 /*
2  * Copyright (C) 2024 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.systemui.communal.ui.compose
18 
19 import androidx.compose.foundation.background
20 import androidx.compose.foundation.layout.Arrangement
21 import androidx.compose.foundation.layout.Box
22 import androidx.compose.foundation.layout.Column
23 import androidx.compose.foundation.layout.PaddingValues
24 import androidx.compose.foundation.layout.Row
25 import androidx.compose.foundation.layout.fillMaxWidth
26 import androidx.compose.foundation.layout.padding
27 import androidx.compose.foundation.layout.wrapContentHeight
28 import androidx.compose.foundation.shape.RoundedCornerShape
29 import androidx.compose.material3.MaterialTheme
30 import androidx.compose.material3.Text
31 import androidx.compose.material3.TextButton
32 import androidx.compose.runtime.Composable
33 import androidx.compose.runtime.DisposableEffect
34 import androidx.compose.runtime.getValue
35 import androidx.compose.runtime.mutableStateOf
36 import androidx.compose.runtime.remember
37 import androidx.compose.runtime.setValue
38 import androidx.compose.ui.Alignment
39 import androidx.compose.ui.Modifier
40 import androidx.compose.ui.platform.LocalView
41 import androidx.compose.ui.res.stringResource
42 import androidx.compose.ui.text.style.TextAlign
43 import androidx.compose.ui.unit.dp
44 import com.android.compose.theme.LocalAndroidColorScheme
45 import com.android.systemui.res.R
46 import com.android.systemui.statusbar.phone.ComponentSystemUIDialog
47 import com.android.systemui.statusbar.phone.SystemUIDialogFactory
48 import com.android.systemui.statusbar.phone.create
49 
50 /** Dialog shown upon tapping a disabled widget which allows users to enable the widget. */
51 @Composable
EnableWidgetDialognull52 fun EnableWidgetDialog(
53     isEnableWidgetDialogVisible: Boolean,
54     dialogFactory: SystemUIDialogFactory,
55     title: String,
56     positiveButtonText: String,
57     onConfirm: () -> Unit,
58     onCancel: () -> Unit
59 ) {
60     var dialog: ComponentSystemUIDialog? by remember { mutableStateOf(null) }
61     val context = LocalView.current.context
62 
63     DisposableEffect(isEnableWidgetDialogVisible) {
64         if (isEnableWidgetDialogVisible) {
65             dialog =
66                 dialogFactory.create(
67                     context = context,
68                 ) {
69                     DialogComposable(title, positiveButtonText, onConfirm, onCancel)
70                 }
71             dialog?.apply {
72                 setCancelable(true)
73                 setCanceledOnTouchOutside(true)
74                 setOnCancelListener { onCancel() }
75                 show()
76             }
77         }
78 
79         onDispose {
80             dialog?.dismiss()
81             dialog = null
82         }
83     }
84 }
85 
86 @Composable
DialogComposablenull87 private fun DialogComposable(
88     title: String,
89     positiveButtonText: String,
90     onConfirm: () -> Unit,
91     onCancel: () -> Unit,
92 ) {
93     Box(
94         Modifier.fillMaxWidth()
95             .padding(top = 18.dp, bottom = 8.dp)
96             .background(LocalAndroidColorScheme.current.surfaceBright, RoundedCornerShape(28.dp))
97     ) {
98         Column(
99             modifier = Modifier.fillMaxWidth(),
100             verticalArrangement = Arrangement.spacedBy(20.dp),
101         ) {
102             Box(
103                 modifier = Modifier.padding(horizontal = 24.dp).fillMaxWidth().wrapContentHeight(),
104                 contentAlignment = Alignment.TopStart
105             ) {
106                 Text(
107                     text = title,
108                     style = MaterialTheme.typography.titleMedium,
109                     color = LocalAndroidColorScheme.current.onSurface,
110                     textAlign = TextAlign.Center,
111                     maxLines = 1,
112                 )
113             }
114 
115             Box(
116                 modifier = Modifier.padding(end = 12.dp).fillMaxWidth().wrapContentHeight(),
117                 contentAlignment = Alignment.Center
118             ) {
119                 Row(
120                     modifier = Modifier.fillMaxWidth(),
121                     horizontalArrangement = Arrangement.End,
122                 ) {
123                     TextButton(
124                         contentPadding = PaddingValues(16.dp),
125                         onClick = onCancel,
126                     ) {
127                         Text(
128                             text = stringResource(R.string.cancel),
129                         )
130                     }
131                     TextButton(
132                         contentPadding = PaddingValues(16.dp),
133                         onClick = onConfirm,
134                     ) {
135                         Text(
136                             text = positiveButtonText,
137                         )
138                     }
139                 }
140             }
141         }
142     }
143 }
144