Skip to content

Krouter helps you transfer data more easily in compose(KRouter 可以帮助你简单构建compose路由)

Notifications You must be signed in to change notification settings

ckenergy/ComposeKRouter

Repository files navigation

ComposeKRouter is a component router

中文文档

how to use (last_version = "1.0.7")

1、add dependencies

root build.gradle

dependencies {
    classpath  "io.github.ckenergy:KRoutePlugin:$KRouter_version"
}

app build.gradle

plugins {
    id 'KRoute-plugin'
}

implementation("io.github.ckenergy:kRouter-core:$KRouter_version")

add in module build.gradle where use @KRouter

ksp {
    arg("KROUTER_PACKAGE", "you module namespace")
}
ksp("io.github.ckenergy:kRouter-compiler:$KRouter_version")

2、add @KRouter in @Composable function(no need add compose in NavHost)

@KRouter(ComposeRouterMapper.Second)
@Composable
fun SecondPage(int: Int, string: String? = "default", testBean: TestBean, vararg long: Long)

Note: The parameters on the function can only put simple data (primitives , string and beans, because this piece is serialized by Gson)

The code above is equal to the original way of registering routes

NavHost() {
    composable(ComposeRouterMapper.Second) {
        //... get parameters
        SecondPage(....)
    }
}

3、add in NavHost

NavHost() {
    NavGraphManager.initKRouter(context, this, navController) {
        notFindPage = { route: String -> NotFindPage(route) }
    }
}

4、put data when navigate

controller.navigateRoute(ComposeRouterMapper.Second) {
    put("int", 1)//key的名字要和SecondPage函数参数名要保持一致一一对应
    put("long", longArrayOf(1L, 3L))
    put("testBean", TestBean(1.5f, false))
}

5、Custom registration route composeModules

When your compose page parameters are more complex, such as parameter is function

@Composable
fun OtherPage(start: () -> Unit)

At this time, you need to customize your own to register the route

val navGraph = composeModules { controller ->
    packageName = "com.ckenergy.compose.other"
    composable(ComposeRouterMapper.Other) {
        OtherPage {
            controller.navigateRoute(ComposeRouterMapper.Second)
        }
    }
}

NavGraphManager.initKRouter(context, this, navController) {
    notFindPage = { route: String -> NotFindPage(route) }
    isAnimation = true
    isDebug = BuildConfig.DEBUG
    addModules(navGraph)//add route map
}

About

Krouter helps you transfer data more easily in compose(KRouter 可以帮助你简单构建compose路由)

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published