android, desktop: fix alerts/modals when they are shown before UI init (#3697)

This commit is contained in:
Stanislav Dmitrenko 2024-01-17 19:50:48 +07:00 committed by GitHub
parent 809dd1ef01
commit ab8a87acad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 8 deletions

View File

@ -22,24 +22,27 @@ import chat.simplex.common.ui.theme.*
import chat.simplex.res.MR import chat.simplex.res.MR
import dev.icerock.moko.resources.StringResource import dev.icerock.moko.resources.StringResource
import dev.icerock.moko.resources.compose.painterResource import dev.icerock.moko.resources.compose.painterResource
import kotlinx.coroutines.flow.MutableStateFlow
class AlertManager { class AlertManager {
private var alertViews = mutableStateListOf<(@Composable () -> Unit)>() // Don't use mutableStateOf() here, because it produces this if showing from SimpleXAPI.startChat():
// java.lang.IllegalStateException: Reading a state that was created after the snapshot was taken or in a snapshot that has not yet been applied
private var alertViews = MutableStateFlow(listOf<(@Composable () -> Unit)>())
fun showAlert(alert: @Composable () -> Unit) { fun showAlert(alert: @Composable () -> Unit) {
Log.d(TAG, "AlertManager.showAlert") Log.d(TAG, "AlertManager.showAlert")
alertViews.add(alert) alertViews.value += alert
} }
fun hideAlert() { fun hideAlert() {
alertViews.removeLastOrNull() alertViews.value = ArrayList(alertViews.value).also { it.removeLastOrNull() }
} }
fun hideAllAlerts() { fun hideAllAlerts() {
alertViews.clear() alertViews.value = listOf()
} }
fun hasAlertsShown() = alertViews.isNotEmpty() fun hasAlertsShown() = alertViews.value.isNotEmpty()
fun showAlertDialogButtons( fun showAlertDialogButtons(
title: String, title: String,
@ -242,7 +245,7 @@ class AlertManager {
@Composable @Composable
fun showInView() { fun showInView() {
remember { alertViews }.lastOrNull()?.invoke() alertViews.collectAsState().value.lastOrNull()?.invoke()
} }
companion object { companion object {

View File

@ -11,6 +11,7 @@ import androidx.compose.ui.graphics.Color
import chat.simplex.common.model.ChatModel import chat.simplex.common.model.ChatModel
import chat.simplex.common.platform.* import chat.simplex.common.platform.*
import chat.simplex.common.ui.theme.* import chat.simplex.common.ui.theme.*
import kotlinx.coroutines.flow.MutableStateFlow
import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicBoolean
import kotlin.math.min import kotlin.math.min
@ -49,7 +50,9 @@ class ModalManager(private val placement: ModalPlacement? = null) {
private val modalCount = mutableStateOf(0) private val modalCount = mutableStateOf(0)
private val toRemove = mutableSetOf<Int>() private val toRemove = mutableSetOf<Int>()
private var oldViewChanging = AtomicBoolean(false) private var oldViewChanging = AtomicBoolean(false)
private var passcodeView: MutableState<(@Composable (close: () -> Unit) -> Unit)?> = mutableStateOf(null) // Don't use mutableStateOf() here, because it produces this if showing from SimpleXAPI.startChat():
// java.lang.IllegalStateException: Reading a state that was created after the snapshot was taken or in a snapshot that has not yet been applied
private var passcodeView: MutableStateFlow<(@Composable (close: () -> Unit) -> Unit)?> = MutableStateFlow(null)
fun showModal(settings: Boolean = false, showClose: Boolean = true, endButtons: @Composable RowScope.() -> Unit = {}, content: @Composable ModalData.() -> Unit) { fun showModal(settings: Boolean = false, showClose: Boolean = true, endButtons: @Composable RowScope.() -> Unit = {}, content: @Composable ModalData.() -> Unit) {
val data = ModalData() val data = ModalData()
@ -140,7 +143,7 @@ class ModalManager(private val placement: ModalPlacement? = null) {
@Composable @Composable
fun showPasscodeInView() { fun showPasscodeInView() {
remember { passcodeView }.value?.invoke { passcodeView.value = null } passcodeView.collectAsState().value?.invoke { passcodeView.value = null }
} }
/** /**