" /> " /> "/>

Kotlin multi-platform application navigation library.

Overview

navigation

Kotlin multi-platform application navigation library. Supports Jetpack Compose.
GitHub tag (latest by date)

val navigator = rememberNavigatorByKey("Greeting") { key ->
    when (key) {
        "Greeting" -> Text("Hello")
        "Farewell" -> Text("Good-bye")
        else -> Text("Unexpected Key: $key")
    }
}

navigator.goTo("Farewell")

Usage

Navigation is handled differently for each platform and UI framework. This library provides some common navigation components that serve as a recommended structure, but each platform and UI framework is independently responsible for handling navigation and choosing whether to conform to the provided components.

Jetpack Compose

There are three different ComposeNavigators that can handle navigation in Jetpack Compose. Which one to use depends on the application needs and personal preference.

Navigate by content

This approach allows for specifying the @Composable content on demand, rather than up-front.

val navigator = rememberNavigatorByContent("Greeting") { Text("Hello") }

// The NavContainer will start by displaying the initial content, which in this case is "Hello".
NavContainer(navigator)

// The above NavContainer will display "Good-bye" after the following call:
navigator.goTo("Farewell") { Text("Good-bye") }

// Goes back to the initial content: "Hello":
navigator.goBack()
Navigate by key

This approach allows for specifying the @Composable content for each key up-front. Then navigation can be done by simply providing a key.

val navigator = rememberNavigatorByKey("Greeting") { key ->
    when (key) {
        "Greeting" -> Text("Hello")
        "Farewell" -> Text("Good-bye")
        else -> Text("Unexpected Key: $key")
    }
}

// The NavContainer will start by displaying the initial content, which in this case is "Hello"
NavContainer(navigator)

// The above NavContainer will display "Good Bye" after the following call:
navigator.goTo("Farewell")

// Goes back to the initial content: "Hello":
navigator.goBack()
Navigate by NavigationIntent

This approach is similar to the key approach, but the key is a NavigationIntent and the returned ComposeNavigator implements the NavigationEventNavigator interface from the core module.

val navigator = rememberNavigatorByIntent(HomeNavigationIntent.Greeting) { navigationIntent ->
    when (navigationIntent) {
        HomeNavigationIntent.Greeting -> Text("Hello")
        HomeNavigationIntent.Farewell -> Text("Good-bye")
    }
}

// The NavContainer will start by displaying the initial content, which in this case is "Hello"
NavContainer(navigator)

// The above NavContainer will display "Good Bye" after the following call:
navigator.goTo(HomeNavigationIntent.Farewell)

// Goes back to the initial content: "Hello":
navigator.goBack()
Nested navigation

Within the scope of the content blocks, you can access the navigator property which can be used for nested navigation:

val navigator = rememberNavigatorByKey("Hello") { key ->
    when (key) {
        "Greeting" -> Button("Hello") {
            navigator.goBack() // Safe access to the navigator property within this scope.
        }
        "Farewell" -> Text("Good-bye")
        else -> Text("Unexpected Key: $key")
    }
}
Key State changes

The key of the currently displayed @Composable can be accessed by the ComposeNavigator.currentKey property:

navigator.currentKey

To listen to changes to the current key, use the ComposeNavigator.keyChanges property along with the Flow<T>.collectAsState function:

val currentKey by navigator.keyChanges.collectAsState(initial = currentKey)

For convenience, there is an extension function that performs the above logic: ComposeNavigator.currentKeyAsState() This is especially useful when a @Composable needs to be updated when the key changes, for instance in a Bottom Navigation component:

val currentKey by navigator.currentKeyAsState()

BottomNavigation {
    listOf(ScreenIntent.ColorList, ScreenIntent.Palette).forEach {
        BottomNavigationItem(
            selected = currentKey == it,
            onClick = {
                navigator.goTo(it)
            }
        )
    }
}

Android

To create a Navigator use one the provided navigator() functions. For instance:

val navigator = navigator<NavigationIntent>(activity = activity, onGoTo = { navigationIntent ->
    activity.startActivity(...)
})

navigator.goBack()

Building the library

The library is provided through Repsy.io. Checkout the releases page to get the latest version.
GitHub tag (latest by date)

Repository

repositories {
    maven {
        url = uri("https://repo.repsy.io/mvn/chrynan/public")
    }
}

Dependencies

core

implementation("com.chrynan.navigation:navigation-core:VERSION")

compose

implementation("com.chrynan.navigation:navigation-compose:VERSION")

Documentation

More detailed documentation is available in the docs folder. The entry point to the documentation can be found here.

License

Copyright 2021 chRyNaN

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...
Multi-thread ZX0 data compressor in Kotlin

ZX0-Kotlin ZX0-Kotlin is a multi-thread implementation of the ZX0 data compressor in Kotlin. Requirements To run this compressor, you must have instal

For Kotlin with SpringBoot project that have multi-module-structure template

Goals kotlin + spring-boot + gradle + multi-module building Module-Structure ---root |--- src.main.kotlin.KotlinSpringbootMultiModuleTemplateAppl

Microservice-grpc-multi-language-example - gRPC communication on multiple language demonstration (spring kotlin, go, .NET core 6)
A Gradle plugin providing various utility methods and common code required to set up multi-version Minecraft mods.

Essential Gradle Toolkit A Gradle plugin providing various utility methods and common code required to set up multi-version Minecraft mods via archite

Kotlin compiler plugin for converting suspend functions to platform-compatible functions

Kotlin suspend transform compiler plugin Summary Kotlin compiler plugin for generating platform-compatible functions for suspend functions. JVM class

