Skip to content

Commit

Permalink
Fix: Annoying tab switching behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed Mar 27, 2024
1 parent 341c4a2 commit 2ddc16a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
22 changes: 13 additions & 9 deletions app/src/main/java/com/bnyro/clock/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import com.bnyro.clock.util.ThemeUtil
class MainActivity : ComponentActivity() {

val stopwatchModel by viewModels<StopwatchModel>()
private var initialTab: NavRoutes = NavRoutes.Alarm

lateinit var stopwatchService: StopwatchService
private val serviceConnection = object : ServiceConnection {
Expand All @@ -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
Expand Down
14 changes: 3 additions & 11 deletions app/src/main/java/com/bnyro/clock/ui/nav/NavContainer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -135,6 +126,7 @@ fun NavContainer(
navController,
settingsModel,
clockModel,
startDestination = initialTab,
modifier = Modifier
.fillMaxSize()
)
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/bnyro/clock/ui/nav/NavHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2ddc16a

Please sign in to comment.