Slide 1

Slide 1 text

How to cook a well done MVI for Android Sergey Ryabov

Slide 2

Slide 2 text

• Android Engineer & Mobile Consultant • Kotlin User Group SPb • Android Academy SPb & Msk • Bla-bla-bla • Digital Nomad

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

PROBLEMS OF A MODERN APP

Slide 5

Slide 5 text

PROBLEMS OF A MODERN APP ▸ Lots of asynchronicity: REST, WebSockets, Pushes, …

Slide 6

Slide 6 text

PROBLEMS OF A MODERN APP ▸ Lots of asynchronicity: REST, WebSockets, Pushes, … ▸ State updates from random places

Slide 7

Slide 7 text

PROBLEMS OF A MODERN APP ▸ Lots of asynchronicity: REST, WebSockets, Pushes, … ▸ State updates from random places ▸ Big size

Slide 8

Slide 8 text

PROBLEMS OF A MODERN APP ▸ Lots of asynchronicity: REST, WebSockets, Pushes, … ▸ State updates from random places ▸ Big size ▸ Async * Size => Something changes Somewhere and it all F***ed up

Slide 9

Slide 9 text

PROBLEMS OF A MODERN APP ▸ Lots of asynchronicity: REST, WebSockets, Pushes, … ▸ State updates from random places ▸ Big size ▸ Async * Size => Something changes Somewhere and it all F***ed up ▸ Pain In The Ass when looking for “where it all started to go wrong”

Slide 10

Slide 10 text

COMMON STATE

Slide 11

Slide 11 text

COMMON STATE ▸ Single source of truth

Slide 12

Slide 12 text

COMMON STATE ▸ Single source of truth ▸ Easy to check at any particular time

Slide 13

Slide 13 text

COMMON STATE ▸ Single source of truth ▸ Easy to check at any particular time ▸ Clear sequence of changes

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

COMMON STATE Benefits are clear, but the arch implementation is still hard

Slide 17

Slide 17 text

UNIDIRECTIONAL DATA FLOW

Slide 18

Slide 18 text

UNIDIRECTIONAL DATA FLOW view( model( intent() ) )

Slide 19

Slide 19 text

UNIDIRECTIONAL DATA FLOW render( state( actions() ) )

Slide 20

Slide 20 text

Доклад Вартанова

Slide 21

Slide 21 text

REACTIVE STATE Search query SUBMIT

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

clicks()

Slide 27

Slide 27 text

doOnNext()

Slide 28

Slide 28 text

searchView.text

Slide 29

Slide 29 text

doOnNext()

Slide 30

Slide 30 text

subscribe()

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

