Skip to content

Latest commit

 

History

History
131 lines (91 loc) · 3.91 KB

README.md

File metadata and controls

131 lines (91 loc) · 3.91 KB

Compose Navigator

Compose Navigator provides possibility to Control AndroidX NavController from View Model's.

The library facilitates the usage of a single NavHost controlled by one point of truth - Navigator. Provides a unified interface for providing results back between composables and receiving activity results.

Features

View Model's can use Navigator interface provided by navigation-runtime artifact to:

  • navigate forward to route with optional NavOptions
  • navigate backward
  • provide result data to previous backstack entry
  • launch and receive activity results using ActivityResultContract

Destination routes can be identified with String or Kotlin Serializable object used for Navigation Kotlin DSL Type-Safety.

Warning: library is designed to work with a single NavHost. Using nested NavHost is not tested and can cause unexpected behavior.

Setup

Compose Navigator is available via maven central.

  1. Add dependency to to navigation-runtime
dependencies {
    implementation "com.miquido.android.navigation:navigation-runtime:[version]"
}
  1. Choose dependency injection integration used in your project navigation-hilt or navigation-koin
dependencies {
    implementation "com.miquido.android.navigation:navigation-hilt:[version]"
    // OR
    implementation "com.miquido.android.navigation:navigation-koin:[version]"
}

Note: that for multi-module project add this dependency only in application module.

  1. Pass NavController to NavigationHandler to start processing commands sent by Navigator in View Models.
import com.miquido.android.navigation.handler.NavigationHandler

@kotlinx.serialization.Serializable
data object RootRoute

@kotlinx.serialization.Serializable
data object StartRoute


@Composable
fun AppRoot() {
    // ...
    val navController = rememberNavController()

    NavigationHandler(
        navController = navController
    )

    NavHost(
        navController = navController,
        route = RootRoute::class, // or simple string "root"
        startDestination = StartRoute // or simple string "start"
    ) {
        // build your NavGraph
    }
}
  1. Create View Model in composable using navEntryViewModel method.
import com.miquido.android.navigation.Navigator
import com.miquido.android.navigation.viewmodel.navEntryViewModel

@Composable
fun StartScreen(
    viewModel: StartViewModel = navEntryViewModel()
) {
}

Note: that all remaining dependency injection configuration (like using adding @HiltViewModel for Hilt or declaring module with viewModelOf for Koin) remains same as it was.

  1. (Optional) Add integration with Compose Destinations
dependencies {
    implementation "com.miquido.android.navigation:navigation-destinations:[version]"
}

Using this artifact provides additional kotlin extension methods for:

  • Navigator to avoid calling Direction#rote and Route#route when using provided API.
  • NavAction's to allow object creation using Direction / Route directly.

Samples

The repository contains few samples - showcasing:

About

The library contains a core feature set commonly used. Please do try it and open issues if you find any.

Any feedback and contributions are highly appreciated!

If you like the library, consider starring and sharing it with your colleagues.


About Miquido