Skip to content

Iteo/advanced_navigation_plugin

Repository files navigation

Advanced Navigation Plugin

Advanced Nav Plugin is a library project which brings several improvements to navigation component from Android Jetpack. It's inspired by Advanced Navigation Sample by Google

Installation

Add the following dependency to your build.gradle file:

dependencies {
    implementation 'com.iteo.android_navigation:core:0.0.1'
}

Usage

BottomNavBar with separate back stacks

val navGraphIds = listOf(
    R.navigation.list_nav_graph,
    R.navigation.notifications_nav_graph,
    R.navigation.filtered_list_nav_graph
)

bottomNav.setupWithNavController(
    navGraphIds,
    childFragmentManager,
    R.id.fragmentContainer,
    requireActivity().intent
)

Navigation from Notifications

val nestedDeepLink = NestedDeepLink(
    mainGraphId = R.navigation.main_nav_graph,
    mainEntryDestination = R.id.entryFragment,
    nestedGraphId = R.navigation.filtered_list_nav_graph,
    nestedDestination = R.id.detailsFragment,
    nestedDestinationBundle = ItemDetailsFragmentArgs(1).toBundle()
)

val not = NotificationCompat.Builder(requireContext(), mChannel.id)
  .setSmallIcon(R.drawable.ic_airplane)
  .setContentTitle("Title")
  .setContentText("Test")
  .setPriority(NotificationCompat.PRIORITY_DEFAULT)
  .setContentIntent(nestedDeepLink.toPendingIntent(requireContext()))
  .build()

with(NotificationManagerCompat.from(requireContext())) {
    notify(1, not)
}

Remember to pass intent from your activity to your bottom bar extension function handleIntent

Safe navigate

Avoid double click crashes by using navigate extensions function. Exceptions will be printed in logcat.

val adapter = ItemAdapter { clickedItem ->
    navigate(ListFragmentDirections.openDetails(clickedItem.id))
}

Global deep navigation actions

  1. Create global action in one of your graphs
<action android:id="@+id/openDetails"
        app:destination="@id/detailsFragment">
    <argument android:name="itemId"
              app:argType="integer" />
</action>
  1. Implement BottomGraphContainer interface in your bottom bar container fragment(s)
private val navGraphIds = listOf(
    R.navigation.list_nav_graph,
    R.navigation.notifications_nav_graph,
    R.navigation.filtered_list_nav_graph
)

class DashboardFragment @Inject constructor() : Fragment(), BottomGraphContainer {
    {...}

    override fun bottomGraphContainerSettings(): BottomBarContainerSetting {
        return BottomBarContainerSetting(navGraphIds, R.id.fragmentContainer, bottomNav)
    }
}
  1. Use navigateGlobally function
navigateToDifferentGraph.setOnClickListener {
    val direction = ListNavGraphDirections.openDetails(itemId = 2)
    navigateGlobally(direction)
}

Deeplink deep navigation

Navigate to your nested bottom bar graphs by passing intent to your BottomNavBar container fragment.

  1. Register your deeplink in your main graph AND in your bottom bar graph.
<fragment android:id="@+id/dashboardFragment"
          android:name="com.iteo.android_navigation.dashboard.DashboardFragment"
          tools:layout="@layout/fragment_dashboard"
          android:label="DashboardFragment" >
    ...

    <deepLink android:id="@+id/deepLink" app:uri="www.example.com/item/{itemId}" />
</fragment>
    <fragment android:id="@+id/detailsFragment"
              android:name="com.iteo.android_navigation.item_details.ItemDetailsFragment"
              android:label="DetailsFragment"
              tools:layout="@layout/fragment_item_details">

        <argument android:name="itemId"
                  app:argType="integer" />

        <deepLink android:id="@+id/deepLink" app:uri="www.example.com/item/{itemId}" />
    </fragment>
  1. Pass your intent from activity to bottom bar extension function handleIntent. In our example we are using RxRelay (publishsubject) to emit new intent globally in application.
observeNewIntentDisposable = newIntentObservable
    .subscribeBy(
        onNext = { intent ->
            bottomNav.handleDeepLink(
                navGraphIds,
                childFragmentManager,
                R.id.fragmentContainer,
                intent
            )
        }
    )

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages