Skip to content

Commit 214b173

Browse files
committed
library: Call onDismissRequest when Dialog closed via BackHandler
1 parent f83f4e4 commit 214b173

File tree

3 files changed

+58
-12
lines changed

3 files changed

+58
-12
lines changed

composeApp/src/commonMain/kotlin/component/TextComponent.kt

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import androidx.compose.foundation.layout.size
1616
import androidx.compose.foundation.layout.width
1717
import androidx.compose.material.icons.Icons
1818
import androidx.compose.material.icons.filled.Person
19+
import androidx.compose.material.icons.filled.Star
1920
import androidx.compose.runtime.Composable
2021
import androidx.compose.runtime.MutableState
2122
import androidx.compose.runtime.getValue
@@ -50,6 +51,7 @@ import top.yukonga.miuix.kmp.utils.squircleshape.SquircleShape
5051
@Composable
5152
fun TextComponent() {
5253
val showDialog = remember { mutableStateOf(false) }
54+
val showDialog2 = remember { mutableStateOf(false) }
5355
var checkbox by remember { mutableStateOf(false) }
5456
var checkboxTrue by remember { mutableStateOf(true) }
5557
var switch by remember { mutableStateOf(false) }
@@ -96,14 +98,16 @@ fun TextComponent() {
9698
) {
9799
Image(
98100
colorFilter = ColorFilter.tint(MiuixTheme.colorScheme.onBackground),
99-
imageVector = Icons.Default.Person,
101+
imageVector = Icons.Default.Star,
100102
contentDescription = "Person",
101103
)
102104
}
103105
},
104-
title = "Person",
105-
summary = "An introduction",
106-
onClick = {}
106+
title = "Title",
107+
summary = "Click to show Dialog 1",
108+
onClick = {
109+
showDialog.value = true
110+
}
107111
)
108112

109113
Row(
@@ -234,9 +238,9 @@ fun TextComponent() {
234238

235239
SuperArrow(
236240
title = "Dialog",
237-
summary = "Click to show a Dialog",
241+
summary = "Click to show Dialog 2",
238242
onClick = {
239-
showDialog.value = true
243+
showDialog2.value = true
240244
}
241245
)
242246

@@ -260,16 +264,58 @@ fun TextComponent() {
260264
}
261265

262266
dialog(showDialog)
267+
dialog2(showDialog2)
263268
}
264269

270+
265271
@Composable
266272
fun dialog(showDialog: MutableState<Boolean>) {
273+
if (!showDialog.value) return
274+
showDialog(
275+
content = {
276+
SuperDialog(
277+
title = "Dialog 1",
278+
summary = "Summary",
279+
show = showDialog,
280+
onDismissRequest = {
281+
showDialog.value = false
282+
},
283+
) {
284+
Row(
285+
horizontalArrangement = Arrangement.SpaceBetween
286+
) {
287+
Button(
288+
modifier = Modifier.weight(1f),
289+
text = "Cancel",
290+
onClick = {
291+
dismissDialog()
292+
showDialog.value = false
293+
}
294+
)
295+
Spacer(Modifier.width(20.dp))
296+
Button(
297+
modifier = Modifier.weight(1f),
298+
text = "Confirm",
299+
submit = true,
300+
onClick = {
301+
dismissDialog()
302+
showDialog.value = false
303+
}
304+
)
305+
}
306+
}
307+
}
308+
)
309+
}
310+
311+
@Composable
312+
fun dialog2(showDialog: MutableState<Boolean>) {
313+
if (!showDialog.value) return
267314
val value = remember { mutableStateOf("") }
268315
showDialog(
269-
show = showDialog.value,
270316
content = {
271317
SuperDialog(
272-
title = "Title",
318+
title = "Dialog 2",
273319
summary = "Summary",
274320
show = showDialog,
275321
onDismissRequest = {
@@ -308,4 +354,4 @@ fun dialog(showDialog: MutableState<Boolean>) {
308354
}
309355
}
310356
)
311-
}
357+
}

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/extra/SuperDialog.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import top.yukonga.miuix.kmp.utils.squircleshape.SquircleShape
3939
* @param title The title of the [SuperDialog].
4040
* @param titleColor The color of the title.
4141
* @param summary The summary of the [SuperDialog].
42+
* @param show The state of the [SuperDialog].
4243
* @param onDismissRequest The callback when the [SuperDialog] is dismissed.
4344
* @param insideMargin The margin inside the [SuperDialog].
4445
* @param content The [Composable] content of the [SuperDialog].
@@ -65,7 +66,7 @@ fun SuperDialog(
6566

6667
BackHandler(enabled = show.value) {
6768
dismissDialog()
68-
show.value = false
69+
onDismissRequest()
6970
}
7071

7172
Box(

miuix/src/commonMain/kotlin/top/yukonga/miuix/kmp/utils/MiuixPopupUtil.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ class MiuixPopupUtil {
3838
*/
3939
@Composable
4040
fun showDialog(
41-
show: Boolean,
4241
content: (@Composable () -> Unit)? = null,
4342
) {
44-
isDialogShowing.value = show
43+
isDialogShowing.value = true
4544
dialogContext.value = content
4645
}
4746

0 commit comments

Comments
 (0)