Skip to content

Instantly share code, notes, and snippets.

@gabriel-TheCode
Created July 26, 2023 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabriel-TheCode/7ee7dea02997692a6502978d4f7e934a to your computer and use it in GitHub Desktop.
Save gabriel-TheCode/7ee7dea02997692a6502978d4f7e934a to your computer and use it in GitHub Desktop.
class NavigationManager {
fun goToMainActivity(activity: Activity) {
val intent = Intent(activity, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
activity.startActivity(intent)
}
fun popBackStack(navController: NavController) {
navController.popBackStack()
}
fun navigateTo(destinationId: Int, navController: NavController) {
navController.navigate(destinationId, null, navOptions {
launchSingleTop = true
restoreState = true
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
})
}
fun goToGallery(activity: Activity) {
navigateTo(activity, R.id.tabGalleryFragment)
}
fun goToOrderDetails(view: View, orderId: OrderId) {
val bundle = bundleOf(ORDER_ID to orderId)
view.goTo(R.id.orderDetailsFragment, bundle)
}
private fun Fragment.goTo(
@IdRes id: Int, bundle: Bundle? = null, navOptions: NavOptions? = null,
navigatorExtras: Navigator.Extras? = null
) {
if (mayNavigate(id)) findNavController().navigate(id, bundle, navOptions, navigatorExtras)
}
companion object {
const val PRODUCT_ID = "productId"
const val ORDER_ID = "orderId"
}
}
enum class DEEPLINK_QUERY(val type: String) {
GALLERY_QUERY("gallery"),
ORDERS_QUERY("orders"),
CART_QUERY("cart"),
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment