Optimise DialogState with value class

This commit is contained in:
Dude so hot authored and renovate[bot] committed 2024-10-13 20:48:24 +08:00
1 parent 2c0d621b16
commit 3bf6f763aa
2 files changed
+8 -6

No files matched your search

@@ -51,6 +51,7 @@ import androidx.compose.material3.rememberDatePickerState
import androidx.compose.material3.rememberTimePickerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
@@ -95,18 +96,19 @@ fun interface DismissDialogScope<R> {
fun dismissWith(value: R)
}
class DialogState {
var content: (@Composable () -> Unit)? by mutableStateOf(null)
typealias MutableComposable = MutableState<(@Composable () -> Unit)?>
@JvmInline
value class DialogState(val field: MutableComposable = mutableStateOf(null)) : MutableComposable by field {
@Composable
fun Intercept() = content?.invoke()
fun Intercept() = value?.invoke()
fun dismiss() {
content = null
value = null
}
suspend inline fun <R> dialog(crossinline block: @Composable (CancellableContinuation<R>) -> Unit) = try {
suspendCancellableCoroutine { cont -> content = { block(cont) } }
suspendCancellableCoroutine { cont -> value = { block(cont) } }
} finally {
dismiss()
}
@@ -49,7 +49,7 @@ fun ProgressDialog() {
suspend fun <R> DialogState.bgWork(work: suspend () -> R) = try {
suspendCoroutineUninterceptedOrReturn { cont ->
work.startCoroutineUninterceptedOrReturn(cont).also { r ->
if (r == COROUTINE_SUSPENDED) content = { ProgressDialog() }
if (r == COROUTINE_SUSPENDED) value = { ProgressDialog() }
}
}
} finally {