REACTIVE STATE submitBtn.clicks() .map { SearchAction(searchView.text.toString()) } .flatMap { action -> api.search(action.query) .map { 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") } }

Slide 34

Slide 34 text

REACTIVE STATE val actions = submitBtn.clicks() .map { SearchAction(searchView.text.toString()) } actions.flatMap { action -> api.search(action.query) .map { 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") } }

Slide 35

Slide 35 text

REACTIVE STATE val actions = submitBtn.clicks() .map { SearchAction(searchView.text.toString()) } val states = actions.flatMap { action -> api.search(action.query) .map { 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") } }

Slide 36

Slide 36 text

REACTIVE STATE val actions = submitBtn.clicks() .map { SearchAction(searchView.text.toString()) } val states = actions.flatMap { action -> api.search(action.query) .map { 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 -> finish() is Failure -> toast("Search failed") }

Slide 37

Slide 37 text

REACTIVE STATE class SearchComponent(private val api: Api, val uiScheduler: Scheduler) { fun bind(actions: Observable): Observable { return actions.flatMap { action -> api.search(action.query) .map { result -> Success(result) } .onErrorReturn { e -> Failure(e) } .observeOn(uiScheduler) .startWith(Loading) } } }

Slide 38

Slide 38 text

REACTIVE STATE class SearchComponent(private val api: Api, val uiScheduler: Scheduler) { fun bind(actions: Observable): Observable { return actions.flatMap { action -> api.search(action.query) .map { result -> Success(result) } .onErrorReturn { e -> Failure(e) } .observeOn(uiScheduler) .startWith(Loading) } } } // View part searchComponent.bind(actions).subscribe(::render)

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

SearchAction UiState

Slide 41

Slide 41 text

searchComponent.bind(actions).subscribe(::render)

Slide 42

Slide 42 text

render( state( actions() ) ) searchComponent.bind(actions).subscribe(::render)

Slide 43

Slide 43 text

What if we have more complex logic?

Slide 44

Slide 44 text

What if we have more complex logic? How about two network calls?

Slide 45

Slide 45 text

REACTIVE STATE Fanta| SUBMIT Fantastic Four Fantastic Beasts Final Fantasy

Slide 46

Slide 46 text

sealed class UiAction : Action { class SearchAction(val query: String) : UiAction() }

Slide 47

Slide 47 text

sealed class UiAction : Action { class SearchAction(val query: String) : UiAction() class LoadSuggestionsAction(val query: String) : UiAction() }

Slide 48

Slide 48 text

sealed class UiState { object Loading : UiState() class Success(val data: Data) : UiState() class Failure(val error: Throwable) : UiState() }

Slide 49

Slide 49 text

class UiState( val loading: Boolean = false, val data: String? = null, val error: Throwable? = null, val suggestions: List? = null )

Slide 50

Slide 50 text

sealed class InternalAction : Action { object SearchLoadingAction : InternalAction() class SearchSuccessAction(val data: String) : InternalAction() class SearchFailureAction(val error: Throwable) : InternalAction() class SuggestionsLoadedAction(val suggestions: List) : InternalAction() }

Slide 51

Slide 51 text

sealed class InternalAction : Action { object SearchLoadingAction : InternalAction() class SearchSuccessAction(val data: String) : InternalAction() class SearchFailureAction(val error: Throwable) : InternalAction() class SuggestionsLoadedAction(val suggestions: List) : InternalAction() }

Slide 52

Slide 52 text

fun bind(actions: Observable): Observable { ... }

Slide 53

Slide 53 text

fun bind(actions: Observable): Observable { ... } .ofType .publish Observable Observable .ofType .merge … …

Slide 54

Slide 54 text

fun bind(actions: Observable): Observable { return actions.publish { shared -> Observable.merge() } } .ofType .publish Observable Observable .ofType .merge … …

Slide 55

Slide 55 text

fun bind(actions: Observable): Observable { return actions.publish { shared -> Observable.merge( bind(shared.ofType()), bind(shared.ofType())) } } .ofType .publish Observable Observable .ofType .merge … …

Slide 56

Slide 56 text

fun bind(actions: Observable): Observable { return actions.flatMap { action -> api.search(action.query) .map { result -> SearchSuccessAction(result) } .onErrorReturn { e -> SearchFailureAction(e) } .observeOn(uiScheduler) .startWith(SearchLoadingAction) } } fun bind(actions: Observable): Observable { return actions.flatMap { action -> api.suggestions(action.query) .onErrorReturnItem(emptyList()) .map { result -> SuggestionsLoadedAction(result) } .observeOn(uiScheduler) } }

Slide 57

Slide 57 text

fun bind(actions: Observable): Observable { return actions.flatMap { action -> api.search(action.query) .map { result -> SearchSuccessAction(result) } .onErrorReturn { e -> SearchFailureAction(e) } .observeOn(uiScheduler) .startWith(SearchLoadingAction) } } fun bind(actions: Observable): Observable { return actions.flatMap { action -> api.suggestions(action.query) .onErrorReturnItem(emptyList()) .map { result -> SuggestionsLoadedAction(result) } .observeOn(uiScheduler) } }

Slide 58

Slide 58 text

fun bind(actions: Observable): Observable { return actions.publish { shared -> Observable.merge( bind(shared.ofType()), bind(shared.ofType())) } }

Slide 59

Slide 59 text

fun bind(actions: Observable): Observable { return actions.publish { shared -> Observable.merge( bind(shared.ofType()), bind(shared.ofType())) } .scan(UiState()) { state, action -> ... } }

Slide 60

Slide 60 text

bind(shared.ofType())) } .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 } } }

Slide 61

Slide 61 text

bind(shared.ofType())) } .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 } } }

Slide 62

Slide 62 text

bind(shared.ofType())) } .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 } } }

Slide 63

Slide 63 text

bind(shared.ofType())) } .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 } } }

Slide 64

Slide 64 text

bind(shared.ofType())) } .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 } } }

Slide 65

Slide 65 text

bind(shared.ofType())) } .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 } } }

Slide 66

Slide 66 text

