mill
0.1.0indexedFacilitates easy management of UI states for developers by providing components like ViewStateStore, Reducer, and built-in navigation. Implements concepts similar to Redux.
Facilitates easy management of UI states for developers by providing components like ViewStateStore, Reducer, and built-in navigation. Implements concepts similar to Redux.
Mill is a tool for compose multiplatform developers to easily manage the UI states.
Before using mill, adding the dependency is needed
kotlin {
sourceSets {
val commonMain by getting {
dependencies {
implementation("io.github.zegjoker:mill-core:$version")
implementation("io.github.zegjoker:mill-router:$version")
}
}
}
}
If you only want to use the part for UI state management, you can only core to the dependencies block
kotlin {
sourceSets {
val commonMain by getting {
dependencies {
implementation("io.github.zegjoker:mill-core:$version")
}
}
}
}
If you are a fullstack developer, this will be easiest part for you because mill uses the almost the same strategy as Redux. If you are new to Redux or mill, don't worry, the following guide will teach you steps by steps. There are 5 important elements/components in mill:
ViewStateStoreSaver at the root composable// Android
CompositionLocalProvider(
LocalViewStateStoreSaver provides ViewStateStoreSaver(viewModelStore) // viewModelStore comes from activity
) {
content() // Your UI content
}
// Other platforms
CompositionLocalProvider(LocalViewStateStoreSaver.provides(ViewStateStoreSaver())) {
content() // Your UI content
}
val store = rememberStore(
name = "counter-store",
counterReducer,
) { 0 }
val state by store.state.collectAsState()
Then you can use the state to show the UI.
Column(
modifier = modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text("Count: $state")
Spacer(modifier = Modifier.height(8.dp))
Button(onClick = {
store.dispatch(Unit)
}) {
Text(" + 1 ")
}
}
Mill has a built-in navigation component named router, it is pretty much the same as android's navigation compose. To use it:
val controller = rememberRouteControler()
RouteHostRouteHost(
controller = controller,
modifier = Modifier.fillMaxSize(),
startRoute = "start"
) {
route(path ="start") {
StartScreen(onNav = {
controller.navigateTo("second/12")
})
}
route(
path = "second/{param}",
params = listOf(
RouteParam("param", type = RouteParamType.Int)
)
) {
val param = LocalRouteContext.current.getIntParam("param") ?: 0
SecondScreen(param = param, onBack = {
controller.navigateUp()
})
}
}
Copyright 2023 ZegJoker.
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:
Unless applicable law agreed to writing, software
distributed under the License distributed an BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
See the License the specific language governing permissions
limitations under the License.
class CounterReducer: Reducer<Unit, Int, Unit> {
override suspend fun reduce(action: Unit, currentState: Int, onEffect: (Unit) -> Unit) {
return currentState + 1
}
}
val counterReducer = createReducer {_, current, _ ->
current + 1
}
ViewStateStore by using rememberStore function, it will need you to pass 3 params:Surfaced from shared tags and platforms — no rankings paid for.