Generate helper methods for compose navigation using KSP

Overview

Compose NavGen

Generate helper methods for compose navigation using KSP.

🚧 You can try it now, but it's still under development. 🚧

TODO

  • Support default value and nullable.
  • Support argument types.
  • Support animation navigation.
  • Add unit test.
  • Add Github Actions.

Usage

Add @NavGen annotation to each compose screen.

@NavGen("list")
@Composable
fun ListScreen(navController: NavController) {
    // ...
}

@NavGen("detail")
@Composable
fun DetailScreen(data: String, navController: NavController) {
    // ...
}

When build, the processor generates helper methods from @NavGen.

Generated code
DetailScreen( backStackEntry.arguments!!.getString("data")!!, navController, ) } } fun NavController.detail(`data`: String) { navigate("detail/$data") } ">
// list
fun NavGraphBuilder.list(navController: NavController) {
  composable("list") {
    ListScreen(navController)
  }
}
public fun NavController.list(): Unit {
  navigate("list")
}

// detail
fun NavGraphBuilder.detail(navController: NavController) {
  composable(
  "detail/{data}",
    arguments = listOf(navArgument("data") { type = NavType.StringType })
  ) { backStackEntry ->
    DetailScreen(
      backStackEntry.arguments!!.getString("data")!!,
      navController,
    )
  }
}
fun NavController.detail(`data`: String) {
  navigate("detail/$data")
}

You can use generated code for compose navigation.

val navController = rememberNavController()
NavHost(navController = navController, startDestination = NavGenRoutes.list) {
    list(navController) // Add route 'list'
    detail(navController) // Add route 'detail/{data}'
}

// navigate to next screen
navController.detail(data) // Call NavController.navigate("detail/$data")

Set srcDirs

Android Studio does not automatically index generated codes by KSP. You have to add sourceSets manually.

Ref: https://github.com/google/ksp/issues/37

  • build.gradle.kts
android {
    // ...
    applicationVariants.all {
        val variantName = name
        sourceSets {
            getByName("main") {
                java.srcDir(File("build/generated/ksp/$variantName/kotlin"))
            }
        }
    }
}
  • build.gradle
android {
    // ...
    applicationVariants.all { variant ->
        variant.sourceSets.java.each {
            it.srcDirs += "build/generated/ksp/${variant.name}/kotlin"
        }
    }
}

Download

plugins {
    id("com.google.devtools.ksp") version "1.5.30-1.0.0"
}

dependencies {
    implementation("androidx.navigation:navigation-compose:2.4.0-alpha10")

    implementation("com.star-zero:compose-navgen:1.0.0-alpha01")
    ksp("com.star-zero:compose-navgen-processor:1.0.0-alpha01")
}

License

Copyright 2021 Kenji Abe

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
You might also like...
Android Secure SharedPreferences Using Facebook Conceal Encryption
Android Secure SharedPreferences Using Facebook Conceal Encryption

SharedChamber Android Project : SharedChamber on top of SharedPreferences using Facebook Conceal Description Conceal provides a set of Java APIs to pe

A lightweight library for config and using SharedPreferences

preferences-helper SharePreferences is very popular with any project and all most all project has SharePreferences for saving data. This library will

Trail is a simple logging system for Java and Android. Create logs using the same API and the library will detect automatically in which platform the code is running.

Trail Trail is a simple logging system for Java and Android. Create logs using the same API and the library will detect automatically in which platfor

This project is an add-on for the excellent J2V8 Project. It allows users to debug JS running in V8 using Chrome DevTools. Uses Stetho for communication with Chrome DevTools.

J2V8-Debugger This project is an add-on for the excellent J2V8 Project. It allows users to debug JS running in V8 using Chrome DevTools. Uses Stetho f

Extensions to encrypt DataStore using Tink

encrypted-datastore Extensions to encrypt DataStore using Tink. ⚠️ This tiny library will be maintained until an official solution for DataStore encry