Modular Android architecture which showcase Kotlin, MVVM, Navigation, Hilt, Coroutines, Jetpack compose, Retrofit, Unit test and Kotlin Gradle DSL.

SampleCompose Modular Android architecture which showcase Kotlin, MVVM, Navigation, Hilt, Coroutines, Jetpack compose, Retrofit, Unit test and Kotlin

A simple android library which helps you to create a curved bottom navigation

CurvedBottomNavigation A simple android library which helps you to create a curved bottom navigation DEMO Setup Update your module level build.gradle

A ksp library to automatically generate navigation functions for jetpack compose.
A ksp library to automatically generate navigation functions for jetpack compose.

Compose/Navigation/Generator ⚠️ This library is still under development and not considered stable! Content Introduction Usage Example: Single destinat

Releases(0.6.0)
  • 0.6.0(Oct 8, 2022)

    • Updated Kotlin to version 1.7.10 and compose-jb to version 1.2.0-beta02
    • Added iOS and JS support to the navigation-compose module
    • Added NavContainer with Modifier parameter to the common source set of the navigation-compose module

    Full Changelog: https://github.com/chRyNaN/navigation/compare/0.5.0...0.6.0

    Source code(tar.gz)
    Source code(zip)
  • 0.5.0(Aug 20, 2022)

    • New API for creating a Navigator and rendering the current content.
    val navigator = rememberNavigator("Greeting")
    
    NavContainer(navigator) { _, destination ->
        when (destination) {
            "Greeting" -> Text("Hello")
            "Farewell" -> Text("Good-bye")
            else -> Text("Unexpected Key: $key")
        }
    }
    
    navigator.goTo("Farewell")
    
    • Updated Kotlin to version 1.7.0
    • Updated compose-jb to version 1.2.0-alpha01-dev755
    • Updated other dependencies
    • Added ViewModel class and removed Saver

    Full Changelog: https://github.com/chRyNaN/navigation/compare/0.4.0...0.5.0

    Source code(tar.gz)
    Source code(zip)
  • 0.4.0(Mar 4, 2022)

    • Moved ExperimentalNavigationApi annotation to the navigation-core module
    • Updated NavigationEventHandler and NavigationEventNavigator to not have an upper bounds on NavigationIntent
    • Updated NavigationEvents generic type to not have an upper bound on NavigationIntent
    • Updated NavigationHandler onNavigate parameter name
    • Created StackNavigator interface and removed ComposeStackNavigator
    • Renamed NavStackDuplicateContentStrategy to StackDuplicateContentStrategy
    • Created StackNavigationHandler
    • Made NavigationEvent constructors internal
    • Added support for Compose Saver for key properties
    • Created NavigationContext interface
    • Converted ComposeScopeNavigator to ComposeContextNavigator
    • Added support for iOS Simulator Arm64 to the navigation-core module
    • Updated Kotlin to 1.6.10

    Full Changelog: https://github.com/chRyNaN/navigation/compare/0.3.0...0.4.0

    Source code(tar.gz)
    Source code(zip)
Owner
Christopher
Christopher
HackerNews with Kotlin Multi-platform mobile technology

KNews The goal of this project is to build mobile apps that consumes HackerNews API with Kotlin Multi-Platform technology. About My idea is to build 2

Kittinun Vantasin 43 Oct 3, 2022
Kotlin Multi Platform UI

Xeon UI (work-in-progress ?? ??️ ??‍♀️ ⛏ ) Development Version Release This Is Latest Release ~ In Development $version_release = ~ What's New?? * In

Frogobox 2 Oct 15, 2021
Kotlin multi platform project template and sample app with everything shared except the UI. Built with clean architecture + MVI

KMMNewsAPP There are two branches Main News App Main The main branch is a complete template that you can clone and use to build the awesome app that y

Kashif Mehmood 188 Dec 30, 2022
The home of the amigo-platform which serves as the main service for the amigo multimedia platform

amigo-platform This is the home of the amigo-platform which serves as the main service for the amigo multimedia platform. Authentication with JWT Toke

null 1 Nov 22, 2021
A complete Kotlin application built to demonstrate the use of Modern development tools with best practices implementation using multi-module architecture developed using SOLID principles

This repository serves as template and demo for building android applications for scale. It is suited for large teams where individuals can work independently on feature wise and layer wise reducing the dependency on each other.

Devrath 11 Oct 21, 2022
A deep learning based mobile application for the multi-class classification of pneumonia into three categories via Chest X-rays

PneumoniaClassifier A deep learning based mobile application for the multi-class classification of pneumonia into three categories via Chest X-rays. W

Timilehin Aregbesola 2 Dec 15, 2021
Android Multi Theme Switch Library ,use kotlin language ,coroutine ,and so on ...

Magic Mistletoe Android多主题(换肤)切换框架 背景 时隔四年,在网易换肤之前的思路下,做了几点改进,现在完全通过反射创建View,并且在SkinLoadManager中提供一个configCustomAttrs以支持自定义View的属性插队替换 摈弃了之前的AsyncTask

Mistletoe 18 Jun 17, 2022
Clean Android multi-module offline-first scalable app in 2022. Including Jetpack Compose, MVI, Kotlin coroutines/Flow, Kotlin serialization, Hilt and Room.

Android Kotlin starter project - 2022 edition Android starter project, described precisely in this article. Purpose To show good practices using Kotli

Krzysztof Dąbrowski 176 Jan 3, 2023
Collection of Rewrite Recipes pertaining to the JHipster web application & microservice development platform

Apply JHipster best practices automatically What is this? This project implements a Rewrite module that applies best practices and migrations pertaini

OpenRewrite 5 Mar 7, 2022