Share
Embed
How to cook a well done MVI for Android (GDG DevFest Pisa 2019)
Reactive programming has firmly become one of the modern Android developer's tools. But most developers only use it in specific application parts concerning background operations. But what if we build an application solely on reactive patterns, what if we model our app as a single data stream? You've probably heard of Redux from the web world. It seems that per se this approach doesn't fit Android, however, we can still borrow something useful from it.
In this talk, we'll discuss how we can adapt Unidirectional Data Flow to modern Android development and how Kotlin and its key features can help us in that. We'll also take a look at examples of typical use cases implementation for this approach.
Video: https://youtu.be/Ls0uKLqNFz4
Sergey Ryabov
April 13, 2019
Transcript
205 slides
-
• Android Engineer, ex-Backend • 8 years of Android, 4
years of Kotlin • Kotlin User Group SPb • Android Academy SPb & Msk -
PROBLEMS OF A MODERN APP ▸ Big codebases ▸ Lots
of async interactions -
PROBLEMS OF A MODERN APP ▸ Big codebases ▸ Lots
of async interactions ▸ Size * Async => Something changes Somewhere and it all crapped up -
PROBLEMS OF A MODERN APP ▸ Big codebases ▸ Lots
of async interactions ▸ Size * Async => Something changes Somewhere and it all crapped up ▸ Pain In The Ass when looking for “where it all started to go wrong” -
COMMON STATE ▸ Single source of truth ▸ Easy to
check at any particular time -
COMMON STATE ▸ Single source of truth ▸ Easy to
check at any particular time ▸ Clear sequence of changes -
COMMON STATE ▸ Single source of truth ▸ Easy to
check at any particular time ▸ Clear sequence of changes ▸ Easy to find the cause of the change -
COMMON STATE ▸ Single source of truth ▸ Easy to
check at any particular time ▸ Clear sequence of changes ▸ Easy to find the cause of the change ▸ Easy decoupled testing -
REACTIVE STATE submitBtn.clicks() .doOnNext { submitBtn.isEnabled = false progressView.visibility =
VISIBLE } .flatMap { api.search(searchView.text.toString()) } .observeOn(uiScheduler) .doOnNext { progressView.visibility = GONE } .subscribe( { data -> showData(data) }, { submitBtn.isEnabled = true toast("Search failed") } ) -
REACTIVE STATE submitBtn.clicks() .doOnNext { submitBtn.isEnabled = false progressView.visibility =
VISIBLE } .flatMap { api.search(searchView.text.toString()) } .observeOn(uiScheduler) .doOnNext { progressView.visibility = GONE } .subscribe( { data -> showData(data) }, { submitBtn.isEnabled = true toast("Search failed") } ) -
REACTIVE STATE submitBtn.clicks() .doOnNext { submitBtn.isEnabled = false progressView.visibility =
VISIBLE } .flatMap { api.search(searchView.text.toString()) } .observeOn(uiScheduler) .doOnNext { progressView.visibility = GONE } .subscribe( { data -> showData(data) }, { submitBtn.isEnabled = true toast("Search failed") } ) -
REACTIVE STATE sealed class UiAction { class SearchAction(val query: String)
: UiAction() } sealed class UiState { object Loading : UiState() class Success(val data: Data) : UiState() class Failure(val error: Throwable) : UiState() } -
REACTIVE STATE submitBtn.clicks() .map { SearchAction(searchView.text.toString()) } .flatMap { action
-> api.search(action.query) .map<UiState> { result -> Success(result) } .onErrorReturn { e -> Failure(e) } .observeOn(uiScheduler) .startWith(Loading) }. .subscribe { state -> submitBtn.isEnabled = state !is Loading progressView.visibility = if (state is Loading) VISIBLE else GONE when (state) { is Success -> showData(state.data) is Failure -> toast("Search failed") }. }< -
REACTIVE STATE val actions = submitBtn.clicks() .map { SearchAction(searchView.text.toString()) }
actions.flatMap { action -> api.search(action.query) .map<UiState> { result -> Success(result) } .onErrorReturn { e -> Failure(e) } .observeOn(uiScheduler) .startWith(Loading) }. .subscribe { state -> submitBtn.isEnabled = state !is Loading progressView.visibility = if (state is Loading) VISIBLE else GONE when (state) { is Success -> showData(state.data) is Failure -> toast("Search failed") }. }< -
REACTIVE STATE val actions = submitBtn.clicks() .map { SearchAction(searchView.text.toString()) }
val states = actions.flatMap { action -> api.search(action.query) .map<UiState> { result -> Success(result) } .onErrorReturn { e -> Failure(e) } .observeOn(uiScheduler) .startWith(Loading) }. states.subscribe { state -> submitBtn.isEnabled = state !is Loading progressView.visibility = if (state is Loading) VISIBLE else GONE when (state) { is Success -> showData(state.data) is Failure -> toast("Search failed") }. }< -
REACTIVE STATE val actions = submitBtn.clicks() .map { SearchAction(searchView.text.toString()) }
val states = actions.flatMap { action -> api.search(action.query) .map<UiState> { result -> Success(result) } .onErrorReturn { e -> Failure(e) } .observeOn(uiScheduler) .startWith(Loading) }. states.subscribe(::render) private fun render(state: UiState) { submitBtn.isEnabled = state !is Loading progressView.visibility = if (state is Loading) VISIBLE else GONE when (state) { is Success -> showData(state.data) is Failure -> toast("Search failed") }. -
REACTIVE STATE class SearchComponent(private val api: Api, val uiScheduler: Scheduler)
{ fun bind(actions: Observable<SearchAction>): Observable<UiState> { return actions.flatMap { action -> api.search(action.query) .map<UiState> { result -> Success(result) } .onErrorReturn { e -> Failure(e) } .observeOn(uiScheduler) .startWith(Loading) }< }< }< -
REACTIVE STATE class SearchComponent(private val api: Api, val uiScheduler: Scheduler)
{ fun bind(actions: Observable<SearchAction>): Observable<UiState> { return actions.flatMap { action -> api.search(action.query) .map<UiState> { result -> Success(result) } .onErrorReturn { e -> Failure(e) } .observeOn(uiScheduler) .startWith(Loading) }< }< }< // View part searchComponent.bind(actions).subscribe(::render) -
What if we have more complex logic? How about two
network calls? -
sealed class UiAction : Action { class SearchAction(val query: String)
: UiAction() class LoadSuggestionsAction(val query: String) : UiAction() } -
sealed class UiState { object Loading : UiState() class Success(val
data: Data) : UiState() class Failure(val error: Throwable) : UiState() } -
class UiState( val loading: Boolean = false, val data: String?
= null, val error: Throwable? = null, val suggestions: List<String>? = null ) -
sealed class InternalAction : Action { object SearchLoadingAction : InternalAction()
class SearchSuccessAction(val data: String) : InternalAction() class SearchFailureAction(val error: Throwable) : InternalAction() class SuggestionsLoadedAction(val suggestions: List<String>) : InternalAction() } -
sealed class InternalAction : Action { object SearchLoadingAction : InternalAction()
class SearchSuccessAction(val data: String) : InternalAction() class SearchFailureAction(val error: Throwable) : InternalAction() class SuggestionsLoadedAction(val suggestions: List<String>) : InternalAction() } -
fun bind(actions: Observable<Action>): Observable<UiState> { ... } .ofType<ActionA> .publish Observable<Action>
Observable<Result> .ofType<ActionB> .merge … … -
fun bind(actions: Observable<Action>): Observable<UiState> { return actions.publish { shared ->
Observable.merge<Action>() } } .ofType<ActionA> .publish Observable<Action> Observable<Result> .ofType<ActionB> .merge … … -
fun bind(actions: Observable<Action>): Observable<UiState> { return actions.publish { shared ->
Observable.merge<Action>( bind(shared.ofType<SearchAction>()), bind(shared.ofType<LoadSuggestionsAction>())) } } .ofType<ActionA> .publish Observable<Action> Observable<Result> .ofType<ActionB> .merge … … -
fun bind(actions: Observable<SearchAction>): Observable<InternalAction> { return actions.flatMap { action ->
api.search(action.query) .map { result -> SearchSuccessAction(result) } .onErrorReturn { e -> SearchFailureAction(e) } .observeOn(uiScheduler) .startWith(SearchLoadingAction) } } fun bind(actions: Observable<LoadSuggestionsAction>): Observable<InternalAction> { return actions.flatMap { action -> api.suggestions(action.query) .onErrorReturnItem(emptyList()) .map { result -> SuggestionsLoadedAction(result) } .observeOn(uiScheduler) } } -
fun bind(actions: Observable<SearchAction>): Observable<InternalAction> { return actions.flatMap { action ->
api.search(action.query) .map { result -> SearchSuccessAction(result) } .onErrorReturn { e -> SearchFailureAction(e) } .observeOn(uiScheduler) .startWith(SearchLoadingAction) } } fun bind(actions: Observable<LoadSuggestionsAction>): Observable<InternalAction> { return actions.flatMap { action -> api.suggestions(action.query) .onErrorReturnItem(emptyList()) .map { result -> SuggestionsLoadedAction(result) } .observeOn(uiScheduler) } } -
fun bind(actions: Observable<Action>): Observable<UiState> { return actions.publish { shared ->
Observable.merge<Action>( bind(shared.ofType<SearchAction>()), bind(shared.ofType<LoadSuggestionsAction>())) } } -
fun bind(actions: Observable<Action>): Observable<UiState> { return actions.publish { shared ->
Observable.merge<Action>( bind(shared.ofType<SearchAction>()), bind(shared.ofType<LoadSuggestionsAction>())) } .scan(UiState()) { state, action -> ... } } -
bind(shared.ofType<LoadSuggestionsAction>())) } .scan(UiState()) { state, action -> when (action) {
SearchLoadingAction -> state.copy( loading = true, error = null, suggestions = null) is SearchSuccessAction -> state.copy( loading = false, data = newData, error = null, suggestions = null) is SearchFailureAction -> state.copy( loading = false, error = action.error) is SuggestionsLoadedAction -> state.copy( suggestions = action.suggestions) is SearchAction, is LoadSuggestionsAction -> state } } } -
bind(shared.ofType<LoadSuggestionsAction>())) } .scan(UiState()) { state, action -> when (action) {
SearchLoadingAction -> state.copy( loading = true, error = null, suggestions = null) is SearchSuccessAction -> state.copy( loading = false, data = newData, error = null, suggestions = null) is SearchFailureAction -> state.copy( loading = false, error = action.error) is SuggestionsLoadedAction -> state.copy( suggestions = action.suggestions) is SearchAction, is LoadSuggestionsAction -> state } } } -
bind(shared.ofType<LoadSuggestionsAction>())) } .scan(UiState()) { state, action -> when (action) {
SearchLoadingAction -> state.copy( loading = true, error = null, suggestions = null) is SearchSuccessAction -> state.copy( loading = false, data = newData, error = null, suggestions = null) is SearchFailureAction -> state.copy( loading = false, error = action.error) is SuggestionsLoadedAction -> state.copy( suggestions = action.suggestions) is SearchAction, is LoadSuggestionsAction -> state } } } -
bind(shared.ofType<LoadSuggestionsAction>())) } .scan(UiState()) { state, action -> when (action) {
SearchLoadingAction -> state.copy( loading = true, error = null, suggestions = null) is SearchSuccessAction -> state.copy( loading = false, data = newData, error = null, suggestions = null) is SearchFailureAction -> state.copy( loading = false, error = action.error) is SuggestionsLoadedAction -> state.copy( suggestions = action.suggestions) is SearchAction, is LoadSuggestionsAction -> state } } } -
bind(shared.ofType<LoadSuggestionsAction>())) } .scan(UiState()) { state, action -> when (action) {
SearchLoadingAction -> state.copy( loading = true, error = null, suggestions = null) is SearchSuccessAction -> state.copy( loading = false, data = newData, error = null, suggestions = null) is SearchFailureAction -> state.copy( loading = false, error = action.error) is SuggestionsLoadedAction -> state.copy( suggestions = action.suggestions) is SearchAction, is LoadSuggestionsAction -> state } } } -
bind(shared.ofType<LoadSuggestionsAction>())) } .scan(UiState()) { state, action -> when (action) {
SearchLoadingAction -> state.copy( loading = true, error = null, suggestions = null) is SearchSuccessAction -> state.copy( loading = false, data = newData, error = null, suggestions = null) is SearchFailureAction -> state.copy( loading = false, error = action.error) is SuggestionsLoadedAction -> state.copy( suggestions = action.suggestions) is SearchAction, is LoadSuggestionsAction -> state } } } -
REACTIVE STATE class SearchComponent(private val api: Api, private val uiScheduler:
Scheduler) { fun bind(actions: Observable<Action>): Observable<UiState> { return actions.publish { shared -> Observable.merge<Action>( bind(shared.ofType<SearchAction>()), bind(shared.ofType<LoadSuggestionsAction>())) } .scan(UiState()) { state, action -> when (action) { SearchLoadingAction -> state.copy(...) is SearchSuccessAction -> state.copy(...) is SearchFailureAction -> state.copy(...) is SuggestionsLoadedAction -> state.copy(...) is SearchAction, is LoadSuggestionsAction -> state } } } } -
UNIDIRECTIONAL DATA FLOW ▸ Redux ▸ Cycle.js ▸ Flux ▸
Elm programming language -
ELM - REDUX ▸ Update -> Reducer ▸ Call ▸
Component -
ELM - REDUX ▸ Update -> Reducer ▸ Call ->
Middleware ▸ Component -
ELM - REDUX ▸ Update -> Reducer ▸ Call ->
Middleware ▸ Component -> Store -
REACTIVE STATE class SearchComponent(private val api: Api, private val uiScheduler:
Scheduler) { fun bind(actions: Observable<Action>): Observable<UiState> { return actions.publish { shared -> Observable.merge<Action>( bind(shared.ofType<SearchAction>()), bind(shared.ofType<LoadSuggestionsAction>())) } .scan(UiState()) { state, action -> when (action) { SearchLoadingAction -> state.copy(...) is SearchSuccessAction -> state.copy(...) is SearchFailureAction -> state.copy(...) is SuggestionsLoadedAction -> state.copy(...) is SearchAction, is LoadSuggestionsAction -> state } } } } -
REACTIVE STATE class SearchComponent(private val api: Api, private val uiScheduler:
Scheduler) { fun bind(actions: Observable<Action>): Observable<UiState> { return actions.publish { shared -> Observable.merge<Action>( bind(shared.ofType<SearchAction>()), bind(shared.ofType<LoadSuggestionsAction>())) } .scan(UiState()) { state, action -> when (action) { SearchLoadingAction -> state.copy(...) is SearchSuccessAction -> state.copy(...) is SearchFailureAction -> state.copy(...) is SuggestionsLoadedAction -> state.copy(...) is SearchAction, is LoadSuggestionsAction -> state } } } } Reducer -
REACTIVE STATE class SearchComponent(private val api: Api, private val uiScheduler:
Scheduler) { fun bind(actions: Observable<Action>): Observable<UiState> { return actions.publish { shared -> Observable.merge<Action>( bind(shared.ofType<SearchAction>()), bind(shared.ofType<LoadSuggestionsAction>())) } .scan(UiState()) { state, action -> when (action) { SearchLoadingAction -> state.copy(...) is SearchSuccessAction -> state.copy(...) is SearchFailureAction -> state.copy(...) is SuggestionsLoadedAction -> state.copy(...) is SearchAction, is LoadSuggestionsAction -> state } } } } Reducer Middleware -
REACTIVE STATE class SearchComponent(private val api: Api, private val uiScheduler:
Scheduler) { fun bind(actions: Observable<Action>): Observable<UiState> { return actions.publish { shared -> Observable.merge<Action>( bind(shared.ofType<SearchAction>()), bind(shared.ofType<LoadSuggestionsAction>())) } .scan(UiState()) { state, action -> when (action) { SearchLoadingAction -> state.copy(...) is SearchSuccessAction -> state.copy(...) is SearchFailureAction -> state.copy(...) is SuggestionsLoadedAction -> state.copy(...) is SearchAction, is LoadSuggestionsAction -> state } } } } Reducer Middleware Store -
sealed class InternalAction : Action { object SearchLoadingAction : InternalAction()
class SearchSuccessAction(val data: String) : InternalAction() class SearchFailureAction(val error: Throwable) : InternalAction() class SuggestionsLoadedAction(val suggestions: List<String>) : InternalAction() } -
REDUCER class SearchReducer : Reducer<UiState, Action> { override fun reduce(state:
UiState, action: Action): UiState { return when (action) { SearchLoadingAction -> state.copy(...) is SearchSuccessAction -> state.copy(...) is SearchFailureAction -> state.copy(...) is SuggestionsLoadedAction -> state.copy(...) is SearchAction, is LoadSuggestionsAction -> state } } } -
MIDDLEWARE class SearchMiddleware(val api: Api) : Middleware<Action, UiState> { override
fun bind(actions: Observable<Action>): Observable<Action> { return actions.ofType<SearchAction>() .flatMap { action -> api.search(action.query) .map<InternalAction> { result -> SearchSuccessAction(result) } .onErrorReturn { e -> SearchFailureAction(e) } .startWith(SearchLoadingAction) } } } -
REACTIVE STATE class SearchMiddleware(val api: Api) : Middleware<Action, UiState> {
override fun bind(actions: Observable<Action>, state: Observable<UiState>) : Observable<Action> { return actions.ofType<SearchAction>() .withLatestFrom(state) { action, currentState -> action to currentState } .flatMap { (action, state) -> api.search(action.query) .map<InternalAction> { result -> SearchSuccessAction(result) } .onErrorReturn { e -> SearchFailureAction(e) } .startWith(SearchLoadingAction) } } } -
REACTIVE STATE class SearchMiddleware(val api: Api) : Middleware<Action, UiState> {
override fun bind(actions: Observable<Action>, state: Observable<UiState>) : Observable<Action> { return actions.ofType<SearchAction>() .withLatestFrom(state) { action, currentState -> action to currentState } .flatMap { (action, state) -> api.search(action.query) .map<InternalAction> { result -> SearchSuccessAction(result) } .onErrorReturn { e -> SearchFailureAction(e) } .startWith(SearchLoadingAction) } } } -
STORE - SEARCH COMPONENT private val state = BehaviorRelay.createDefault<UiState>(UiState()) private
val actions = PublishRelay.create<Action>() -
STORE - SEARCH COMPONENT private val state = BehaviorRelay.createDefault<UiState>(UiState()) private
val actions = PublishRelay.create<Action>() fun wire(): Disposable { val disposable = CompositeDisposable() disposable += actions .withLatestFrom(state) { action, state -> SearchReducer().reduce(state, action) }. .subscribe(state::accept) return disposable }. -
STORE - SEARCH COMPONENT private val state = BehaviorRelay.createDefault<UiState>(UiState()) private
val actions = PublishRelay.create<Action>() fun wire(): Disposable { val disposable = CompositeDisposable() disposable += actions .withLatestFrom(state) { action, state -> SearchReducer().reduce(state, action) }. .distinctUntilChanged() .subscribe(state::accept) return disposable }. -
STORE - SEARCH COMPONENT private val state = BehaviorRelay.createDefault<UiState>(UiState()) private
val actions = PublishRelay.create<Action>() fun wire(): Disposable { val disposable = CompositeDisposable() disposable += actions .withLatestFrom(state) { action, state -> SearchReducer().reduce(state, action) }. .distinctUntilChanged() .subscribe(state::accept) disposable += Observable.merge<Action>( SearchMiddleware(api).bind(actions, state), SuggestionsMiddleware(api).bind(actions, state) ).subscribe(actions::accept) return disposable }. -
STORE - SEARCH COMPONENT private val state = BehaviorRelay.createDefault<UiState>(UiState()) private
val actions = PublishRelay.create<Action>() fun wire(): Disposable { val disposable = CompositeDisposable() disposable += actions .withLatestFrom(state) { action, state -> SearchReducer().reduce(state, action) } .distinctUntilChanged() .subscribe(state::accept) disposable += Observable.merge<Action>( SearchMiddleware(api).bind(actions, state), SuggestionsMiddleware(api).bind(actions, state) ).subscribe(actions::accept) return disposable } -
STORE fun bind(actions: Observable<Action>, render: (UiState) -> Unit): Disposable {
val disposable = CompositeDisposable() disposable += state.observeOn(uiScheduler).subscribe(render) disposable += actions.subscribe(actions::accept) return disposable } -
STORE fun bind(actions: Observable<Action>, render: (UiState) -> Unit): Disposable {
val disposable = CompositeDisposable() disposable += state.observeOn(uiScheduler).subscribe(render) disposable += actions.subscribe(actions::accept) return disposable }. -
STORE interface MviView<A, S> { val actions: Observable<A> fun render(state:
S) } // SearchComponent fun bind(actions: Observable<Action>, render: (UiState) -> Unit): Disposable { val disposable = CompositeDisposable() disposable += state.observeOn(uiScheduler).subscribe(render) disposable += actions.subscribe(actions::accept) return disposable }. -
STORE interface MviView<A, S> { val actions: Observable<A> fun render(state:
S) } // SearchComponent fun bind(view: MviView<Action, UiState>): Disposable { val disposable = CompositeDisposable() disposable += state.observeOn(uiScheduler).subscribe(view::render) disposable += view.actions.subscribe(actions::accept) return disposable } -
STORE interface MviView<A, S> { val actions: Observable<A> fun render(state:
S) } // SearchComponent fun bind(view: MviView<Action, UiState>): Disposable { val disposable = CompositeDisposable() disposable += state.observeOn(uiScheduler).subscribe(view::render) disposable += view.actions.subscribe(actions::accept) return disposable } -
STORE private val state = BehaviorRelay.createDefault<UiState>(UiState()) private val actions =
PublishRelay.create<Action>() fun wire(): Disposable { val disposable = CompositeDisposable() disposable += actions .withLatestFrom(state) { action, state -> SearchReducer().reduce(state, action) } .distinctUntilChanged() .subscribe(state::accept) disposable += Observable.merge<Action>( SearchMiddleware(api).bind(actions, state), SuggestionsMiddleware(api).bind(actions, state) ).subscribe(actions::accept) return disposable } -
STORE private val state = BehaviorRelay.createDefault<UiState>(UiState()) private val actions =
PublishRelay.create<Action>() fun wire(): Disposable { val disposable = CompositeDisposable() disposable += actions .withLatestFrom(state) { action, state -> SearchReducer().reduce(state, action) } .distinctUntilChanged() .subscribe(state::accept) disposable += Observable.merge<Action>( SearchMiddleware(api).bind(actions, state), SuggestionsMiddleware(api).bind(actions, state) ).subscribe(actions::accept) return disposable } -
STORE private val state = BehaviorRelay.createDefault<UiState>(UiState()) private val actions =
PublishRelay.create<Action>() fun wire(): Disposable { val disposable = CompositeDisposable() disposable += actions .withLatestFrom(state) { action, state -> SearchReducer().reduce(state, action) } .distinctUntilChanged() .subscribe(state::accept) disposable += Observable.merge<Action>( SearchMiddleware(api).bind(actions, state), SuggestionsMiddleware(api).bind(actions, state) ).subscribe(actions::accept) return disposable } -
STORE private val state = BehaviorRelay.createDefault<UiState>(UiState()) private val actions =
PublishRelay.create<Action>() fun wire(): Disposable { val disposable = CompositeDisposable() disposable += actions .withLatestFrom(state) { action, state -> SearchReducer().reduce(state, action) } .distinctUntilChanged() .subscribe(state::accept) disposable += Observable.merge<Action>( SearchMiddleware(api).bind(actions, state), SuggestionsMiddleware(api).bind(actions, state) ).subscribe(actions::accept) return disposable } -
STORE private val state = BehaviorRelay.createDefault<UiState>( UiState()) private val actions
= PublishRelay.create<Action>() fun wire(): Disposable { val disposable = CompositeDisposable() disposable += actions .withLatestFrom(state) { action, state -> SearchReducer().reduce(state, action) } .distinctUntilChanged() .subscribe(state::accept) disposable += Observable.merge<Action>( SearchMiddleware(api).bind(actions, state), SuggestionsMiddleware(api).bind(actions, state) ).subscribe(actions::accept) return disposable } -
STORE private val state = BehaviorRelay.createDefault<UiState>( initialState) private val actions
= PublishRelay.create<Action>() fun wire(): Disposable { val disposable = CompositeDisposable() disposable += actions .withLatestFrom(state) { action, state -> reducer.reduce(state, action) } .distinctUntilChanged() .subscribe(state::accept) disposable += Observable.merge<Action>( middlewares.map { it.bind(actions, state) } ).subscribe(actions::accept) return disposable } -
STORE class SearchComponent( private val reducer: Reducer<UiState, Action>, private val
middlewares: List<Middleware<Action, UiState>>, private val initialState: UiState ) -
class Store<A, S>( private val reducer: Reducer<S, A>, private val
middlewares: List<Middleware<A, S>>, private val initialState: S ) STORE -
CORE interface MviView<A, S> { val actions: Observable<A> fun render(state:
S) } interface Reducer<S, A> { fun reduce(state: S, action: A): S } interface Middleware<A, S> { fun bind(actions: Observable<A>, state: Observable<S>): Observable<A> } class Store<A, S>( private val reducer: Reducer<S, A>, private val middlewares: List<Middleware<A, S>>, private val initialState: S ) { fun wire(): Disposable {} fun bind(view: MviView<Action, UiState>): Disposable {} } -
BIND IT ALL! class SearchViewModel<A, S> @Inject constructor (private val
store: Store<A, S>) : ViewModel() { private val wiring = store.wire() private var viewBinding: Disposable? = null override fun onCleared() { wiring.dispose() } fun bind(view: MviView<A, S>) { viewBinding = store.bind(view) } fun unbind() { viewBinding?.dispose() } } -
BIND IT ALL! class SearchViewModel<A, S> @Inject constructor (private val
store: Store<A, S>) : ViewModel() { private val wiring = store.wire() private var viewBinding: Disposable? = null override fun onCleared() { wiring.dispose() } fun bind(view: MviView<A, S>) { viewBinding = store.bind(view) } fun unbind() { viewBinding?.dispose() } } -
BIND IT ALL! class SearchViewModel<A, S> @Inject constructor (private val
store: Store<A, S>) : ViewModel() { private val wiring = store.wire() private var viewBinding: Disposable? = null override fun onCleared() { wiring.dispose() } fun bind(view: MviView<A, S>) { viewBinding = store.bind(view) } fun unbind() { viewBinding?.dispose() } } -
BIND IT ALL! class SearchViewModel<A, S> @Inject constructor (private val
store: Store<A, S>) : ViewModel() { private val wiring = store.wire() private var viewBinding: Disposable? = null override fun onCleared() { wiring.dispose() } fun bind(view: MviView<A, S>) { viewBinding = store.bind(view) } fun unbind() { viewBinding?.dispose() } } -
OUR UNIDIRECTIONAL DATA FLOW < , > SearchLoadingAction State.Empty ::render
Middleware Middleware Middleware Reducer State.Empty SearchAction -
OUR UNIDIRECTIONAL DATA FLOW < , > SearchLoadingAction State.Empty ::render
Middleware Middleware Middleware Reducer State.Empty SearchAction -
OUR UNIDIRECTIONAL DATA FLOW < , > SearchLoadingAction State.Empty ::render
Middleware Middleware Middleware Reducer State.Empty SearchAction -
OUR UNIDIRECTIONAL DATA FLOW < , > SearchLoadingAction ::render Middleware
Middleware Middleware Reducer State.Empty State(loading = true) -
OUR UNIDIRECTIONAL DATA FLOW < , > SearchLoadingAction ::render Middleware
Middleware Middleware Reducer State.Empty State(loading = true) -
OUR UNIDIRECTIONAL DATA FLOW < , > SearchLoadingAction State(loading =
true) SearchSuccessAction ::render Middleware Middleware Middleware Reducer -
OUR UNIDIRECTIONAL DATA FLOW < , > SearchLoadingAction State(loading =
true) SearchSuccessAction ::render Middleware Middleware Middleware Reducer -
OUR UNIDIRECTIONAL DATA FLOW < , > SearchLoadingAction State(loading =
true) SearchSuccessAction ::render Middleware Middleware Middleware Reducer -
OUR UNIDIRECTIONAL DATA FLOW < , > SearchSuccessAction ::render Middleware
Middleware Middleware Reducer State(loading = true) State(loading = false, data = newData) -
OUR UNIDIRECTIONAL DATA FLOW < , > SearchSuccessAction ::render Middleware
Middleware Middleware Reducer State(loading = true) State(loading = false, data = newData) -
ANY PROBLEMS WITH UI? ▸ Lots of State updates ▸
May cause lots of redundant UI rerendering -
ANY PROBLEMS WITH UI? ▸ Lots of State updates ▸
May cause lots of redundant UI rerendering ▸ … -
ANY PROBLEMS WITH UI? ▸ Lots of State updates ▸
May cause lots of redundant UI rerendering ▸ … ▸ I have libs for you! -
▸ Declarative ▸ Recycle all the things ▸ Async layout
▸ Same Android Widgets Litho Domic -
▸ Declarative ▸ Recycle all the things ▸ Async layout
▸ Same Android Widgets ▸ Diffing changes Litho Domic -
▸ Declarative ▸ Recycle all the things ▸ Async layout
▸ Same Android Widgets ▸ Diffing changes ▸ Reactive Litho Domic -
TESTING VIEW // In val observer = TestObserver.create<UiAction>() realView.actions.subscribe(observer) onView(withId(R.id.search_edit)).perform(typeText("Query"))
onView(withId(R.id.submit_btn)).perform(click()) observer.assertValue(SearchAction("Query")) -
TESTING VIEW // Out val uiState = UiState(loading = false,
data = "TestData") realView.render(uiState) takeScreenshot() -
TESTING LOGIC val viewModel = provide<SearchViewModel<Action, UiState>> viewModel.bind(fakeView) actions.onNext(SearchAction("Query")) states.assertValue(UiState(loading
= true)) viewModel.unbind() actions.onNext(SearchAction("AnotherQuery")) states.assertNoValues() -
DO YOU EVEN CLEAN? MviView Repository Store Reducer Middleware Middleware
Middleware Interactor? -
DO YOU EVEN CLEAN? MviView Repository Store Reducer Middleware Middleware
Middleware Presentation Layer Domain Layer Data Layer -
TO WRAP UP ▸ MVI is about presentation logic ▸
Reactive flow -
TO WRAP UP ▸ MVI is about presentation logic ▸
Reactive flow ▸ Sealed & data classes for Actions & States -
TO WRAP UP ▸ MVI is about presentation logic ▸
Reactive flow ▸ Sealed & data classes for Actions & States ▸ Kotlin multiplatform usage options -
TO WRAP UP ▸ MVI is about presentation logic ▸
Reactive flow ▸ Sealed & data classes for Actions & States ▸ Kotlin multiplatform usage options, but Rx -
TO WRAP UP ▸ MVI is about presentation logic ▸
Reactive flow ▸ Sealed & data classes for Actions & States ▸ Kotlin multiplatform usage options, but Rx ▸ RxKotlin -
TO WRAP UP ▸ MVI is about presentation logic ▸
Reactive flow ▸ Sealed & data classes for Actions & States ▸ Kotlin multiplatform usage options, but Rx ▸ RxKotlin Reaktive -
TO WRAP UP ▸ MVI is about presentation logic ▸
Reactive flow ▸ Sealed & data classes for Actions & States ▸ Kotlin multiplatform usage options, but Rx ▸ RxKotlin Reaktive ▸ kotlinx.coroutines.flow -
TO WRAP UP ▸ MVI is about presentation logic ▸
Reactive flow ▸ Sealed & data classes for Actions & States ▸ Kotlin multiplatform usage options, but Rx ▸ RxKotlin Reaktive ▸ kotlinx.coroutines.flow ▸ Time Travel Debugger -
UNIDIRECTIONAL DATA FLOW LIBS ▸ RxRedux / Freeletics github.com/freeletics/RxRedux ▸
Mobius / Spotify github.com/spotify/mobius -
UNIDIRECTIONAL DATA FLOW LIBS ▸ RxRedux / Freeletics github.com/freeletics/RxRedux ▸
Mobius / Spotify github.com/spotify/mobius ▸ MvRx / AirBnb github.com/airbnb/MvRx -
UNIDIRECTIONAL DATA FLOW LIBS ▸ RxRedux / Freeletics github.com/freeletics/RxRedux ▸
Mobius / Spotify github.com/spotify/mobius ▸ MvRx / AirBnb github.com/airbnb/MvRx ▸ MVICore / Badoo github.com/badoo/MVICore -
UNIDIRECTIONAL DATA FLOW LIBS ▸ RxRedux / Freeletics github.com/freeletics/RxRedux ▸
Mobius / Spotify github.com/spotify/mobius ▸ MvRx / AirBnb github.com/airbnb/MvRx ▸ MVICore / Badoo github.com/badoo/MVICore ▸ Grox / Groupon github.com/groupon/grox -
UNIDIRECTIONAL DATA FLOW LIBS ▸ RxRedux / Freeletics github.com/freeletics/RxRedux ▸
Mobius / Spotify github.com/spotify/mobius ▸ MvRx / AirBnb github.com/airbnb/MvRx ▸ MVICore / Badoo github.com/badoo/MVICore ▸ Grox / Groupon github.com/groupon/grox ▸ Suas / Zendesk github.com/zendesk/Suas-Android -
MVICORE ▸ Wish -> Action ▸ Effect -> Internal Action
▸ Actor ▸ Feature -
MVICORE ▸ Wish -> Action ▸ Effect -> Internal Action
▸ Actor -> Middleware ▸ Feature -
MVICORE ▸ Wish -> Action ▸ Effect -> Internal Action
▸ Actor -> Middleware ▸ Feature -> Store -
‣ Managing State with RxJava by Jake Wharton youtu.be/0IKHxjkgop4 ‣
The Reactive Workflow Pattern by Ray Ryan youtu.be/mvBVkU2mCF4 ‣ Litho: A Declarative UI Framework for Android by Lucas Rocha youtu.be/uzCK4Vnme7o ‣ Litho Project fblitho.com ‣ Domic — Reactive Virtual DOM by Artem Zinnatullin youtu.be/Ce6phlHfKR8 ‣ Domic repo github.com/lyft/domic ‣ kotlinx-coroutines-flow kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow ‣ Reaktive repo github.com/badoo/Reaktive ‣ Reagent repo github.com/JakeWharton/Reagent ‣ Sample project github.com/colriot/talk-well-done-mvi ‣ Intro image nationalpostcom.files.wordpress.com/2018/03/gettyimages-594465522.jpg LINKS -
How to cook a well done MVI for Android Sergey
Ryabov @colriot