Format numbers using a string pattern with this simple number formatted like ##-####-##

AndroidPattern Format numbers using a string pattern with this simple number formatted like ##-####-## Installation To get a Git project into your bui

Android injection using the Anvil compiler plugin

Tangle creates Dagger bindings for Android classes using the Anvil Kotlin compiler plugin. This is meant to be an alternative to Hilt, for those who'd prefer to enjoy the faster compilation and better flexibility of Anvil.

DEMOMovieDB - Client App using movieDB with Kotlin
DEMOMovieDB - Client App using movieDB with Kotlin

DEMOMovieDB DEMOMovieDB is a gorgeous client application for TMDb on Android, bu

λRPC allows using code with high-order functions as a service
λRPC allows using code with high-order functions as a service

λRPC Simple native RPC with high order functions support. Inspired by @altavir and Communicator. λRPC allows using code with high-order functions as a

Releases(1.0.0-alpha02)
  • 1.0.0-alpha02(Oct 24, 2021)

    Full Changelog: https://github.com/STAR-ZERO/compose-navgen/compare/1.0.0-alpha01...1.0.0-alpha02

    New features

    • Support nullable and default value

    Others

    • Update Android gradle plugin 7.0.3
    • Add unit test
    • Add Github Actions
    Source code(tar.gz)
    Source code(zip)
  • 1.0.0-alpha01(Oct 11, 2021)

Owner
Kenji Abe
Google Developers Expert for Android, Kotlin
Kenji Abe
A set of helper classes for using dagger 1 with Android components such as Applications, Activities, Fragments, BroadcastReceivers, and Services.

##fb-android-dagger A set of helper classes for using dagger with Android components such as Applications, Activities, Fragments, BroadcastReceivers,

Andy Dennie 283 Nov 11, 2022
A helper library to ease the most repetitive codes with simple reusable attributes.

ak-universal-android-helper A helper library to ease the most repetitive codes with simple reusable attributes. AKUAH can help you with many repetitiv

Aakash Kumar 20 Jul 8, 2020
Item Helper For Kotlin

Item Helper For Kotlin

Blugon 2 Dec 3, 2021
A beautiful set of predefined colors and a set of color methods to make your Android development life easier.

Colours is a port of the Colours Library for iOS made by my good friend Ben Gordon. You can find that project here. Installation Maven Central Colours

Matthew York 634 Dec 28, 2022
Thrift for Android that saves you methods

Thrifty Thrifty is an implementation of the Apache Thrift software stack for Android, which uses 1/4 of the method count taken by the Apache Thrift co

Microsoft 528 Dec 28, 2022
Annotation proccessor for generate public observers

Anzidcodogen Annotation proccessor for generate public observers Supported annotation PublicSharedFlow, PublicLiveData, InternalSharedFlow, InternalLi

Nazarij 4 Jul 29, 2022
Multi-module, Kotlin, MVI, Compose, Hilt, Navigation Component, Use-cases, Room, Retrofit

Work in progress Multi-module demo app that gets data from dota2 api. API https://docs.opendota.com/ Players by rank (GET) https://api.opendota.com/ap

Mitch Tabian 321 Dec 27, 2022
recompose is a tool for converting Android layouts in XML to Kotlin code using Jetpack Compose.

recompose is a tool for converting Android layouts in XML to Kotlin code using Jetpack Compose.

Sebastian Kaspari 565 Jan 2, 2023
Android library to easily serialize and cache your objects to disk using key/value pairs.

Deprecated This project is no longer maintained. No new issues or pull requests will be accepted. You can still use the source or fork the project to

Anup Cowkur 667 Dec 22, 2022
A simple library for validating user input in forms using annotations.

ValidationKomensky for Android A simple library for validating user input in forms using annotations. Features: Validate all views at once and show fe

Inmite s.r.o. 512 Nov 20, 2022