nhaarman / acorn

Mastering Android navigation :chipmunk:
https://nhaarman.github.io/acorn
Apache License 2.0
181 stars 7 forks source link

CompositeParallelNavigator for "bottom bar navigation" #110

Closed nhaarman closed 5 years ago

nhaarman commented 5 years ago

The Material "Bottom Navigation" component allows movement between primary destinations in an app:

image

Each of the 'primary destinations' is represented by a tab in the bottom navigation bar, and each of them has their own navigational state.

In Acorn, this could be represented by a CompositeParallelNavigator type, which allows for such a component.

A quick sketch-up of the API could be:

enum class MyDestination {
  Favorites,
  Music,
  Places,
  News
}

class MyNavigator(
  savedState: NavigatorState? = null
) : CompositeParallelNavigator<MyDestination>(savedState) {

  override fun createNavigator(destination: MyDestination) : Navigator {
    return when(destination) {
      Favorites -> FavoritesNavigator()
      /* ... */
    }
  }

  override fun instantiateNavigator(
    navigatorClass: KClass<out Navigator>,
    state: NavigatorState?
  ) : Navigator {
    return when(navigatorClass) {
      is FavoritesNavigator::class -> FavoritesNavigator(state)
      /* ... */
    }
  }
}

The CompositeParallelNavigator<T> class could expose methods like select(T) to switch between the nested child Navigators, and provide a way to listen to changes in the selected T type.

nhaarman commented 5 years ago

A draft pull request has been made in #146