REACTIVE STATE class SearchComponent(private val api: Api, private val uiScheduler: Scheduler) { fun bind(actions: Observable): Observable { return actions.publish { shared -> Observable.merge( bind(shared.ofType()), bind(shared.ofType())) } .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 } } } }

Slide 67

Slide 67 text

UNIDIRECTIONAL DATA FLOW

Slide 68

Slide 68 text

UNIDIRECTIONAL DATA FLOW ▸ Redux

Slide 69

Slide 69 text

UNIDIRECTIONAL DATA FLOW ▸ Redux ▸ Cycle.js

Slide 70

Slide 70 text

UNIDIRECTIONAL DATA FLOW ▸ Redux ▸ Cycle.js ▸ Flux

Slide 71

Slide 71 text

UNIDIRECTIONAL DATA FLOW ▸ Redux ▸ Cycle.js ▸ Flux ▸ Elm

Slide 72

Slide 72 text

ELM COMPONENT

Slide 73

Slide 73 text

ELM COMPONENT

Slide 74

Slide 74 text

ELM COMPONENT

Slide 75

Slide 75 text

ELM COMPONENT Update

Slide 76

Slide 76 text

ELM COMPONENT Update

Slide 77

Slide 77 text

ELM COMPONENT Update Render

Slide 78

Slide 78 text

ELM COMPONENT Update Render

Slide 79

Slide 79 text

ELM COMPONENT Update Render

Slide 80

Slide 80 text

ELM COMPONENT Update Render Call

Slide 81

Slide 81 text

ELM COMPONENT Update Render Call

Slide 82

Slide 82 text

ELM COMPONENT Update Render Call

Slide 83

Slide 83 text

ELM COMPONENT Update Render Call

Slide 84

Slide 84 text

ELM

Slide 85

Slide 85 text

ELM ▸ Update

Slide 86

Slide 86 text

ELM ▸ Update ▸ Call

Slide 87

Slide 87 text

ELM ▸ Update ▸ Call ▸ Component

Slide 88

Slide 88 text

ELM - REDUX ▸ Update -> Reducer ▸ Call ▸ Component

Slide 89

Slide 89 text

ELM - REDUX ▸ Update -> Reducer ▸ Call -> Middleware ▸ Component

Slide 90

Slide 90 text

ELM - REDUX ▸ Update -> Reducer ▸ Call -> Middleware ▸ Component -> Store

Slide 91

Slide 91 text

REACTIVE STATE class SearchComponent(private val api: Api, private val uiScheduler: Scheduler) { fun bind(actions: Observable): Observable { return actions.publish { shared -> Observable.merge( bind(shared.ofType()), bind(shared.ofType())) } .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 } } } }

Slide 92

Slide 92 text

