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
This commit is contained in:
topjohnwu
2026-10-24 13:01:45 -07:00
committed by John Wu
parent 5f184cb45e
commit 8acbd6305e
2 changed files with 25 additions and 13 deletions
@@ -79,6 +79,7 @@ import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.download.DownloadEngine import com.topjohnwu.magisk.core.download.DownloadEngine
import com.topjohnwu.magisk.core.download.Subject import com.topjohnwu.magisk.core.download.Subject
import com.topjohnwu.magisk.core.ktx.reboot 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.MainActivity
import com.topjohnwu.magisk.ui.component.AdaptiveColumns import com.topjohnwu.magisk.ui.component.AdaptiveColumns
import com.topjohnwu.magisk.ui.component.LoadingDialog import com.topjohnwu.magisk.ui.component.LoadingDialog
@@ -106,10 +107,12 @@ fun HomeScreen(
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior() val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior()
var showUninstallDialog by rememberSaveable { mutableStateOf(false) } 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<UpdateInfo?>(null) }
var showEnvFixDialog by rememberSaveable { mutableStateOf(false) } var showEnvFixDialog by rememberSaveable { mutableStateOf(false) }
var showInstallDialog 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) { LaunchedEffect(uiState.showUninstall) {
if (uiState.showUninstall) { if (uiState.showUninstall) {
@@ -118,8 +121,8 @@ fun HomeScreen(
} }
} }
LaunchedEffect(uiState.showManagerInstall) { LaunchedEffect(uiState.showManagerInstall) {
if (uiState.showManagerInstall) { uiState.showManagerInstall?.let {
showManagerDialog = true managerUpdate = it
viewModel.onManagerInstallConsumed() viewModel.onManagerInstallConsumed()
} }
} }
@@ -148,14 +151,15 @@ fun HomeScreen(
) )
} }
if (showManagerDialog) { managerUpdate?.let { update ->
ManagerInstallComposableDialog( ManagerInstallComposableDialog(
update = update,
cacheDir = context.cacheDir, cacheDir = context.cacheDir,
onDismiss = { showManagerDialog = false }, onDismiss = { managerUpdate = null },
onInstall = { onInstall = {
showManagerDialog = false managerUpdate = null
(context as? MainActivity)?.let { (context as? MainActivity)?.let {
DownloadEngine.startWithActivity(it, Subject.App()) DownloadEngine.startWithActivity(it, Subject.App(update))
} }
} }
) )
@@ -818,6 +822,7 @@ private fun UninstallComposableDialog(
@Composable @Composable
private fun ManagerInstallComposableDialog( private fun ManagerInstallComposableDialog(
update: UpdateInfo,
cacheDir: File, cacheDir: File,
onDismiss: () -> Unit, onDismiss: () -> Unit,
onInstall: () -> Unit, onInstall: () -> Unit,
@@ -834,8 +839,8 @@ private fun ManagerInstallComposableDialog(
scrollable = true, scrollable = true,
) { ) {
MarkdownTextAsync { MarkdownTextAsync {
val text = Info.update.note val text = update.note
File(cacheDir, "${Info.update.versionCode}.md").writeText(text) File(cacheDir, "${update.versionCode}.md").writeText(text)
text text
} }
} }
@@ -13,6 +13,7 @@ import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.ktx.await import com.topjohnwu.magisk.core.ktx.await
import com.topjohnwu.magisk.core.ktx.reboot import com.topjohnwu.magisk.core.ktx.reboot
import com.topjohnwu.magisk.core.ktx.toast 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.repository.NetworkService
import com.topjohnwu.magisk.core.tasks.MagiskInstaller import com.topjohnwu.magisk.core.tasks.MagiskInstaller
import com.topjohnwu.magisk.utils.asFlow import com.topjohnwu.magisk.utils.asFlow
@@ -41,7 +42,8 @@ class HomeViewModel(
val managerRemoteVersion: String = "", val managerRemoteVersion: String = "",
val managerProgress: Int = 0, val managerProgress: Int = 0,
val showUninstall: Boolean = false, 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 envFixCode: Int = 0,
val showLoading: Boolean = false, val showLoading: Boolean = false,
val magiskState: State = computeMagiskState(), val magiskState: State = computeMagiskState(),
@@ -49,6 +51,9 @@ class HomeViewModel(
val managerInstalledVersion: String = computeManagerInstalledVersion(), 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()) private val _uiState = MutableStateFlow(UiState())
val uiState: StateFlow<UiState> = _uiState.asStateFlow() val uiState: StateFlow<UiState> = _uiState.asStateFlow()
@@ -96,6 +101,7 @@ class HomeViewModel(
) )
} }
Info.fetchUpdate(svc)?.apply { Info.fetchUpdate(svc)?.apply {
latestUpdate = this
val isDebug = Config.updateChannel == Config.Value.DEBUG_CHANNEL val isDebug = Config.updateChannel == Config.Value.DEBUG_CHANNEL
_uiState.update { _uiState.update {
it.copy( it.copy(
@@ -131,12 +137,13 @@ class HomeViewModel(
when (_uiState.value.appState) { when (_uiState.value.appState) {
State.LOADING -> showToast(CoreR.string.loading) State.LOADING -> showToast(CoreR.string.loading)
State.INVALID -> showToast(CoreR.string.no_connection) 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() { fun onManagerInstallConsumed() {
_uiState.update { it.copy(showManagerInstall = false) } _uiState.update { it.copy(showManagerInstall = null) }
} }
fun onEnvFixConsumed() { fun onEnvFixConsumed() {