nhaarman / acorn

Mastering Android navigation :chipmunk:
https://nhaarman.github.io/acorn
Apache License 2.0
181 stars 7 forks source link

Integration with unidirectional/mvi architecture #157

Open lgtout opened 4 years ago

lgtout commented 4 years ago

@nhaarman Do you have ideas about how one might use Acorn together with unidirectional/mvi architecture in the same app? Thanks in advance.

nhaarman commented 4 years ago

Could you give an example of what you mean by an unidirectional/mvi architecture? I'm unfamiliar with it.

lgtout commented 4 years ago

Thanks @nhaarman. A bunch of Android presentation layer frameworks that support unidirectional/mvi architecture have sprung up over the last couple of years. I think this may be the post that kicked it all off. They're all based on some interpretation of Flux/Redux from web-land. It conceives of state as immutable and models the app (or parts of it) as a state-machine. In functional programming terms, I think it's an interpretation and elaboration of the State monad. Some Android implementations are Mobius from Spotify and MvRx from AirBnB.

manueldidonna commented 4 years ago

@lgtout I used MvRx before switching from fragments to Acorn. That library can't be adapted to Acorn components because it's strictly coupled to fragments and viewmodels.

Anyway you can do something similar


// You can easily pass a mock container for testing purpose
interface MyContainer : Container {
    val events: Flow<UIEvent>
    fun render(state: State)
}

// scene can acts like a viewmodel, it persists across configuration changes and can saves its state
class MyScene : Scene<MyContainer> {
    fun onStart() {
        // I created some utilities around coroutines flow. 
        // Try to see RxScene within acorn extension artifacts
        whenViewAvailable { it.events }
            .combineWithLatestView()
            .onEach { (event, view) ->  view.render(**new state based on ui events**) }
            .launchIn(sceneScope) 
    }
}
lgtout commented 4 years ago

@manueldidonna Thank you!

manueldidonna commented 4 years ago

@lgtout your welcome! I'm glad to help you