This library is a helper for the Android Compose Navigation routes

Overview

ComposableRoutes

Release

This library is a helper for the Android Compose Navigation routes:

  • generates routes for the annotated screens
  • provides helpers to register routes in the NavController
  • reduces boiler-plate code for arguments parsing
  • and more...

How does it work?

Add annotation to the Compose screen:

@Composable
@ComposableRoute
fun HomeScreen() {
    // ...
}

@Composable
@ComposableRoute
fun ContactScreen(id: String) { // required parameter
    // ...
}

@Composable
@ComposableRoute
fun AboutScreen(overrideTitle: String?) { // optional parameter
    // ...
}

Build your project and it will automatically generate NavRoutes object.

The next step is to register this route:

val navController = rememberNavController()
NavHost(navController = navController, startDestination = NavRoutes.HomeScreen.PATH) {

    NavRoutes.HomeScreen.register(this)
    NavRoutes.ContactScreen.register(this)
    NavRoutes.AboutScreen.register(this)

}

NavRoutes also provides a helper to register all generated routes with one call:

val navController = rememberNavController()
NavHost(navController = navController, startDestination = NavRoutes.HomeScreen.PATH) {

    NavRoutes.registerAll(this)

}

There are few ways to navigate to the specified route:

// NavController.navigate
navController.navigate(NavRoutes.HomeScreen())

// generated navigateToXXX helpers
navController.navigateToHomeScreen()

// required arguments
navController.navigateToContactScreen(id = "1")

// optional arguments
navController.navigateToAboutScreen(overrideTitle = "Ny About Title")
navController.navigateToAboutScreen()

Library supports following type of arguments:

  • primitives - Boolean, Int, Float, etc.)
  • Strings
  • Parcelable objects
  • Serializable objects

How to pass NavController to the Screen?

Usually you can just pass NavController to the Screen when registering it with the HavHost. Unfortunately, this is not possible - this library will only propagate arguments available in the route. But there is a very easy solution to this problem.

One of the solutions is to use CompositionLocalProvider. The first step is to declare our CompositionLocal key for the provider:

val LocalNavController = compositionLocalOf<NavHostController> {
    error("NavController was not provided")
}

Then we need to add this key to the provider:

val navController = rememberNavController()
CompositionLocalProvider(LocalNavController provides navController) {
    NavHost(navController = navController, startDestination = NavRoutes.JoinAsGuestScreen()) {
        NavRoutes.registerAll(this)
    }
}

That's it. Now you can get NavController in any descendant @Composable function like this:

LocalNavController.current.navigateToHomeScreen()

Add to your project

To add this library into your project:

Step 1. Add a JitPack repository to your root build.gradle:

allprojects {
    repositories {
        maven(url = "https://jitpack.io")
    }
}

Step 2. Add library and compiler dependencies:

dependencies {
    implementation("com.github.MatrixDev.ComposableRoutes:composable-routes-lib:{latest}")
    kapt("com.github.MatrixDev.ComposableRoutes:composable-routes-processor:{latest}")
}

More info can be found at https://jitpack.io/#MatrixDev/ComposableRoutes

TODO

These are just few more nice things to have in the future:

  • Migrate from KAPT to KSP

License

MIT License

Copyright (c) 2018 Rostyslav Lesovyi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
You might also like...
Android App made by Jetpack Compose Components with Kotlin, MVVM Pattern, Multi Module, Navigation, Hilt, Coroutines, Retrofit and cached data by Room
Android App made by Jetpack Compose Components with Kotlin, MVVM Pattern, Multi Module, Navigation, Hilt, Coroutines, Retrofit and cached data by Room

Android App made by Jetpack Compose Components with Kotlin, MVVM Pattern, Multi Module, Navigation, Hilt, Coroutines, Retrofit and cached data by Room

Android App made by Jetpack Compose Components with Kotlin, MVVM Pattern, Multi Module, Navigation, Hilt, Coroutines, Retrofit and cached data by Room
Android App made by Jetpack Compose Components with Kotlin, MVVM Pattern, Multi Module, Navigation, Hilt, Coroutines, Retrofit and cached data by Room

Mobile Banking Android App made by Jetpack Compose Components with Kotlin, MVVM Pattern, Multi Module, Navigation, Hilt, Coroutines, Retrofit and cach

This repos one of the ways hows how to use Jetpack Compose Navigation along with Dagger 2
This repos one of the ways hows how to use Jetpack Compose Navigation along with Dagger 2

