Code Monkey home page Code Monkey logo

mill's Introduction

Mill

Build Maven Central GitHub license

Mill is a tool for compose multiplatform developers to easily manage the UI states.

Setup

Adding Dependency

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")
            }
        }
    }
}

Overview

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:

  • ViewStateStore: The place to store the UI states, provide an interface to pass the UI actions to the reducer, also provide an effect subscription
  • Reducer: This is the place you can put the core logic in, it will process the pass-in UI action with the current state and produce a new state to store
  • ViewState: Represents the UI state that UI view will show different element accordingly.
  • Action: Any actions that comes from UI view, like click, input
  • Effect: The stuff which are not related to UI state, such as navigation event, dialog pops

Usage

UI State Management

  1. Provides the 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
}
  1. Create the reducer by implements the interface or call the convenience function:
    1. Implement interface
    class CounterReducer: Reducer<Unit, Int, Unit> {
        override suspend fun reduce(action: Unit, currentState: Int, onEffect: (Unit) -> Unit) {
            return currentState + 1
        }
    }
    1. Create from the convenience function
    val counterReducer = createReducer {_, current, _ ->
        current + 1
    }
  2. Create a ViewStateStore by using rememberStore function, it will need you to pass 3 params:
  • Name
  • Reducer
  • function to initiate the state
val store = rememberStore(
   name = "counter-store",
   counterReducer,
) { 0 }
  1. Subscribe the state in UI composable
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  ")
    }
}

Navigation

Mill has a built-in navigation component named router, it is pretty much the same as android's navigation compose. To use it:

  1. Create a route controller
val controller = rememberRouteControler()
  1. Create the route graph with RouteHost
RouteHost(
    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()
        })
    }
}

License

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://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

mill's People

Contributors

zegjoker avatar

Stargazers

TestFile avatar  avatar

Watchers

 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.