• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.systemui.res.R
45 import com.android.systemui.statusbar.phone.ComponentSystemUIDialog
46 import com.android.systemui.statusbar.phone.SystemUIDialogFactory
47 import com.android.systemui.statusbar.phone.create
48 
49 /** Dialog shown upon tapping a disabled widget which allows users to enable the widget. */
50 @Composable
EnableWidgetDialognull51 fun EnableWidgetDialog(
52     isEnableWidgetDialogVisible: Boolean,
53     dialogFactory: SystemUIDialogFactory,
54     title: String,
55     positiveButtonText: String,
56     onConfirm: () -> Unit,
57     onCancel: () -> Unit
58 ) {
59     var dialog: ComponentSystemUIDialog? by remember { mutableStateOf(null) }
60     val context = LocalView.current.context
61 
62     DisposableEffect(isEnableWidgetDialogVisible) {
63         if (isEnableWidgetDialogVisible) {
64             dialog =
65                 dialogFactory.create(
66                     context = context,
67                 ) {
68                     DialogComposable(title, positiveButtonText, onConfirm, onCancel)
69                 }
70             dialog?.apply {
71                 setCancelable(true)
72                 setCanceledOnTouchOutside(true)
73                 setOnCancelListener { onCancel() }
74                 show()
75             }
76         }
77 
78         onDispose {
79             dialog?.dismiss()
80             dialog = null
81         }
82     }
83 }
84 
85 @Composable
DialogComposablenull86 private fun DialogComposable(
87     title: String,
88     positiveButtonText: String,
89     onConfirm: () -> Unit,
90     onCancel: () -> Unit,
91 ) {
92     Box(
93         Modifier.fillMaxWidth()
94             .padding(top = 18.dp, bottom = 8.dp)
95             .background(MaterialTheme.colorScheme.surfaceBright, RoundedCornerShape(28.dp))
96     ) {
97         Column(
98             modifier = Modifier.fillMaxWidth(),
99             verticalArrangement = Arrangement.spacedBy(20.dp),
100         ) {
101             Box(
102                 modifier = Modifier.padding(horizontal = 24.dp).fillMaxWidth().wrapContentHeight(),
103                 contentAlignment = Alignment.TopStart
104             ) {
105                 Text(
106                     text = title,
107                     style = MaterialTheme.typography.titleMedium,
108                     color = MaterialTheme.colorScheme.onSurface,
109                     textAlign = TextAlign.Center,
110                     maxLines = 1,
111                 )
112             }
113 
114             Box(
115                 modifier = Modifier.padding(end = 12.dp).fillMaxWidth().wrapContentHeight(),
116                 contentAlignment = Alignment.Center
117             ) {
118                 Row(
119                     modifier = Modifier.fillMaxWidth(),
120                     horizontalArrangement = Arrangement.End,
121                 ) {
122                     TextButton(
123                         contentPadding = PaddingValues(16.dp),
124                         onClick = onCancel,
125                     ) {
126                         Text(
127                             text = stringResource(R.string.cancel),
128                         )
129                     }
130                     TextButton(
131                         contentPadding = PaddingValues(16.dp),
132                         onClick = onConfirm,
133                     ) {
134                         Text(
135                             text = positiveButtonText,
136                         )
137                     }
138                 }
139             }
140         }
141     }
142 }
143