Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding-navigate-single-top-to-navHostController #72

Merged
merged 6 commits into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean>, fabVisibility: MutableState<Boolean>, onBackClicked: () -> Unit, navigateToMedicationConfirm: (List<Medication>) -> Unit) {
fun NavGraphBuilder.addMedicationGraph(navController: NavController, bottomBarVisibility: MutableState<Boolean>, fabVisibility: MutableState<Boolean>, onBackClicked: () -> Unit, navigateToMedicationConfirm: (List<Medication>) -> 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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fun NavGraphBuilder.homeGraph(navController: NavController, bottomBarVisibility:
bottomBarVisibility.value = true
fabVisibility.value = true
}
val askNotificationPermission = navController.previousBackStackEntry?.savedStateHandle?.get<Boolean>(ASK_NOTIFICATION_PERMISSION) ?: false
val askNotificationPermission = navController.currentBackStackEntry?.savedStateHandle?.get<Boolean>(ASK_NOTIFICATION_PERMISSION) ?: false
HomeRoute(navController, askNotificationPermission)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -32,6 +32,7 @@ fun DoseNavHost(
homeGraph(navController, bottomBarVisibility, fabVisibility)
calendarGraph(bottomBarVisibility, fabVisibility)
addMedicationGraph(
navController = navController,
bottomBarVisibility = bottomBarVisibility,
fabVisibility = fabVisibility,
onBackClicked = { navController.navigateUp() },
Expand All @@ -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)
}
)
}
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/com/waseefakhtar/doseapp/util/Utils.kt
Original file line number Diff line number Diff line change
@@ -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
}
}