From af1379c0eb48ab379cb4ce22b7ebe90287e095bb Mon Sep 17 00:00:00 2001 From: evgeny Date: Thu, 29 Oct 2026 10:49:46 +0100 Subject: [PATCH] Keep settings persistence alive after navigation and guard initial preference loading --- .../utun/chat/ui/screens/SettingsScreen.kt | 44 ++++++++++++++++--- .../utun/chat/data/SettingsOperationsTest.kt | 8 ++-- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/tools/chatgui-android/app/src/main/java/com/utun/chat/ui/screens/SettingsScreen.kt b/tools/chatgui-android/app/src/main/java/com/utun/chat/ui/screens/SettingsScreen.kt index 1bacca9f7..03a9fae33 100644 --- a/tools/chatgui-android/app/src/main/java/com/utun/chat/ui/screens/SettingsScreen.kt +++ b/tools/chatgui-android/app/src/main/java/com/utun/chat/ui/screens/SettingsScreen.kt @@ -69,6 +69,7 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext +import kotlinx.coroutines.flow.first import java.util.UUID import java.io.IOException @@ -80,6 +81,27 @@ private val PROTOCOLS = listOf("tcp", "udp") fun SettingsScreen(vm: ChatViewModel, onBack: () -> Unit, onExit: () -> Unit = {}) { val provider = ChatApplication.instance.configProvider val scope = rememberCoroutineScope() + val changes = provider.changes + val changeFailed by changes.failed.collectAsState() + var loaded by remember { mutableStateOf(false) } + var loadFailed by remember { mutableStateOf(false) } + var loadAttempt by remember { mutableIntStateOf(0) } + LaunchedEffect(loadAttempt) { + loadFailed = false + try { withContext(Dispatchers.IO) { provider.allValues.first() }; loaded = true } + catch (e: CancellationException) { throw e } + catch (e: Exception) { loadFailed = true; LogManager.addLog("ERROR", "Settings", "Settings load failed: $e") } + } + if (!loaded) { + Column(Modifier.fillMaxSize().padding(16.dp), verticalArrangement = Arrangement.spacedBy(12.dp)) { + TextButton(onClick = onBack) { Text(stringResource(R.string.back)) } + if (loadFailed) { + Text(stringResource(R.string.settings_update_failed), color = MaterialTheme.colorScheme.error) + Button(onClick = { loadAttempt++ }) { Text(stringResource(R.string.retry)) } + } else CircularProgressIndicator() + } + return + } var dirty by rememberSaveable { mutableStateOf(false) } var applyingConfig by remember { mutableStateOf(false) } var applyFailed by remember { mutableStateOf(false) } @@ -93,6 +115,8 @@ fun SettingsScreen(vm: ChatViewModel, onBack: () -> Unit, onExit: () -> Unit = { applyingConfig = true scope.launch { try { + changes.awaitIdle() + check(!changes.failed.value) { "A settings change failed before restart" } HeadlessService.awaitStartup() if (NativeLib.isReady()) { val config = provider.buildConfigText() @@ -113,6 +137,10 @@ fun SettingsScreen(vm: ChatViewModel, onBack: () -> Unit, onExit: () -> Unit = { } BackHandler(onBack = handleBack) + if (changeFailed) AlertDialog(onDismissRequest = changes::dismissFailure, + text = { Text(stringResource(R.string.settings_update_failed)) }, confirmButton = { + TextButton(onClick = changes::dismissFailure) { Text(stringResource(R.string.close)) } + }) if (applyingConfig) AlertDialog(onDismissRequest = {}, title = { Text(stringResource(R.string.settings)) }, text = { CircularProgressIndicator() }, confirmButton = {}) if (applyFailed) AlertDialog(onDismissRequest = { applyFailed = false }, title = { Text(stringResource(R.string.settings)) }, @@ -179,15 +207,17 @@ private fun StreamSettingsTab(vm: ChatViewModel) { val total = n ?: return@Button val parity = r ?: return@Button saving = true; failed = false; saved = false - vm.viewModelScope.launch { + provider.changes.submit("broadcast parameters") { try { provider.saveStreamSettings(total, parity) NativeLib.setChatSetting("stream_subblocks", total.toString()) NativeLib.setChatSetting("stream_rs_redundant", parity.toString()) saved = true - } catch (e: IOException) { + } catch (e: CancellationException) { throw e } + catch (e: Exception) { failed = true LogManager.addLog("ERROR", "STREAM", "Broadcast settings save failed: $e") + throw e } finally { saving = false } @@ -221,7 +251,7 @@ private fun GeneralSettingsTab(onDirty: (Boolean) -> Unit) { item(key = "language") { LanguageSetting() } item(key = "battery") { BatteryOptimizationSetting(source = "settings") } item(key = "node-name") { - OutlinedTextField(nodeName, { v -> onDirty(true); nodeName = v; scope.launch { provider.setValue("node.name", v) } }, + OutlinedTextField(nodeName, { v -> onDirty(true); nodeName = v; provider.changes.submit("node name") { provider.setValue("node.name", v) } }, label = { Text(stringResource(R.string.node_name)) }, singleLine = true, modifier = Modifier.heightIn(min = 40.dp)) } item(key = "profile-avatar") { ProfileAvatarControls() } @@ -233,11 +263,11 @@ private fun GeneralSettingsTab(onDirty: (Boolean) -> Unit) { Row(verticalAlignment = Alignment.CenterVertically) { Text(stringResource(R.string.local_invites_enabled), Modifier.weight(1f)) Switch(checked = localInvitesEnabled, onCheckedChange = { enabled -> - localInvitesEnabled = enabled - scope.launch { + provider.changes.submit("local invitations") { val value = if (enabled) "1" else "0" provider.setValue("chat.local_invites_enabled", value) if (NativeLib.isReady()) NativeLib.setChatSetting("local_invites_enabled", value) + localInvitesEnabled = enabled } }) } @@ -246,11 +276,11 @@ private fun GeneralSettingsTab(onDirty: (Boolean) -> Unit) { Row(verticalAlignment = Alignment.CenterVertically) { Text(stringResource(R.string.local_broadcasts_enabled), Modifier.weight(1f)) Switch(checked = localBroadcastsEnabled, onCheckedChange = { enabled -> - localBroadcastsEnabled = enabled - scope.launch { + provider.changes.submit("local broadcasts") { val value = if (enabled) "1" else "0" provider.setValue("chat.local_broadcasts_enabled", value) if (NativeLib.isReady()) NativeLib.setChatSetting("local_broadcasts_enabled", value) + localBroadcastsEnabled = enabled } }) } diff --git a/tools/chatgui-android/router-tests/src/test/kotlin/com/utun/chat/data/SettingsOperationsTest.kt b/tools/chatgui-android/router-tests/src/test/kotlin/com/utun/chat/data/SettingsOperationsTest.kt index 7f6ee1d9b..fcc78822b 100644 --- a/tools/chatgui-android/router-tests/src/test/kotlin/com/utun/chat/data/SettingsOperationsTest.kt +++ b/tools/chatgui-android/router-tests/src/test/kotlin/com/utun/chat/data/SettingsOperationsTest.kt @@ -2,7 +2,7 @@ package com.utun.chat.data import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Job +import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.async import kotlinx.coroutines.launch import kotlinx.coroutines.test.runTest @@ -22,9 +22,11 @@ class SettingsOperationsTest { assertEquals(listOf("commit", "apply", "restart"), order) } @Test fun failedPersistenceCannotRunTheFollowingRuntimeUpdateOrKillTheQueue() = runTest { - val logs = mutableListOf(); val owner = SettingsOperations(this) { _, message -> logs.add(message) } + val logs = mutableListOf() + val owner = SettingsOperations(CoroutineScope(coroutineContext + SupervisorJob())) { _, message -> logs.add(message) } var runtime = false - val failed = owner.submit("volume") { throw java.io.IOException("disk rejected"); @Suppress("UNREACHABLE_CODE") runtime = true } + val persist: suspend () -> Unit = { throw java.io.IOException("disk rejected") } + val failed = owner.submit("volume") { persist(); runtime = true } assertTrue(runCatching { failed.await() }.exceptionOrNull() is java.io.IOException) assertFalse(runtime); assertTrue(owner.failed.value); assertTrue(logs.any { "disk rejected" in it }) owner.submit("retry") { runtime = true }.await()