REACTIVE STATE class SearchComponent(private val api: Api, private val uiScheduler: Scheduler) { fun bind(actions: Observable): Observable { return actions.publish { shared -> Observable.merge( bind(shared.ofType()), bind(shared.ofType())) } .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

Slide 93

Slide 93 text

REACTIVE STATE class SearchComponent(private val api: Api, private val uiScheduler: Scheduler) { fun bind(actions: Observable): Observable { return actions.publish { shared -> Observable.merge( bind(shared.ofType()), bind(shared.ofType())) } .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

Slide 94

Slide 94 text

REACTIVE STATE class SearchComponent(private val api: Api, private val uiScheduler: Scheduler) { fun bind(actions: Observable): Observable { return actions.publish { shared -> Observable.merge( bind(shared.ofType()), bind(shared.ofType())) } .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

Slide 95

Slide 95 text

REDUCER interface Reducer { fun reduce(state: S, action: A): S }

Slide 96

Slide 96 text

REDUCER class SearchReducer : Reducer { 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 } } }

Slide 97

Slide 97 text

MIDDLEWARE interface Middleware { fun bind(actions: Observable, state: Observable): Observable }

Slide 98

Slide 98 text

MIDDLEWARE class SearchMiddleware(val api: Api) : Middleware { override fun bind(actions: Observable): Observable { return actions.ofType() .flatMap { action -> api.search(action.query) .map { result -> SearchSuccessAction(result) } .onErrorReturn { e -> SearchFailureAction(e) } .startWith(SearchLoadingAction) } } }

Slide 99

Slide 99 text

REACTIVE STATE class SearchMiddleware(val api: Api) : Middleware { override fun bind(actions: Observable, state: Observable) : Observable { return actions.ofType() .withLatestFrom(state) { action, currentState -> action to currentState } .flatMap { (action, state) -> api.search(action.query) .map { result -> SearchSuccessAction(result) } .onErrorReturn { e -> SearchFailureAction(e) } .startWith(SearchLoadingAction) } } }

Slide 100

Slide 100 text

REACTIVE STATE class SearchMiddleware(val api: Api) : Middleware { override fun bind(actions: Observable, state: Observable) : Observable { return actions.ofType() .withLatestFrom(state) { action, currentState -> action to currentState } .flatMap { (action, state) -> api.search(action.query) .map { result -> SearchSuccessAction(result) } .onErrorReturn { e -> SearchFailureAction(e) } .startWith(SearchLoadingAction) } } }

Slide 101

Slide 101 text

STORE - SEARCH COMPONENT

Slide 102

Slide 102 text

STORE - SEARCH COMPONENT private val state = BehaviorRelay.createDefault(UiState()) private val actions = PublishRelay.create()

Slide 103

Slide 103 text

STORE - SEARCH COMPONENT private val state = BehaviorRelay.createDefault(UiState()) private val actions = PublishRelay.create() fun wire(): Disposable { val disposable = CompositeDisposable() disposable += actions .withLatestFrom(state) { action, state -> SearchReducer().reduce(state, action) } .subscribe(state::accept) return disposable }

Slide 104

Slide 104 text

STORE - SEARCH COMPONENT private val state = BehaviorRelay.createDefault(UiState()) private val actions = PublishRelay.create() fun wire(): Disposable { val disposable = CompositeDisposable() disposable += actions .withLatestFrom(state) { action, state -> SearchReducer().reduce(state, action) } .distinctUntilChanged() .subscribe(state::accept) return disposable }

Slide 105

Slide 105 text

STORE - SEARCH COMPONENT private val state = BehaviorRelay.createDefault(UiState()) private val actions = PublishRelay.create() fun wire(): Disposable { val disposable = CompositeDisposable() disposable += actions .withLatestFrom(state) { action, state -> SearchReducer().reduce(state, action) } .distinctUntilChanged() .subscribe(state::accept) disposable += Observable.merge( SearchMiddleware(api).bind(actions, state), SuggestionsMiddleware(api).bind(actions, state) ).subscribe(actions::accept) return disposable }

Slide 106

Slide 106 text

STORE - SEARCH COMPONENT private val state = BehaviorRelay.createDefault(UiState()) private val actions = PublishRelay.create() fun wire(): Disposable { val disposable = CompositeDisposable() disposable += actions .withLatestFrom(state) { action, state -> SearchReducer().reduce(state, action) } .distinctUntilChanged() .subscribe(state::accept) disposable += Observable.merge( SearchMiddleware(api).bind(actions, state), SuggestionsMiddleware(api).bind(actions, state) ).subscribe(actions::accept) return disposable }

Slide 107

Slide 107 text

STORE fun bind(actions: Observable, render: (UiState) -> Unit): Disposable { val disposable = CompositeDisposable() disposable += state.observeOn(uiScheduler).subscribe(render) disposable += actions.subscribe(actions::accept) return disposable }

Slide 108

Slide 108 text

STORE fun bind(actions: Observable, render: (UiState) -> Unit): Disposable { val disposable = CompositeDisposable() disposable += state.observeOn(uiScheduler).subscribe(render) disposable += actions.subscribe(actions::accept) return disposable }

Slide 112

Slide 112 text

STORE private val state = BehaviorRelay.createDefault(UiState()) private val actions = PublishRelay.create() fun wire(): Disposable { val disposable = CompositeDisposable() disposable += actions .withLatestFrom(state) { action, state -> SearchReducer().reduce(state, action) } .distinctUntilChanged() .subscribe(state::accept) disposable += Observable.merge( SearchMiddleware(api).bind(actions, state), SuggestionsMiddleware(api).bind(actions, state) ).subscribe(actions::accept) return disposable }

Slide 113

Slide 113 text

STORE private val state = BehaviorRelay.createDefault(UiState()) private val actions = PublishRelay.create() fun wire(): Disposable { val disposable = CompositeDisposable() disposable += actions .withLatestFrom(state) { action, state -> SearchReducer().reduce(state, action) } .distinctUntilChanged() .subscribe(state::accept) disposable += Observable.merge( SearchMiddleware(api).bind(actions, state), SuggestionsMiddleware(api).bind(actions, state) ).subscribe(actions::accept) return disposable }

Slide 114

Slide 114 text

STORE private val state = BehaviorRelay.createDefault(initialState) private val actions = PublishRelay.create() fun wire(): Disposable { val disposable = CompositeDisposable() disposable += actions .withLatestFrom(state) { action, state -> reducer.reduce(state, action) } .distinctUntilChanged() .subscribe(state::accept) disposable += Observable.merge( middlewares.map { it.bind(actions, state) } ).subscribe(actions::accept) return disposable }

Slide 115

Slide 115 text

STORE class SearchComponent( private val reducer: Reducer, private val middlewares: List>, private val initialState: UiState )

Slide 116

Slide 116 text

class Store( private val reducer: Reducer, private val middlewares: List>, private val initialState: S ) STORE

Slide 118

Slide 118 text

BIND IT ALL!

Slide 119

Slide 119 text

BIND IT ALL! ▸ Android Arch ViewModels

Slide 120

Slide 120 text

BIND IT ALL! ▸ Android Arch ViewModels ▸ DI Scopes

Slide 121

Slide 121 text

BIND IT ALL! ▸ Android Arch ViewModels ▸ DI Scopes ▸ Others

Slide 122

Slide 122 text

BIND IT ALL! class SearchViewModel @Inject constructor (private val store: Store) : ViewModel() { private val wiring = store.wire() private var viewBinding: Disposable? = null override fun onCleared() { wiring.dispose() } fun bind(view: MviView) { viewBinding = store.bind(view) } fun unbind() { viewBinding?.dispose() } }

Slide 123

Slide 123 text

BIND IT ALL! class SearchViewModel @Inject constructor (private val store: Store) : ViewModel() { private val wiring = store.wire() private var viewBinding: Disposable? = null override fun onCleared() { wiring.dispose() } fun bind(view: MviView) { viewBinding = store.bind(view) } fun unbind() { viewBinding?.dispose() } }

Slide 124

Slide 124 text

BIND IT ALL! class SearchViewModel @Inject constructor (private val store: Store) : ViewModel() { private val wiring = store.wire() private var viewBinding: Disposable? = null override fun onCleared() { wiring.dispose() } fun bind(view: MviView) { viewBinding = store.bind(view) } fun unbind() { viewBinding?.dispose() } }

Slide 125

Slide 125 text

BIND IT ALL! class SearchViewModel @Inject constructor (private val store: Store) : ViewModel() { private val wiring = store.wire() private var viewBinding: Disposable? = null override fun onCleared() { wiring.dispose() } fun bind(view: MviView) { viewBinding = store.bind(view) } fun unbind() { viewBinding?.dispose() } }

Slide 126

Slide 126 text

OUR UNIDIRECTIONAL DATA FLOW

Slide 127

Slide 127 text

OUR UNIDIRECTIONAL DATA FLOW ::render Middleware Middleware Middleware Reducer

Slide 128

Slide 128 text

OUR UNIDIRECTIONAL DATA FLOW ::render Middleware Middleware Middleware Reducer SearchAction

Slide 129

Slide 129 text

OUR UNIDIRECTIONAL DATA FLOW < , > SearchLoadingAction State.Empty ::render Middleware Middleware Middleware Reducer State.Empty SearchAction

Slide 130

Slide 130 text

OUR UNIDIRECTIONAL DATA FLOW < , > SearchLoadingAction State.Empty ::render Middleware Middleware Middleware Reducer State.Empty SearchAction

Slide 131

Slide 131 text

OUR UNIDIRECTIONAL DATA FLOW < , > SearchLoadingAction State.Empty ::render Middleware Middleware Middleware Reducer State.Empty SearchAction

Slide 132

Slide 132 text

OUR UNIDIRECTIONAL DATA FLOW < , > SearchLoadingAction ::render Middleware Middleware Middleware Reducer State.Empty State(loading = true)

Slide 133

Slide 133 text

OUR UNIDIRECTIONAL DATA FLOW < , > SearchLoadingAction ::render Middleware Middleware Middleware Reducer State.Empty State(loading = true)

Slide 134

Slide 134 text

OUR UNIDIRECTIONAL DATA FLOW < , > SearchLoadingAction State(loading = true) SearchSuccessAction ::render Middleware Middleware Middleware Reducer

Slide 135

Slide 135 text

OUR UNIDIRECTIONAL DATA FLOW < , > SearchLoadingAction State(loading = true) SearchSuccessAction ::render Middleware Middleware Middleware Reducer

Slide 136

Slide 136 text

OUR UNIDIRECTIONAL DATA FLOW < , > SearchLoadingAction State(loading = true) SearchSuccessAction ::render Middleware Middleware Middleware Reducer

Slide 137

Slide 137 text

OUR UNIDIRECTIONAL DATA FLOW < , > SearchSuccessAction ::render Middleware Middleware Middleware Reducer State(loading = true) State(loading = false, 
 data = newData)

Slide 138

Slide 138 text

OUR UNIDIRECTIONAL DATA FLOW < , > SearchSuccessAction ::render Middleware Middleware Middleware Reducer State(loading = true) State(loading = false, 
 data = newData)

Slide 139

Slide 139 text

BACK TO REALITY

Slide 140

Slide 140 text

BACK TO REALITY ▸ Rendering Overflow

Slide 141

Slide 141 text

ANY PROBLEMS WITH UI?

Slide 142

Slide 142 text

ANY PROBLEMS WITH UI? ▸ Lots of State updates

Slide 143

Slide 143 text

ANY PROBLEMS WITH UI? ▸ Lots of State updates ▸ May cause lots of redundant UI rerendering

Slide 144

Slide 144 text

ANY PROBLEMS WITH UI? ▸ Lots of State updates ▸ May cause lots of redundant UI rerendering ▸ …

Slide 145

Slide 145 text

ANY PROBLEMS WITH UI? ▸ Lots of State updates ▸ May cause lots of redundant UI rerendering ▸ … ▸ Domic!

Slide 146

Slide 146 text

WHAT IS DOMIC

Slide 147

Slide 147 text

WHAT IS DOMIC ▸ Diffing changes

Slide 148

Slide 148 text

WHAT IS DOMIC ▸ Diffing changes ▸ Same Android Widgets

Slide 149

Slide 149 text

WHAT IS DOMIC ▸ Diffing changes ▸ Same Android Widgets ▸ Threading

Slide 150

Slide 150 text

WHAT IS DOMIC ▸ Diffing changes ▸ Same Android Widgets ▸ Threading

Slide 151

Slide 151 text

BACK TO REALITY ▸ Rendering Overflow ▸ Testing

Slide 152

Slide 152 text

TESTING

Slide 153

Slide 153 text

TESTING VIEW // In val observer = TestObserver.create() 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")) // Out val uiState = UiState(loading = false, data = "TestData") realView.render(uiState) takeScreenshot()

Slide 154

Slide 154 text

TESTING LOGIC val viewModel = provide> viewModel.bind(fakeView) actions.onNext(SearchAction("Query")) states.assertValue(UiState(loading = true)) viewModel.unbind() actions.onNext(SearchAction("AnotherQuery")) states.assertNoValues()

Slide 155

Slide 155 text

BACK TO REALITY ▸ Rendering Overflow ▸ Testing ▸ Paging

Slide 156

Slide 156 text

youtu.be/h5afEeuI0GQ PAGING

Slide 157

Slide 157 text

BACK TO REALITY ▸ Rendering Overflow ▸ Testing ▸ Paging ▸ SingleLiveEvents

Slide 158

Slide 158 text

SINGLE LIVE EVENTS

Slide 159

Slide 159 text

SINGLE LIVE EVENTS Render Call Update

Slide 160

Slide 160 text

SINGLE LIVE EVENTS Subscriptions Render Call Update

Slide 161

Slide 161 text

BACK TO REALITY ▸ Rendering Overflow ▸ Testing ▸ Paging ▸ SingleLiveEvents ▸ Clean Architecture

Slide 162

Slide 162 text

DO YOU EVEN CLEAN?

Slide 163

Slide 163 text

DO YOU EVEN CLEAN? MviView Api Store Reducer Middleware Middleware Middleware

Slide 164

Slide 164 text

DO YOU EVEN CLEAN? MviView Store Reducer Middleware Middleware Middleware Repository

Slide 165

Slide 165 text

DO YOU EVEN CLEAN? MviView Repository Store Reducer Middleware Middleware Middleware

Slide 166

Slide 166 text

DO YOU EVEN CLEAN? MviView Repository Store Reducer Middleware Middleware Middleware Interactor?

Slide 167

Slide 167 text

DO YOU EVEN CLEAN? MviView Repository Store Reducer Middleware Middleware Middleware Presentation Layer Domain Layer Data Layer

Slide 168

Slide 168 text

TO WRAP UP

Slide 169

Slide 169 text

TO WRAP UP ▸ MVI is about presentation logic

Slide 170

Slide 170 text

TO WRAP UP ▸ MVI is about presentation logic ▸ Reactive flow

Slide 171

Slide 171 text

TO WRAP UP ▸ MVI is about presentation logic ▸ Reactive flow ▸ Sealed classes for Actions & States

Slide 172

Slide 172 text

TO WRAP UP ▸ MVI is about presentation logic ▸ Reactive flow ▸ Sealed classes for Actions & States ▸ Kotlin multiplatform usage options

Slide 173

Slide 173 text

TO WRAP UP ▸ MVI is about presentation logic ▸ Reactive flow ▸ Sealed classes for Actions & States ▸ Kotlin multiplatform usage options ▸ RxKotlin (for real)

Slide 174

Slide 174 text

TO WRAP UP ▸ MVI is about presentation logic ▸ Reactive flow ▸ Sealed classes for Actions & States ▸ Kotlin multiplatform usage options ▸ RxKotlin (for real) ▸ Reagent-like

Slide 175

Slide 175 text

TO WRAP UP ▸ MVI is about presentation logic ▸ Reactive flow ▸ Sealed classes for Actions & States ▸ Kotlin multiplatform usage options ▸ RxKotlin (for real) ▸ Reagent-like ▸ Time Travel Debugger

Slide 176

Slide 176 text

UNIDIRECTIONAL DATA FLOW LIBS

Slide 177

Slide 177 text

UNIDIRECTIONAL DATA FLOW LIBS ▸ RxRedux / Freeletics github.com/freeletics/RxRedux

Slide 178

Slide 178 text

UNIDIRECTIONAL DATA FLOW LIBS ▸ RxRedux / Freeletics github.com/freeletics/RxRedux ▸ Mobius / Spotify github.com/spotify/mobius

Slide 179

Slide 179 text

UNIDIRECTIONAL DATA FLOW LIBS ▸ RxRedux / Freeletics github.com/freeletics/RxRedux ▸ Mobius / Spotify github.com/spotify/mobius ▸ MvRx / AirBnb github.com/airbnb/MvRx

Slide 180

Slide 180 text

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

Slide 181

Slide 181 text

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

Slide 182

Slide 182 text

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

Slide 183

Slide 183 text

MVICORE

Slide 184

Slide 184 text

MVICORE ▸ Wish

Slide 185

Slide 185 text

MVICORE ▸ Wish ▸ Effect

Slide 186

Slide 186 text

MVICORE ▸ Wish ▸ Effect ▸ Actor

Slide 187

Slide 187 text

MVICORE ▸ Wish ▸ Effect ▸ Actor ▸ News

Slide 188

Slide 188 text

MVICORE ▸ Wish ▸ Effect ▸ Actor ▸ News ▸ Feature

Slide 189

Slide 189 text

MVICORE ▸ Wish -> Action ▸ Effect ▸ Actor ▸ News ▸ Feature

Slide 190

Slide 190 text

MVICORE ▸ Wish -> Action ▸ Effect -> Internal Action ▸ Actor ▸ News ▸ Feature

Slide 191

Slide 191 text

MVICORE ▸ Wish -> Action ▸ Effect -> Internal Action ▸ Actor -> Middleware ▸ News ▸ Feature

Slide 192

Slide 192 text

MVICORE ▸ Wish -> Action ▸ Effect -> Internal Action ▸ Actor -> Middleware ▸ News -> Subscriptions ▸ Feature

Slide 193

Slide 193 text

MVICORE ▸ Wish -> Action ▸ Effect -> Internal Action ▸ Actor -> Middleware ▸ News -> Subscriptions ▸ Feature -> Store

Slide 194

Slide 194 text

‣ Managing State with RxJava by Jake Wharton youtu.be/0IKHxjkgop4 ‣ The Reactive Workflow Pattern by Ray Ryan youtu.be/mvBVkU2mCF4 ‣ Domic — Reactive Virtual DOM by Artem Zinnatullin youtu.be/Ce6phlHfKR8 ‣ Domic repo github.com/lyft/domic ‣ Reagent repo github.com/JakeWharton/Reagent LINKS

Slide 195

Slide 195 text

How to cook a well done MVI for Android Sergey Ryabov @colriot