diff --git a/app/src/main/java/com/waseefakhtar/doseapp/feature/addmedication/navigation/AddMedicationDestination.kt b/app/src/main/java/com/waseefakhtar/doseapp/feature/addmedication/navigation/AddMedicationDestination.kt index a6afeb6..3f41fc8 100644 --- a/app/src/main/java/com/waseefakhtar/doseapp/feature/addmedication/navigation/AddMedicationDestination.kt +++ b/app/src/main/java/com/waseefakhtar/doseapp/feature/addmedication/navigation/AddMedicationDestination.kt @@ -2,23 +2,29 @@ package com.waseefakhtar.doseapp.feature.addmedication.navigation import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.MutableState +import androidx.navigation.NavController import androidx.navigation.NavGraphBuilder import androidx.navigation.compose.composable import com.waseefakhtar.doseapp.core.navigation.DoseNavigationDestination import com.waseefakhtar.doseapp.domain.model.Medication import com.waseefakhtar.doseapp.feature.addmedication.AddMedicationRoute +import com.waseefakhtar.doseapp.feature.home.navigation.ASK_NOTIFICATION_PERMISSION object AddMedicationDestination : DoseNavigationDestination { override val route = "add_medication_route" override val destination = "add_medication_destination" } -fun NavGraphBuilder.addMedicationGraph(bottomBarVisibility: MutableState, fabVisibility: MutableState, onBackClicked: () -> Unit, navigateToMedicationConfirm: (List) -> Unit) { +fun NavGraphBuilder.addMedicationGraph(navController: NavController, bottomBarVisibility: MutableState, fabVisibility: MutableState, onBackClicked: () -> Unit, navigateToMedicationConfirm: (List) -> Unit) { composable(route = AddMedicationDestination.route) { LaunchedEffect(null) { bottomBarVisibility.value = false fabVisibility.value = false } + + navController.previousBackStackEntry?.savedStateHandle.apply { + this?.set(ASK_NOTIFICATION_PERMISSION, true) + } AddMedicationRoute(onBackClicked, navigateToMedicationConfirm) } } diff --git a/app/src/main/java/com/waseefakhtar/doseapp/feature/home/navigation/HomeDestination.kt b/app/src/main/java/com/waseefakhtar/doseapp/feature/home/navigation/HomeDestination.kt index 9721197..99d0b82 100644 --- a/app/src/main/java/com/waseefakhtar/doseapp/feature/home/navigation/HomeDestination.kt +++ b/app/src/main/java/com/waseefakhtar/doseapp/feature/home/navigation/HomeDestination.kt @@ -20,7 +20,7 @@ fun NavGraphBuilder.homeGraph(navController: NavController, bottomBarVisibility: bottomBarVisibility.value = true fabVisibility.value = true } - val askNotificationPermission = navController.previousBackStackEntry?.savedStateHandle?.get(ASK_NOTIFICATION_PERMISSION) ?: false + val askNotificationPermission = navController.currentBackStackEntry?.savedStateHandle?.get(ASK_NOTIFICATION_PERMISSION) ?: false HomeRoute(navController, askNotificationPermission) } } diff --git a/app/src/main/java/com/waseefakhtar/doseapp/navigation/DoseNavHost.kt b/app/src/main/java/com/waseefakhtar/doseapp/navigation/DoseNavHost.kt index 12e7ff8..3e629ad 100644 --- a/app/src/main/java/com/waseefakhtar/doseapp/navigation/DoseNavHost.kt +++ b/app/src/main/java/com/waseefakhtar/doseapp/navigation/DoseNavHost.kt @@ -9,12 +9,12 @@ import androidx.navigation.compose.NavHost import androidx.navigation.compose.rememberNavController import com.waseefakhtar.doseapp.feature.addmedication.navigation.addMedicationGraph import com.waseefakhtar.doseapp.feature.calendar.navigation.calendarGraph -import com.waseefakhtar.doseapp.feature.home.navigation.ASK_NOTIFICATION_PERMISSION import com.waseefakhtar.doseapp.feature.home.navigation.HomeDestination import com.waseefakhtar.doseapp.feature.home.navigation.homeGraph import com.waseefakhtar.doseapp.feature.medicationconfirm.navigation.MEDICATION import com.waseefakhtar.doseapp.feature.medicationconfirm.navigation.MedicationConfirmDestination import com.waseefakhtar.doseapp.feature.medicationconfirm.navigation.medicationConfirmGraph +import com.waseefakhtar.doseapp.util.navigateSingleTop @Composable fun DoseNavHost( @@ -32,6 +32,7 @@ fun DoseNavHost( homeGraph(navController, bottomBarVisibility, fabVisibility) calendarGraph(bottomBarVisibility, fabVisibility) addMedicationGraph( + navController = navController, bottomBarVisibility = bottomBarVisibility, fabVisibility = fabVisibility, onBackClicked = { navController.navigateUp() }, @@ -51,11 +52,7 @@ fun DoseNavHost( fabVisibility = fabVisibility, onBackClicked = { navController.navigateUp() }, navigateToHome = { - // TODO: Navigate to Home with no backstack. - navController.currentBackStackEntry?.savedStateHandle.apply { - this?.set(ASK_NOTIFICATION_PERMISSION, true) - } - navController.navigate(HomeDestination.route) + navController.navigateSingleTop(HomeDestination.route) } ) } diff --git a/app/src/main/java/com/waseefakhtar/doseapp/util/Utils.kt b/app/src/main/java/com/waseefakhtar/doseapp/util/Utils.kt new file mode 100644 index 0000000..96185b9 --- /dev/null +++ b/app/src/main/java/com/waseefakhtar/doseapp/util/Utils.kt @@ -0,0 +1,10 @@ +package com.waseefakhtar.doseapp.util + +import androidx.navigation.NavHostController + +fun NavHostController.navigateSingleTop(route: String) { + this.navigate(route) { + popUpTo(route) + launchSingleTop = true + } +}