Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.

Latest commit

 

History

History
34 lines (24 loc) · 977 Bytes

simple-syntax.md

File metadata and controls

34 lines (24 loc) · 977 Bytes

Simple syntax

class MyViewModel: ContainerHost<MyState, MySideEffect>, ViewModel() {

    override val container = container<MyState, MySideEffect>(MyState())

    fun loadDataForId(id: Int) = intent {
        postSideEffect(MySideEffect.Toast("Loading data for $id!"))

        val result = repository.loadData(id)

        reduce {
            state.copy(data = result)
        }
    }
}

This syntax integrates with the coroutine framework to bring you something that you will be very comfortable with if you already use coroutines for your project. On the other hand, it's slightly easier to make mistakes if you don't know how to use coroutines effectively. For example, blocking code in intent can block your Container.

Pros:

  • Extremely light and flexible
  • Your MVI logic executes in a suspend function
  • Interoperability with e.g. RxJava achieved through standard Kotlin libraries

Cons:

  • Your code has to conform to coroutine best practices