From bad66cab18fbf6ddb710e4360de0e615e277c340 Mon Sep 17 00:00:00 2001 From: Karanveer Singh Date: Mon, 9 Oct 2023 23:31:55 +0530 Subject: [PATCH 1/3] adding-navigate-single-top-to-navHostController --- .../com/waseefakhtar/doseapp/navigation/DoseNavHost.kt | 4 ++-- .../main/java/com/waseefakhtar/doseapp/util/Utils.kt | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/com/waseefakhtar/doseapp/util/Utils.kt 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..fbb3559 100644 --- a/app/src/main/java/com/waseefakhtar/doseapp/navigation/DoseNavHost.kt +++ b/app/src/main/java/com/waseefakhtar/doseapp/navigation/DoseNavHost.kt @@ -15,6 +15,7 @@ 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( @@ -51,11 +52,10 @@ 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..c18fe1e --- /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(destination:String){ + this.navigate(destination){ + popUpTo(destination) + launchSingleTop = true + } +} \ No newline at end of file From 3ece79b9faa05ea849a1308b93c4c50490923fe1 Mon Sep 17 00:00:00 2001 From: Waseef Akhtar Date: Sat, 14 Oct 2023 07:42:14 +0200 Subject: [PATCH 2/3] Fix passing ASK_NOTIFICATION_PERMISSION value to HomeDestination --- .../addmedication/navigation/AddMedicationDestination.kt | 8 +++++++- .../doseapp/feature/home/navigation/HomeDestination.kt | 2 +- .../com/waseefakhtar/doseapp/navigation/DoseNavHost.kt | 5 +---- 3 files changed, 9 insertions(+), 6 deletions(-) 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 fbb3559..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,7 +9,6 @@ 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 @@ -33,6 +32,7 @@ fun DoseNavHost( homeGraph(navController, bottomBarVisibility, fabVisibility) calendarGraph(bottomBarVisibility, fabVisibility) addMedicationGraph( + navController = navController, bottomBarVisibility = bottomBarVisibility, fabVisibility = fabVisibility, onBackClicked = { navController.navigateUp() }, @@ -52,9 +52,6 @@ fun DoseNavHost( fabVisibility = fabVisibility, onBackClicked = { navController.navigateUp() }, navigateToHome = { - navController.currentBackStackEntry?.savedStateHandle.apply { - this?.set(ASK_NOTIFICATION_PERMISSION, true) - } navController.navigateSingleTop(HomeDestination.route) } ) From 35cd1f58cf1b8e73672cd03d5faa3207015eb414 Mon Sep 17 00:00:00 2001 From: Waseef Akhtar Date: Sat, 14 Oct 2023 07:48:22 +0200 Subject: [PATCH 3/3] Fix ktlint --- app/src/main/java/com/waseefakhtar/doseapp/util/Utils.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/waseefakhtar/doseapp/util/Utils.kt b/app/src/main/java/com/waseefakhtar/doseapp/util/Utils.kt index c18fe1e..96185b9 100644 --- a/app/src/main/java/com/waseefakhtar/doseapp/util/Utils.kt +++ b/app/src/main/java/com/waseefakhtar/doseapp/util/Utils.kt @@ -2,9 +2,9 @@ package com.waseefakhtar.doseapp.util import androidx.navigation.NavHostController -fun NavHostController.navigateSingleTop(destination:String){ - this.navigate(destination){ - popUpTo(destination) +fun NavHostController.navigateSingleTop(route: String) { + this.navigate(route) { + popUpTo(route) launchSingleTop = true } -} \ No newline at end of file +}