Dagger 2 and Jetpack Compose Integration This repository is about a way how to use Dagger 2 for projects which using Jetpack Compose. Here is an artic

Create Bottom Navigation Bar with Jetpack Compose
Create Bottom Navigation Bar with Jetpack Compose

BottomNavigationBarComposeExample Create Bottom Navigation Bar with Jetpack Compose https://johncodeos.com/how-to-create-bottom-navigation-bar-with-je

Kotlin, MVVM, Navigation Component, Hilt, Jetpack Compose, Retrofit2

What is this project? This course will replace my old java mvvm introduction: https://codingwithmitch.com/courses/rest-api-mvvm-retrofit2/. Watch the

[Tutorial] D-pad navigation in Jetpack Compose

dpad-compose D-pad navigation in Jetpack Compose The problem While Android is mostly used on touch devices, the operating system can also be used with

Missing safe arguments generator for Compose Navigation

Safe Arguments Generator Yet another attempt to add safe arguments to Compose Navigation. Why Since routes in Navigation Component don't support safe

Model-driven navigation for Jetpack Compose
Model-driven navigation for Jetpack Compose

Model-driven navigation for Jetpack Compose

Android.compose.squircle - Android LightWeight Squircle Library for JetPack Compose

Android LightWeight Squircle Library for JetPack Compose Usage Based on Compose

Comments
  • Question: How do I actually navigate from screen A to B?

    Question: How do I actually navigate from screen A to B?

    From the readme I now the code, but how do I get a navController inside the Screen Composable?

    Usually, we pass a lambda to the Composable that is "implemented" in the NavHost level, but that seems to not be possible with the register route method.

    opened by raamcosta 3
Releases(0.1.14)
Owner
Rostyslav Lesovyi
Rostyslav Lesovyi
A library that enables Safe Navigation for you Composable destinations when using Jetpack Compose Navigation

A library that enables Safe Navigation for you Composable destinations when using Jetpack Compose Navigation

Roman Levinzon 59 Oct 19, 2022
Navigation-Compose - A sample to showcase Kotlin, MVVM, Hilt, Coroutines, StateFlow, Jetpack compose

Navigation-Compose A sample to showcase Kotlin, MVVM, Hilt, Coroutines, StateFlo

Mohammadali Rezaei 6 Jul 13, 2022
A Simple Blog App using Jetpack Compose, Flow, Navigation Compose, Room and Firebase

BlogCompose A Simple Blog App using Jetpack Compose, Flow, Navigation Compose, Room and Firebase Instructions Download your Firebase configuration fil

null 4 Oct 10, 2022
Small code generating library for safe Jetpack Compose navigation with no boilerplate.

Compose Destinations A KSP library to use alongside compose navigation. It reduces boilerplate code and is less error-prone since passing arguments be

Rafael Costa 1.9k Jan 5, 2023
Odyssey it's a declarative multiplatform navigation library for Multiplatform Compose

Odyssey Odyssey it's a declarative multiplatform navigation library for Multiplatform Compose ?? WARNING! It's an early preview, so you use it with yo

Alex 168 Jan 5, 2023
Compose desktop navigation library

Navipose Compose desktop navigation library Features Now navipose supports basic screen navigation between few screens Examples At first you should cr

null 5 Oct 28, 2022
🧱 Brick - Multiplatform navigation library for Compose.

Brick Take control of your apps Brick is a lightweight library to make navigation. Features Framework free (Router can be injected in any layer of pro

Petr Shubin 43 Jan 1, 2023
🚀 Android project template with Compose, MVVM, Hilt and Navigation

compose-android-template An Android project template with MVVM, Hilt, Navigation and Compose ✍️ Author ?? theapache64 Twitter: @theapache64 Email: the

theapache64 55 Jan 3, 2023
Small Android project demonstrating some navigation components for Jetpack Compose.

Small Android project demonstrating some navigation components for Jetpack Compose. Created this for presenting about this topic for a GDG meetup.

Parshav 3 Sep 15, 2021
Android Sample Kotlin+ MVI + Jetpack compose + Coroutines + Retrofit + Hilt + Room + Navigation component

MVIComposeSample Android Sample app to show user latest movies implementing MVI + Clean Architecture using kotlin & Jetpack compose following solid an

Ahmed Atwa 10 Dec 28, 2022