Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/artemmey/compose-jb-routing
Easiest routing for compose-jb
https://github.com/artemmey/compose-jb-routing
compose compose-jb kotlin kotlin-android kotlin-library kotlin-multiplatform router routing
Last synced: 8 days ago
JSON representation
Easiest routing for compose-jb
- Host: GitHub
- URL: https://github.com/artemmey/compose-jb-routing
- Owner: ArTemmey
- License: apache-2.0
- Created: 2021-09-19T04:22:45.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2022-02-10T18:58:15.000Z (almost 3 years ago)
- Last Synced: 2023-07-28T10:08:31.168Z (over 1 year ago)
- Topics: compose, compose-jb, kotlin, kotlin-android, kotlin-library, kotlin-multiplatform, router, routing
- Language: Kotlin
- Homepage:
- Size: 56.6 KB
- Stars: 34
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
**Easiest routing for compose-jb**
Supported targets:
- Jvm
- Js## Installation
```kotlin
repositories {
mavenCentral()
}dependencies {
implementation("io.github.artemmey:compose-jb-routing:0.9.5")
}
```## Usage
1. Declare your app routes:
```kotlin
object MyAppRoute {
const val Home = "/"
const val Articles = "/articles"// {id} is route param
const val Article = "$Articles/{id}"
}
```2. Configure routing
```kotlin
@Composable
fun App() {
initRouting(MyAppRoute.Home)
Router {
// `exact = true` means that current location matches exactly the given route.
route(MyAppRoute.Home, exact = true) { Home() }
// Default value of `exact` (false) means that current location can be any of those that starts with the given route. In this example location matches "/articles.*"
route(MyAppRoute.Articles) { Articles() }
}
}@Composable
fun Articles() {
// Nested routing
Router {
route(MyAppRoute.Articles, exact = true) { ArticleList() }
// Obtaining {id} param
route(MyAppRoute.Article) { Article(param("id")) }
}
}
```3. Perform routing
```kotlin
@Composable
fun ArticleList() {
articles.forEach {
ArticlePreview(onClick = {
// Navigate to next location and add current to the back stack
routing.push(MyAppRoute.Article, "id" to it.id)// Here we can see the back stack
println(routing.history)
})
}
}@Composable
fun Article(id: String) {
BackButton(onClick = {
// Navigate to prev location
routing.pop()
})
}
```4. Make redirects
```kotlin
@Composable
fun PrivateRoute() {
if (!auth) {
// Redirecting to login and setting current location as ref
Redirect("${MyAppRoute.Login}?ref=${routing.location}")
return
}
}@Composable
fun Login() {
LoginButton(onClick = {
performLogin()
// If have ref - redirecting back to it, else - just navigate back
routing.location.query()["ref"]
?.let { routing.redirect(it) }
?: routing.pop()
})
}
```