Giter Club home page Giter Club logo

compose-navigation-reimagined's Introduction

A small and simple, yet fully fledged and customizable navigation library for Jetpack Compose:

  • Full type-safety
  • Built-in state restoration
  • Nested navigation with independent backstacks
  • Own Lifecycle, ViewModelStore and SavedStateRegistry for every backstack entry
  • Animated transitions
  • Dialog and bottom sheet navigation
  • Scopes for easier ViewModel sharing
  • No builders, no obligatory superclasses for your composables

Quick start

Add a single dependency to your project:

implementation("dev.olshevski.navigation:reimagined:1.5.0")

Define a set of destinations. It is convenient to use a sealed class for this:

sealed class Destination : Parcelable {

    @Parcelize
    data object First : Destination()

    @Parcelize
    data class Second(val id: Int) : Destination()

    @Parcelize
    data class Third(val text: String) : Destination()

}

Create a composable with NavController, NavBackHandler and NavHost:

@Composable
fun NavHostScreen() {
    val navController = rememberNavController<Destination>(
        startDestination = Destination.First
    )

    NavBackHandler(navController)

    NavHost(navController) { destination ->
        when (destination) {
            is Destination.First -> Column {
                Text("First destination")
                Button(onClick = {
                    navController.navigate(Destination.Second(id = 42))
                }) {
                    Text("Open Second destination")
                }
            }

            is Destination.Second -> Column {
                Text("Second destination: ${destination.id}")
                Button(onClick = {
                    navController.navigate(Destination.Third(text = "Hello"))
                }) {
                    Text("Open Third destination")
                }
            }

            is Destination.Third -> {
                Text("Third destination: ${destination.text}")
            }
        }
    }
}

As you can see, NavController is used for switching between destinations, NavBackHandler handles back presses and NavHost provides a composable corresponding to the last destination in the backstack. As simple as that.

What about animations?

Just replace NavHost with AnimatedNavHost. The default transition between destinations is a simple crossfade, but you can customize each transition with the transitionSpec parameter:

AnimatedNavHost(
    controller = navController,
    transitionSpec = { action, _, _ ->
        val direction = if (action == NavAction.Pop) {
            AnimatedContentTransitionScope.SlideDirection.End
        } else {
            AnimatedContentTransitionScope.SlideDirection.Start
        }
        slideIntoContainer(direction) togetherWith slideOutOfContainer(direction)
    }
) { destination ->
    // ...
}

Documentation

Full documentation is available here.

Additional dependencies

Library-specific hiltViewModel() implementation:

implementation("dev.olshevski.navigation:reimagined-hilt:<latest-version>")

BottomSheetNavHost implementation:

// if you are using Material
implementation("dev.olshevski.navigation:reimagined-material:<latest-version>")

// if you are using Material 3
implementation("dev.olshevski.navigation:reimagined-material3:<latest-version>")

Sample

Explore the sample. It demonstrates:

  • passing values and returning results
  • animated transitions
  • dialog and bottom sheet navigation
  • nested navigation
  • BottomNavigation integration
  • entry-scoped and shared ViewModels
  • hoisting of NavController to the ViewModel layer
  • deeplinks

About

I've been thinking about Android app architecture and navigation in particular for the longest time. After being introduced to Compose I could finally create the navigation structure that satisfies all my needs perfectly.

I hope it can help you as well.

If you like this library and find it useful, please star the project and share it with your fellow developers. You can also buy me a coffee:

Buy Me A Coffee

compose-navigation-reimagined's People

Contributors

olshevski avatar lucchetto avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.