Tweak DialogScope
This commit is contained in:
1 parent
ac7fed649a
commit
fe158e7f62
2 files changed
+22
-24
No files matched your search
@@ -30,7 +30,7 @@ import kotlinx.coroutines.suspendCancellableCoroutine
|
||||
import kotlin.coroutines.resume
|
||||
|
||||
interface DialogScope<R> {
|
||||
infix fun shouldReturn(value: R)
|
||||
var expectedValue: R
|
||||
}
|
||||
|
||||
class DialogState {
|
||||
@@ -47,7 +47,8 @@ class DialogState {
|
||||
return suspendCancellableCoroutine { cont ->
|
||||
cont.invokeOnCancellation { dismiss() }
|
||||
content = {
|
||||
var result by remember { mutableStateOf(initial) }
|
||||
val state = remember(cont) { mutableStateOf(initial) }
|
||||
val impl = remember(cont) { object : DialogScope<R> { override var expectedValue by state } }
|
||||
AlertDialog(
|
||||
onDismissRequest = {
|
||||
cont.cancel()
|
||||
@@ -56,19 +57,13 @@ class DialogState {
|
||||
confirmButton = {
|
||||
TextButton(onClick = {
|
||||
dismiss()
|
||||
cont.resume(result)
|
||||
cont.resume(state.value)
|
||||
}) {
|
||||
Text(text = stringResource(id = android.R.string.ok))
|
||||
}
|
||||
},
|
||||
title = title?.let { { Text(text = stringResource(id = title)) } },
|
||||
text = {
|
||||
block(object : DialogScope<R> {
|
||||
override infix fun shouldReturn(value: R) {
|
||||
result = value
|
||||
}
|
||||
})
|
||||
},
|
||||
text = { block(impl) },
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -133,13 +128,21 @@ class DialogState {
|
||||
tonalElevation = AlertDialogDefaults.TonalElevation,
|
||||
) {
|
||||
Column {
|
||||
Text(text = title, modifier = Modifier.padding(horizontal = 16.dp).padding(top = 16.dp), style = MaterialTheme.typography.titleMedium)
|
||||
Text(
|
||||
text = title,
|
||||
modifier = Modifier
|
||||
.padding(horizontal = 16.dp)
|
||||
.padding(top = 16.dp),
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
)
|
||||
items.forEachIndexed { index, (icon, text) ->
|
||||
Row(
|
||||
modifier = Modifier.clickable {
|
||||
dismiss()
|
||||
cont.resume(index)
|
||||
}.fillMaxWidth(),
|
||||
modifier = Modifier
|
||||
.clickable {
|
||||
dismiss()
|
||||
cont.resume(index)
|
||||
}
|
||||
.fillMaxWidth(),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
Icon(imageVector = icon, contentDescription = null, modifier = Modifier.padding(16.dp), tint = AlertDialogDefaults.iconContentColor)
|
||||
|
||||
@@ -99,24 +99,19 @@ class GalleryPreviewScreen : Fragment() {
|
||||
}
|
||||
|
||||
suspend fun showGoToDialog() {
|
||||
val goto = dialogState.show(initial = 1, title = R.string.go_to) {
|
||||
var jumpTo by remember { mutableStateOf(1f) }
|
||||
initialKey = dialogState.show(initial = 1F, title = R.string.go_to) {
|
||||
Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.padding(top = 18.dp)) {
|
||||
Text(text = "1", modifier = Modifier.padding(12.dp))
|
||||
Slider(
|
||||
value = jumpTo,
|
||||
onValueChange = { jumpTo = it },
|
||||
value = expectedValue,
|
||||
onValueChange = { expectedValue = it },
|
||||
modifier = Modifier.height(48.dp).width(0.dp).weight(1F).align(Alignment.CenterVertically),
|
||||
valueRange = 1f..galleryDetail.previewPages.toFloat(),
|
||||
steps = galleryDetail.previewPages - 2,
|
||||
onValueChangeFinished = {
|
||||
this@show shouldReturn jumpTo.roundToInt()
|
||||
},
|
||||
)
|
||||
Text(text = pages.toString(), modifier = Modifier.padding(12.dp))
|
||||
}
|
||||
}
|
||||
initialKey = goto
|
||||
}.roundToInt()
|
||||
}
|
||||
|
||||
val previewPagesMap = rememberMemorized { mutableMapOf<Int, PreviewPage>().apply { put(1, galleryDetail.previewList) } }
|
||||
|
||||
Reference in new issue
Block a user