From 8acbd6305e8a552ff25eb97508c87f7a99e9817e Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 24 Oct 2026 06:04:34 +0000 Subject: [PATCH] app: keep Home dialog state across recreation Two Home dialogs didn't restore their data with their visibility: - The "abnormal state" dialog kept its visibility in saved state but its exit code in plain remember. After a rotation the code was 0, so for code 2, which needs a full reinstall, it offered the quick fix (fix environment and reboot) instead. - The app install dialog read the update from Info.update, which is reset on every connectivity change, including each time the app comes to the foreground, and after process death. It then showed empty notes and downloaded an empty link. Save the exit code, and keep the update shown by the Home screen in the ViewModel and in the dialog's saved state, passing it to the download. Assisted-by: Claude Opus 5.5 --- .../topjohnwu/magisk/ui/home/HomeScreen.kt | 25 +++++++++++-------- .../topjohnwu/magisk/ui/home/HomeViewModel.kt | 13 +++++++--- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/home/HomeScreen.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/home/HomeScreen.kt index 9ff349fdc..e4d5e0e23 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/home/HomeScreen.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/home/HomeScreen.kt @@ -79,6 +79,7 @@ import com.topjohnwu.magisk.core.Info import com.topjohnwu.magisk.core.download.DownloadEngine import com.topjohnwu.magisk.core.download.Subject import com.topjohnwu.magisk.core.ktx.reboot +import com.topjohnwu.magisk.core.model.UpdateInfo import com.topjohnwu.magisk.ui.MainActivity import com.topjohnwu.magisk.ui.component.AdaptiveColumns import com.topjohnwu.magisk.ui.component.LoadingDialog @@ -106,10 +107,12 @@ fun HomeScreen( val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior() var showUninstallDialog by rememberSaveable { mutableStateOf(false) } - var showManagerDialog by rememberSaveable { mutableStateOf(false) } + // Keep the update itself, so a restored dialog doesn't depend on Info.update, which is reset + // on connectivity changes and after process death + var managerUpdate by rememberSaveable { mutableStateOf(null) } var showEnvFixDialog by rememberSaveable { mutableStateOf(false) } var showInstallDialog by rememberSaveable { mutableStateOf(false) } - var envFixCode by remember { mutableIntStateOf(0) } + var envFixCode by rememberSaveable { mutableIntStateOf(0) } LaunchedEffect(uiState.showUninstall) { if (uiState.showUninstall) { @@ -118,8 +121,8 @@ fun HomeScreen( } } LaunchedEffect(uiState.showManagerInstall) { - if (uiState.showManagerInstall) { - showManagerDialog = true + uiState.showManagerInstall?.let { + managerUpdate = it viewModel.onManagerInstallConsumed() } } @@ -148,14 +151,15 @@ fun HomeScreen( ) } - if (showManagerDialog) { + managerUpdate?.let { update -> ManagerInstallComposableDialog( + update = update, cacheDir = context.cacheDir, - onDismiss = { showManagerDialog = false }, + onDismiss = { managerUpdate = null }, onInstall = { - showManagerDialog = false + managerUpdate = null (context as? MainActivity)?.let { - DownloadEngine.startWithActivity(it, Subject.App()) + DownloadEngine.startWithActivity(it, Subject.App(update)) } } ) @@ -818,6 +822,7 @@ private fun UninstallComposableDialog( @Composable private fun ManagerInstallComposableDialog( + update: UpdateInfo, cacheDir: File, onDismiss: () -> Unit, onInstall: () -> Unit, @@ -834,8 +839,8 @@ private fun ManagerInstallComposableDialog( scrollable = true, ) { MarkdownTextAsync { - val text = Info.update.note - File(cacheDir, "${Info.update.versionCode}.md").writeText(text) + val text = update.note + File(cacheDir, "${update.versionCode}.md").writeText(text) text } } diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/home/HomeViewModel.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/home/HomeViewModel.kt index 4300cfc98..7f1ac623a 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/home/HomeViewModel.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/home/HomeViewModel.kt @@ -13,6 +13,7 @@ import com.topjohnwu.magisk.core.Info import com.topjohnwu.magisk.core.ktx.await import com.topjohnwu.magisk.core.ktx.reboot import com.topjohnwu.magisk.core.ktx.toast +import com.topjohnwu.magisk.core.model.UpdateInfo import com.topjohnwu.magisk.core.repository.NetworkService import com.topjohnwu.magisk.core.tasks.MagiskInstaller import com.topjohnwu.magisk.utils.asFlow @@ -41,7 +42,8 @@ class HomeViewModel( val managerRemoteVersion: String = "", val managerProgress: Int = 0, val showUninstall: Boolean = false, - val showManagerInstall: Boolean = false, + // The update to show in the app install dialog, as one-shot event + val showManagerInstall: UpdateInfo? = null, val envFixCode: Int = 0, val showLoading: Boolean = false, val magiskState: State = computeMagiskState(), @@ -49,6 +51,9 @@ class HomeViewModel( val managerInstalledVersion: String = computeManagerInstalledVersion(), ) + // Info.update is reset on every connectivity change, so keep the update that the UI shows + private var latestUpdate: UpdateInfo? = null + private val _uiState = MutableStateFlow(UiState()) val uiState: StateFlow = _uiState.asStateFlow() @@ -96,6 +101,7 @@ class HomeViewModel( ) } Info.fetchUpdate(svc)?.apply { + latestUpdate = this val isDebug = Config.updateChannel == Config.Value.DEBUG_CHANNEL _uiState.update { it.copy( @@ -131,12 +137,13 @@ class HomeViewModel( when (_uiState.value.appState) { State.LOADING -> showToast(CoreR.string.loading) State.INVALID -> showToast(CoreR.string.no_connection) - else -> _uiState.update { it.copy(showManagerInstall = true) } + else -> latestUpdate?.let { info -> _uiState.update { it.copy(showManagerInstall = info) } } + ?: showToast(CoreR.string.no_connection) } } fun onManagerInstallConsumed() { - _uiState.update { it.copy(showManagerInstall = false) } + _uiState.update { it.copy(showManagerInstall = null) } } fun onEnvFixConsumed() {