Code Monkey home page Code Monkey logo

quickmvi's Introduction

QuickMVI

Small piece of code store class with necessary builders to provide some architecture pattern to manage better state changes for kotlin multiplatform and compose based projects or android (check ViewModel wrapper in cheat day app.

Quick start

I'm not going to publish it anywhere now, if you want to use it, copy to your project, just mvi.kt file.

  1. Create store, you, pass default state as argument and scope.
  2. Declare intent action as below, in reducer mutate your current state to get new one and update UI.
  3. In sideEffect action you can declare action which will be fired on every triggered update.
class TickerCounterStore(scope: CoroutineScope) : Store3<Int>(scope, 0) {

    fun bumpCounter() = intent<Int> {

        reducer { state.inc() }

        sideEffect { println("Bumped to $state") }
    }
}
  1. Create your Composable method which takes store as argument
  2. Collect state with collectState extension, you can pass optional initial action
  3. Build your UI basing on state.
@Composable
fun TickerCounter(store: TickerCounterStore) {

    val state: Int by store.collectState {
        // initial action
    }

    Button(
        modifier = Modifier.padding(16.dp),
        onClick = { store.bumpCounter() }) {
        Text("$state")
    }
}

Intent action

In onTrigger method you can define action, which will be executed after calling your intent method. It should always return flow.

Method reducer as mentioned above contains mutations of your state, called every time when onTrigger emit any value, if onTrigger is not set, reducer and sideEffect action will be called only once.

In below example, action declared in sideEffect is called every time when onTrigger flow emit value. Result of calling below code, will make state change from current to 3 in every second until reaching 3. When 2 and 3 values will be emitted, sideEffect action will print value in console.

    fun runAction() = intent<Int>(id = "id") {
        onTrigger {
            flow {
                delay(1000)
                emit(1)
                delay(1000)
                emit(2)
                delay(1000)
                emit(3)
            }
        }
    
        reducer { state = resultNonNull() }

        sideEffect { 
            if (result == 2 || result == 3) {
                println("Value: $state")
            }
        }
    }

Parameter id is empty by default. If it is not empty, triggered action will be remembered and if next action with same id will be fired and previous one is still pending, it will be killed.

You can also cancel actions with method fun cancel(vararg ids: String) as below:

    fun cancel() = sideEffect {
        cancel(
            INTENT_SLOW_TIMER_ID,
            INTENT_MEDIUM_TIMER_ID,
            INTENT_QUICK_TIMER_ID
        )
    }

Store Delegate

Logic inside intent could be extracted into separate classes and reuse in more than one store.

  1. Declare common interface for delegated methods:

    interface TestDelegate {
    
        fun doThings()
    }
  2. Implement new interface and open class StoreDelegate:

     class TestDelegateImpl : StoreDelegate<Int>(), TestDelegate {
     
         override fun doThings() = intent<Int> {
             onTrigger { flowOf(1, 2, 3) }
     
             reducer { state * 10 + resultNonNull() }
         }
     }
  3. Make your store implement your interface and implement methods by delegation in your store. Using delegate extension install delegate in store

     class TestStore(
         scope: CoroutineScope,
         defaultState: Int,
         testDelegate: TestDelegate
     ) : Store4Impl<Int>(scope, defaultState), TestDelegate by testDelegate {
    
         init {
             delegates(testDelegate)
         }
     }
    
     // ...
      val store = TestStore(scope, 0, TestDelegateImpl())
    
      store.doThings()

For more use cases check:

quickmvi's People

Contributors

mariuszmarzec avatar

Watchers

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