From 2ddc16a63b4f7d6fd8ae99091cdbcdab9fd97ee4 Mon Sep 17 00:00:00 2001 From: Suhas Dissanayake Date: Wed, 27 Mar 2024 14:20:03 +0530 Subject: [PATCH] Fix: Annoying tab switching behaviour --- .../java/com/bnyro/clock/ui/MainActivity.kt | 22 +++++++++++-------- .../com/bnyro/clock/ui/nav/NavContainer.kt | 14 +++--------- .../java/com/bnyro/clock/ui/nav/NavHost.kt | 3 ++- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/com/bnyro/clock/ui/MainActivity.kt b/app/src/main/java/com/bnyro/clock/ui/MainActivity.kt index 296c6d67..b7213d7b 100644 --- a/app/src/main/java/com/bnyro/clock/ui/MainActivity.kt +++ b/app/src/main/java/com/bnyro/clock/ui/MainActivity.kt @@ -37,6 +37,7 @@ import com.bnyro.clock.util.ThemeUtil class MainActivity : ComponentActivity() { val stopwatchModel by viewModels() + private var initialTab: NavRoutes = NavRoutes.Alarm lateinit var stopwatchService: StopwatchService private val serviceConnection = object : ServiceConnection { @@ -59,21 +60,24 @@ class MainActivity : ComponentActivity() { stopwatchService.onPositionChange = {} } } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + initialTab = when (intent?.action) { + SHOW_STOPWATCH_ACTION -> NavRoutes.Stopwatch + AlarmClock.ACTION_SET_ALARM, AlarmClock.ACTION_SHOW_ALARMS -> NavRoutes.Alarm + AlarmClock.ACTION_SET_TIMER, AlarmClock.ACTION_SHOW_TIMERS -> NavRoutes.Timer + else -> bottomNavItems.first { + Preferences.instance.getString( + Preferences.startTabKey, + NavRoutes.Alarm.route + ) == it.route + } + } setContent { val settingsModel: SettingsModel = viewModel() - val initialTab = when (intent?.action) { - SHOW_STOPWATCH_ACTION -> NavRoutes.Stopwatch - AlarmClock.ACTION_SET_ALARM, AlarmClock.ACTION_SHOW_ALARMS -> NavRoutes.Alarm - AlarmClock.ACTION_SET_TIMER, AlarmClock.ACTION_SHOW_TIMERS -> NavRoutes.Timer - else -> bottomNavItems.first { - Preferences.instance.getString(Preferences.startTabKey, NavRoutes.Alarm.route) == it.route - } - } - val darkTheme = when (settingsModel.themeMode) { SettingsModel.Theme.SYSTEM -> isSystemInDarkTheme() SettingsModel.Theme.DARK, SettingsModel.Theme.AMOLED -> true diff --git a/app/src/main/java/com/bnyro/clock/ui/nav/NavContainer.kt b/app/src/main/java/com/bnyro/clock/ui/nav/NavContainer.kt index fd09bae4..50fa46e6 100644 --- a/app/src/main/java/com/bnyro/clock/ui/nav/NavContainer.kt +++ b/app/src/main/java/com/bnyro/clock/ui/nav/NavContainer.kt @@ -18,7 +18,6 @@ import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember -import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalConfiguration @@ -31,7 +30,6 @@ import androidx.navigation.compose.rememberNavController import com.bnyro.clock.ui.MainActivity import com.bnyro.clock.ui.model.ClockModel import com.bnyro.clock.ui.model.SettingsModel -import kotlinx.coroutines.launch val bottomNavItems = listOf( NavRoutes.Alarm, @@ -40,13 +38,13 @@ val bottomNavItems = listOf( NavRoutes.Stopwatch ) val navRoutes = bottomNavItems + NavRoutes.Settings + @Composable fun NavContainer( settingsModel: SettingsModel, initialTab: NavRoutes ) { val context = LocalContext.current - val scope = rememberCoroutineScope() val clockModel: ClockModel = viewModel(factory = ClockModel.Factory) val navController = rememberNavController() @@ -71,14 +69,7 @@ fun NavContainer( navRoutes.firstOrNull { it.route == destination.route } ?.let { selectedRoute = it } } - - // this needs to be launched in a new scope to avoid crashes when it's called too early - // while the navController doesn't yet have the ability to navigate because the NavContainer - // is not yet composed - scope.launch { - navController.navigate(selectedRoute.route) - navController.addOnDestinationChangedListener(listener) - } + navController.addOnDestinationChangedListener(listener) onDispose { navController.removeOnDestinationChangedListener(listener) @@ -135,6 +126,7 @@ fun NavContainer( navController, settingsModel, clockModel, + startDestination = initialTab, modifier = Modifier .fillMaxSize() ) diff --git a/app/src/main/java/com/bnyro/clock/ui/nav/NavHost.kt b/app/src/main/java/com/bnyro/clock/ui/nav/NavHost.kt index d3e2a428..e5545017 100644 --- a/app/src/main/java/com/bnyro/clock/ui/nav/NavHost.kt +++ b/app/src/main/java/com/bnyro/clock/ui/nav/NavHost.kt @@ -22,13 +22,14 @@ fun AppNavHost( navController: NavHostController, settingsModel: SettingsModel, clockModel: ClockModel, + startDestination: NavRoutes, modifier: Modifier = Modifier ) { val alarmModel: AlarmModel = viewModel() val timerModel: TimerModel = viewModel() val stopwatchModel: StopwatchModel = viewModel() - NavHost(navController, startDestination = NavRoutes.Alarm.route, modifier = modifier) { + NavHost(navController, startDestination = startDestination.route, modifier = modifier) { composable(NavRoutes.Alarm.route) { AlarmScreen(onClickSettings = { navController.navigate(NavRoutes.Settings.route)