Coder Social home page Coder Social logo

typednavigation's Introduction

TypedNavigation

Run detekt and build

A lightweight library to help you navigate in compose with well typed functions.

Installation:

You can add this library to your project by just adding the following code to your root build.gradle

allprojects {
    repositories {
        // ...
        maven { url 'https://jitpack.io' }
    }
}

Then import the library in your app build.gradle file.

implementation 'com.github.xmartlabs:TypedNavigation:0.0.4'

Usage:

You just have to define your screens and the arguments they receive:

object Router {
    val default = TypedNavigation.E("default")
    val sample = TypedNavigation.A3("sample", NavType.StringType, NavType.StringType, NavType.StringType)
}

And after that the library will provide you with the following functions:

To add your screen to the NavHost:

setContent {
    val navigationController: NavHostController = rememberNavController()
    NavHost(navController = navigationController, startDestination = Router.default.url) {
        composable(Router.default) {
            Default(navigationController = navigationController)
        }
        composable(Router.example) { a: String?, b: String?, c: String? ->
            Example(a, b, c)
        }
    }
}

To navigate from one screen to another:

navigationController.navigate(Router.example.route("a", "b", "c"))

Add deep linking to your screen by setting up the correct path to the url:

   val example =
    TypedNavigation.A3("example", NavType.StringType, NavType.StringType, NavType.StringType,
        listOf { a1, a2, a3 -> // a1, a2 and a3 contains the keys for the attributes previously defined
            "www.example.com/$a1/$a2/$a3" 
        }
    )

Use it with hilt ViewModel

You can access attributes stored in the SavedStateHandle by using withAttributes

@HiltViewModel
class HiltExampleViewModel @Inject constructor(
    savedStateHandle: SavedStateHandle
) : ViewModel() {
    data class ScreenState(val name: String? = null, val age: Int? = null, val knowsHilt: Boolean? = null)

    val stateFlow: MutableStateFlow<ScreenState> = MutableStateFlow(ScreenState())

    init {
        Router.hiltExample.withAttributes(savedStateHandle) { name, age, knowsHilt ->
            viewModelScope.launch {
                stateFlow.emit(ScreenState(name!!, age, knowsHilt))
            }
        }
    }
}

For more examples you can check out our example app.

About

Made with ❤️ by XMARTLABS

typednavigation's People

Contributors

f7deleon avatar mirland 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.