mirror of
https://github.com/kvorschk/Magisk.git
synced 2026-12-04 19:10:47 -05:00
app: modernize navigation and predictive back
Refactor Navigator to wrap NavBackStack<NavKey> with Parcelable listSaver and multi-key support, and remove dead @Serializable annotations and imports from Routes.kt. Implement hierarchical tab back navigation in MainScreen to animate scroll to Tab.HOME before bubbling to root exit, only while Route.Main is the current destination. Scope child pager pages in MainScreen and LogScreen with rememberNavigationEventDispatcherOwner(enabled = isCurrentPage) to prevent inactive offscreen tabs from intercepting navigation events. Add NavigationBackHandler and exit confirmation dialogs to FlashScreen and ActionScreen during active execution, and guard TopAppBar back navigation buttons while operations are in progress. Add NavigationBackHandler and TopAppBar back interception to DenyListScreen and SuperuserGrantScreen to collapse active search queries before exiting the screen. Assisted-by: Gemini 4 Argon Assisted-by: Claude Opus 5.5
This commit is contained in:
@@ -30,7 +30,13 @@ import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteScaffo
|
||||
import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteScaffoldDefaults
|
||||
import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteType
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.CompositionLocalProvider
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.navigationevent.NavigationEventInfo
|
||||
import androidx.navigationevent.compose.LocalNavigationEventDispatcherOwner
|
||||
import androidx.navigationevent.compose.NavigationBackHandler
|
||||
import androidx.navigationevent.compose.rememberNavigationEventDispatcherOwner
|
||||
import androidx.navigationevent.compose.rememberNavigationEventState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
@@ -61,6 +67,7 @@ import com.topjohnwu.magisk.ui.module.ModuleScreen
|
||||
import com.topjohnwu.magisk.ui.module.ModuleViewModel
|
||||
import com.topjohnwu.magisk.ui.navigation.CollectNavEvents
|
||||
import com.topjohnwu.magisk.ui.navigation.LocalNavigator
|
||||
import com.topjohnwu.magisk.ui.navigation.Route
|
||||
import com.topjohnwu.magisk.ui.settings.SettingsScreen
|
||||
import com.topjohnwu.magisk.ui.settings.SettingsViewModel
|
||||
import com.topjohnwu.magisk.ui.superuser.SuperuserScreen
|
||||
@@ -106,6 +113,19 @@ fun MainScreen(
|
||||
val isModulesTab = visibleTabs.getOrNull(pagerState.currentPage) == Tab.MODULES
|
||||
val isSuperuserTab = visibleTabs.getOrNull(pagerState.currentPage) == Tab.SUPERUSER
|
||||
|
||||
val homePageIndex = remember(visibleTabs) { visibleTabs.indexOf(Tab.HOME).coerceAtLeast(0) }
|
||||
val isNotAtHome = pagerState.currentPage != homePageIndex && navigator.current() == Route.Main
|
||||
val tabBackState = rememberNavigationEventState(currentInfo = NavigationEventInfo.None)
|
||||
NavigationBackHandler(
|
||||
state = tabBackState,
|
||||
isBackEnabled = isNotAtHome,
|
||||
onBackCompleted = {
|
||||
scope.launch {
|
||||
pagerState.animateScrollToPage(homePageIndex)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
// The collapsed rail needs 520dp (44dp top space, 96dp FAB header, 5 items, 44dp bottom) to
|
||||
// show every item. In shorter windows use the bar, like the library does below 480dp.
|
||||
val systemBars = WindowInsets.systemBars
|
||||
@@ -211,8 +231,10 @@ fun MainScreen(
|
||||
userScrollEnabled = isBar,
|
||||
) { page ->
|
||||
val isCurrentPage = pagerState.currentPage == page
|
||||
val tab = visibleTabs[page]
|
||||
Box(
|
||||
val childOwner = rememberNavigationEventDispatcherOwner(enabled = isCurrentPage)
|
||||
CompositionLocalProvider(LocalNavigationEventDispatcherOwner provides childOwner) {
|
||||
val tab = visibleTabs[page]
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.focusProperties {
|
||||
@@ -303,6 +325,7 @@ fun MainScreen(
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,6 +58,9 @@ import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.navigationevent.NavigationEventInfo
|
||||
import androidx.navigationevent.compose.NavigationBackHandler
|
||||
import androidx.navigationevent.compose.rememberNavigationEventState
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
@@ -91,6 +94,15 @@ fun DenyListScreen(
|
||||
var showSortMenu by remember { mutableStateOf(false) }
|
||||
var showFilterMenu by remember { mutableStateOf(false) }
|
||||
|
||||
val searchBackState = rememberNavigationEventState(currentInfo = NavigationEventInfo.None)
|
||||
NavigationBackHandler(
|
||||
state = searchBackState,
|
||||
isBackEnabled = query.isNotEmpty(),
|
||||
onBackCompleted = {
|
||||
viewModel.setQuery("")
|
||||
}
|
||||
)
|
||||
|
||||
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior()
|
||||
Scaffold(
|
||||
modifier = modifier,
|
||||
@@ -99,7 +111,13 @@ fun DenyListScreen(
|
||||
title = { Text(stringResource(CoreR.string.denylist)) },
|
||||
navigationIcon = {
|
||||
IconButton(
|
||||
onClick = onBack
|
||||
onClick = {
|
||||
if (query.isNotEmpty()) {
|
||||
viewModel.setQuery("")
|
||||
} else {
|
||||
onBack()
|
||||
}
|
||||
}
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.AutoMirrored.Filled.ArrowBack,
|
||||
|
||||
@@ -17,7 +17,11 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.navigationevent.NavigationEventInfo
|
||||
import androidx.navigationevent.compose.NavigationBackHandler
|
||||
import androidx.navigationevent.compose.rememberNavigationEventState
|
||||
import com.topjohnwu.magisk.R
|
||||
import com.topjohnwu.magisk.ui.component.rememberConfirmDialog
|
||||
import com.topjohnwu.magisk.ui.component.rememberExternalStoragePermissionLauncher
|
||||
import com.topjohnwu.magisk.ui.terminal.TerminalScreen
|
||||
import com.topjohnwu.magisk.core.R as CoreR
|
||||
@@ -36,6 +40,26 @@ fun FlashScreen(
|
||||
viewModel.saveLog()
|
||||
}
|
||||
|
||||
val exitConfirmDialog = rememberConfirmDialog(onConfirm = onBack)
|
||||
val exitTitle = stringResource(CoreR.string.running_exit_title)
|
||||
val exitMessage = stringResource(CoreR.string.running_exit_msg)
|
||||
|
||||
val backState = rememberNavigationEventState(currentInfo = NavigationEventInfo.None)
|
||||
NavigationBackHandler(
|
||||
state = backState,
|
||||
isBackEnabled = !finished,
|
||||
onBackCompleted = {
|
||||
if (!finished) {
|
||||
exitConfirmDialog.showConfirm(
|
||||
title = exitTitle,
|
||||
content = exitMessage,
|
||||
)
|
||||
} else {
|
||||
onBack()
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
val statusText = when (flashState) {
|
||||
FlashViewModel.State.FLASHING -> stringResource(CoreR.string.flashing)
|
||||
FlashViewModel.State.SUCCESS -> stringResource(CoreR.string.done)
|
||||
@@ -50,7 +74,16 @@ fun FlashScreen(
|
||||
title = { Text("${stringResource(CoreR.string.flash_screen_title)} - $statusText") },
|
||||
navigationIcon = {
|
||||
IconButton(
|
||||
onClick = onBack
|
||||
onClick = {
|
||||
if (!finished) {
|
||||
exitConfirmDialog.showConfirm(
|
||||
title = exitTitle,
|
||||
content = exitMessage,
|
||||
)
|
||||
} else {
|
||||
onBack()
|
||||
}
|
||||
}
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.AutoMirrored.Filled.ArrowBack,
|
||||
|
||||
@@ -47,7 +47,10 @@ import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.material3.TopAppBarDefaults
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.CompositionLocalProvider
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.navigationevent.compose.LocalNavigationEventDispatcherOwner
|
||||
import androidx.navigationevent.compose.rememberNavigationEventDispatcherOwner
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
@@ -191,36 +194,39 @@ fun LogScreen(
|
||||
beyondViewportPageCount = 0,
|
||||
) { page ->
|
||||
val isCurrentLogPage = pagerState.currentPage == page
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.focusProperties {
|
||||
onEnter = {
|
||||
if (!isCurrentLogPage) {
|
||||
cancelFocusChange()
|
||||
val childOwner = rememberNavigationEventDispatcherOwner(enabled = isCurrentLogPage)
|
||||
CompositionLocalProvider(LocalNavigationEventDispatcherOwner provides childOwner) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.focusProperties {
|
||||
onEnter = {
|
||||
if (!isCurrentLogPage) {
|
||||
cancelFocusChange()
|
||||
}
|
||||
}
|
||||
}
|
||||
onExit = {
|
||||
if (requestedFocusDirection == FocusDirection.Left ||
|
||||
requestedFocusDirection == FocusDirection.Right
|
||||
) {
|
||||
cancelFocusChange()
|
||||
onExit = {
|
||||
if (requestedFocusDirection == FocusDirection.Left ||
|
||||
requestedFocusDirection == FocusDirection.Right
|
||||
) {
|
||||
cancelFocusChange()
|
||||
}
|
||||
}
|
||||
}
|
||||
.focusGroup()
|
||||
) {
|
||||
when (page) {
|
||||
0 -> SuLogTab(
|
||||
logs = uiState.suLogs,
|
||||
nestedScrollConnection = scrollBehavior.nestedScrollConnection,
|
||||
contentPadding = padding,
|
||||
)
|
||||
1 -> MagiskLogTab(
|
||||
entries = uiState.magiskLogEntries,
|
||||
nestedScrollConnection = scrollBehavior.nestedScrollConnection,
|
||||
contentPadding = padding,
|
||||
)
|
||||
}
|
||||
.focusGroup()
|
||||
) {
|
||||
when (page) {
|
||||
0 -> SuLogTab(
|
||||
logs = uiState.suLogs,
|
||||
nestedScrollConnection = scrollBehavior.nestedScrollConnection,
|
||||
contentPadding = padding,
|
||||
)
|
||||
1 -> MagiskLogTab(
|
||||
entries = uiState.magiskLogEntries,
|
||||
nestedScrollConnection = scrollBehavior.nestedScrollConnection,
|
||||
contentPadding = padding,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,11 @@ import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.navigationevent.NavigationEventInfo
|
||||
import androidx.navigationevent.compose.NavigationBackHandler
|
||||
import androidx.navigationevent.compose.rememberNavigationEventState
|
||||
import com.topjohnwu.magisk.R
|
||||
import com.topjohnwu.magisk.ui.component.rememberConfirmDialog
|
||||
import com.topjohnwu.magisk.ui.component.rememberExternalStoragePermissionLauncher
|
||||
import com.topjohnwu.magisk.ui.terminal.TerminalScreen
|
||||
import com.topjohnwu.magisk.core.R as CoreR
|
||||
@@ -37,6 +41,26 @@ fun ActionScreen(
|
||||
viewModel.saveLog()
|
||||
}
|
||||
|
||||
val exitConfirmDialog = rememberConfirmDialog(onConfirm = onBack)
|
||||
val exitTitle = stringResource(CoreR.string.running_exit_title)
|
||||
val exitMessage = stringResource(CoreR.string.running_exit_msg)
|
||||
|
||||
val backState = rememberNavigationEventState(currentInfo = NavigationEventInfo.None)
|
||||
NavigationBackHandler(
|
||||
state = backState,
|
||||
isBackEnabled = !finished,
|
||||
onBackCompleted = {
|
||||
if (!finished) {
|
||||
exitConfirmDialog.showConfirm(
|
||||
title = exitTitle,
|
||||
content = exitMessage,
|
||||
)
|
||||
} else {
|
||||
onBack()
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior()
|
||||
Scaffold(
|
||||
modifier = modifier,
|
||||
@@ -45,7 +69,16 @@ fun ActionScreen(
|
||||
title = { Text(actionName) },
|
||||
navigationIcon = {
|
||||
IconButton(
|
||||
onClick = onBack
|
||||
onClick = {
|
||||
if (!finished) {
|
||||
exitConfirmDialog.showConfirm(
|
||||
title = exitTitle,
|
||||
content = exitMessage,
|
||||
)
|
||||
} else {
|
||||
onBack()
|
||||
}
|
||||
}
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.AutoMirrored.Filled.ArrowBack,
|
||||
|
||||
@@ -1,23 +1,22 @@
|
||||
package com.topjohnwu.magisk.ui.navigation
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.mutableStateListOf
|
||||
import androidx.compose.runtime.saveable.Saver
|
||||
import androidx.compose.runtime.saveable.listSaver
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.snapshots.SnapshotStateList
|
||||
import androidx.compose.runtime.staticCompositionLocalOf
|
||||
import androidx.navigation3.runtime.NavBackStack
|
||||
import androidx.navigation3.runtime.NavKey
|
||||
|
||||
class Navigator(initialKey: NavKey) {
|
||||
val backStack: SnapshotStateList<NavKey> = mutableStateListOf(initialKey)
|
||||
class Navigator(val backStack: NavBackStack<NavKey>) {
|
||||
constructor(vararg initialKeys: NavKey) : this(NavBackStack(*initialKeys))
|
||||
|
||||
fun push(key: NavKey) {
|
||||
backStack.add(key)
|
||||
}
|
||||
|
||||
fun pop() {
|
||||
backStack.removeLastOrNull()
|
||||
fun pop(): Boolean {
|
||||
return backStack.removeLastOrNull() != null
|
||||
}
|
||||
|
||||
fun current(): NavKey? = backStack.lastOrNull()
|
||||
@@ -26,20 +25,22 @@ class Navigator(initialKey: NavKey) {
|
||||
val Saver: Saver<Navigator, Any> = listSaver(
|
||||
save = { navigator -> navigator.backStack.toList() },
|
||||
restore = { savedList ->
|
||||
val initialKey = savedList.firstOrNull() ?: Route.Main
|
||||
Navigator(initialKey).also {
|
||||
it.backStack.clear()
|
||||
it.backStack.addAll(savedList)
|
||||
val stack = NavBackStack<NavKey>()
|
||||
if (savedList.isNotEmpty()) {
|
||||
stack.addAll(savedList)
|
||||
} else {
|
||||
stack.add(Route.Main)
|
||||
}
|
||||
Navigator(stack)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun rememberNavigator(startRoute: NavKey): Navigator {
|
||||
return rememberSaveable(startRoute, saver = Navigator.Saver) {
|
||||
Navigator(startRoute)
|
||||
fun rememberNavigator(vararg startRoutes: NavKey = arrayOf(Route.Main)): Navigator {
|
||||
return rememberSaveable(saver = Navigator.Saver) {
|
||||
Navigator(*startRoutes)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,19 +3,15 @@ package com.topjohnwu.magisk.ui.navigation
|
||||
import android.os.Parcelable
|
||||
import androidx.navigation3.runtime.NavKey
|
||||
import kotlinx.parcelize.Parcelize
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
sealed interface Route : NavKey, Parcelable {
|
||||
@Parcelize
|
||||
@Serializable
|
||||
data object Main : Route
|
||||
|
||||
@Parcelize
|
||||
@Serializable
|
||||
data object DenyList : Route
|
||||
|
||||
@Parcelize
|
||||
@Serializable
|
||||
data class Flash(
|
||||
val action: String,
|
||||
val additionalData: String? = null,
|
||||
@@ -23,15 +19,12 @@ sealed interface Route : NavKey, Parcelable {
|
||||
) : Route
|
||||
|
||||
@Parcelize
|
||||
@Serializable
|
||||
data class SuperuserDetail(val uid: Int) : Route
|
||||
|
||||
@Parcelize
|
||||
@Serializable
|
||||
data object SuperuserGrant : Route
|
||||
|
||||
@Parcelize
|
||||
@Serializable
|
||||
data class Action(
|
||||
val id: String,
|
||||
val name: String,
|
||||
|
||||
@@ -42,6 +42,9 @@ import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.navigationevent.NavigationEventInfo
|
||||
import androidx.navigationevent.compose.NavigationBackHandler
|
||||
import androidx.navigationevent.compose.rememberNavigationEventState
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
@@ -72,6 +75,15 @@ fun SuperuserGrantScreen(
|
||||
var showSortMenu by remember { mutableStateOf(false) }
|
||||
var showFilterMenu by remember { mutableStateOf(false) }
|
||||
|
||||
val searchBackState = rememberNavigationEventState(currentInfo = NavigationEventInfo.None)
|
||||
NavigationBackHandler(
|
||||
state = searchBackState,
|
||||
isBackEnabled = query.isNotEmpty(),
|
||||
onBackCompleted = {
|
||||
viewModel.setQuery("")
|
||||
}
|
||||
)
|
||||
|
||||
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior()
|
||||
Scaffold(
|
||||
modifier = modifier,
|
||||
@@ -79,7 +91,15 @@ fun SuperuserGrantScreen(
|
||||
TopAppBar(
|
||||
title = { Text(stringResource(CoreR.string.superuser_grant)) },
|
||||
navigationIcon = {
|
||||
IconButton(onClick = onBack) {
|
||||
IconButton(
|
||||
onClick = {
|
||||
if (query.isNotEmpty()) {
|
||||
viewModel.setQuery("")
|
||||
} else {
|
||||
onBack()
|
||||
}
|
||||
}
|
||||
) {
|
||||
Icon(
|
||||
imageVector = Icons.AutoMirrored.Filled.ArrowBack,
|
||||
contentDescription = stringResource(CoreR.string.back),
|
||||
|
||||
@@ -237,4 +237,6 @@
|
||||
<string name="install_unknown_denied">Allow \"Install unknown apps\" to enable this functionality</string>
|
||||
<string name="app_not_found">No app found to handle this action</string>
|
||||
<string name="reboot_apply_change">Reboot to apply changes</string>
|
||||
<string name="running_exit_title">Execution in progress</string>
|
||||
<string name="running_exit_msg">Are you sure you want to exit? The running operation may not complete.</string>
|
||||
</resources>
|
||||
|
||||
Reference in New Issue
Block a user