diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt index f0bbd8fdb..849515df5 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt @@ -77,6 +77,8 @@ class MainActivity : ComponentActivity(), SplashScreenHost { override val splashController = SplashController(this) private val intentState = MutableStateFlow(0) + // A tab requested by a new intent while the activity is running (it's singleTask) + private val tabRequest = MutableStateFlow(null) internal val showUnsupported = MutableStateFlow>>(emptyList()) override fun attachBaseContext(base: Context) { @@ -116,7 +118,7 @@ class MainActivity : ComponentActivity(), SplashScreenHost { } } - val initialTab = getInitialTab(intent) + val initialTab = getRequestedTab(intent) ?: Tab.HOME.ordinal setContent { MagiskTheme { @@ -150,8 +152,11 @@ class MainActivity : ComponentActivity(), SplashScreenHost { val superuserVm: SuperuserViewModel = viewModel( viewModelStoreOwner = this@MainActivity, factory = VMFactory ) + val requestedTab by tabRequest.collectAsStateWithLifecycle() MainScreen( initialTab = initialTab, + requestedTab = requestedTab, + onTabRequestHandled = { tabRequest.value = null }, superuserViewModel = superuserVm, ) } @@ -336,10 +341,11 @@ class MainActivity : ComponentActivity(), SplashScreenHost { override fun onNewIntent(intent: Intent) { super.onNewIntent(intent) setIntent(intent) + getRequestedTab(intent)?.let { tabRequest.value = it } intentState.value += 1 } - private fun getInitialTab(intent: Intent?): Int { + private fun getRequestedTab(intent: Intent?): Int? { val section = when { intent?.action == Intent.ACTION_APPLICATION_PREFERENCES -> Const.Nav.SETTINGS intent?.action == FlashUtils.INTENT_FLASH && @@ -347,10 +353,11 @@ class MainActivity : ComponentActivity(), SplashScreenHost { else -> intent?.getStringExtra(Const.Key.OPEN_SECTION) } return when (section) { + Const.Nav.HOME -> Tab.HOME.ordinal Const.Nav.SUPERUSER -> Tab.SUPERUSER.ordinal Const.Nav.MODULES -> Tab.MODULES.ordinal Const.Nav.SETTINGS -> Tab.SETTINGS.ordinal - else -> Tab.HOME.ordinal + else -> null } } diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainScreen.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainScreen.kt index 197b0e2a2..30f80aa44 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainScreen.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/MainScreen.kt @@ -45,6 +45,7 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.rememberUpdatedState import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.focus.FocusDirection @@ -91,6 +92,8 @@ enum class Tab(val titleRes: Int, val iconRes: Int) { fun MainScreen( modifier: Modifier = Modifier, initialTab: Int = Tab.HOME.ordinal, + requestedTab: Int? = null, + onTabRequestHandled: () -> Unit = {}, superuserViewModel: SuperuserViewModel? = null, ) { val navigator = LocalNavigator.current @@ -106,6 +109,15 @@ fun MainScreen( } val initialPage = visibleTabs.indexOf(Tab.entries[initialTab]).coerceAtLeast(0) val pagerState = rememberPagerState(initialPage = initialPage, pageCount = { visibleTabs.size }) + + // Show a tab requested while the app is already running, e.g. by the system settings + val currentOnTabRequestHandled by rememberUpdatedState(onTabRequestHandled) + LaunchedEffect(requestedTab) { + if (requestedTab == null) return@LaunchedEffect + val page = visibleTabs.indexOf(Tab.entries[requestedTab]) + if (page >= 0) pagerState.scrollToPage(page) + currentOnTabRequestHandled() + } val fabFocusRequester = remember { FocusRequester() } val navModulesFocusRequester = remember { FocusRequester() } val navSuperuserFocusRequester = remember { FocusRequester() }