Slide 1

Slide 1 text

MVI in a nutshell Aida Issayeva

Slide 2

Slide 2 text

About me 󰠁 Software Engineer 🤖 I build Android apps 󰠅 Udacity Instructor 🐦 @aida_isay 🌐 cupsofcode.com

Slide 3

Slide 3 text

Android Architecture MVC MVP MVVM MVI

Slide 4

Slide 4 text

What is MVI? __

Slide 5

Slide 5 text

What is MVI? Model View Intent __

Slide 6

Slide 6 text

Unidirectional flow! Or reactive arch pattern __

Slide 7

Slide 7 text

https://proandroiddev.com/the-contract-of-the-model-view-intent-architecture-777f95706c1e

Slide 8

Slide 8 text

Multiple User Actions sealed class FeedIntent { data class RestaurantClicked(val restaurantId: String) : FeedIntent() data class LikeClicked(val restaurantId: String, val isLiked: Boolean) : FeedIntent() } __

Slide 9

Slide 9 text

Multiple Results val dataIntent = feedRestaurantsUseCase.execute() .map { FeedIntent.Data(restaurants = it.map { it.toUiModel(stringResources) }) } .startWith(FeedIntent.Loading) .onErrorReturn { FeedIntent.Error(error = it) } __

Slide 10

Slide 10 text

Single ViewState data class FeedViewState( val isLoading: Boolean = false, val error: Throwable? = null, val restaurants: List = emptyList(), val toolbarTitle: String = "", val searchedCity: String = "Hoboken", val emptyListMessage: String = "" ) __

Slide 11

Slide 11 text

Reducer from multiple intents into single viewstate. __________________

Slide 12

Slide 12 text

private val reducer = BiFunction { previous, intent -> when (intent) { is FeedIntent.Data -> { previous.copy( restaurants = intent.restaurants, isLoading = false, toolbarTitle = "Sample", emptyListMessage = "" ) } else -> previous } } __

Slide 13

Slide 13 text

UI subscription to the single viewstate __

Slide 14

Slide 14 text

Benefits: State Management Thread Safety Easy Testing __

Slide 15

Slide 15 text

Side effects: Opportunity for memory leaks Boilerplate __

Slide 16

Slide 16 text

Checkout the github project

Slide 17

Slide 17 text

Thank you @aida_isay cupsofcode.com

Slide 18

Slide 18 text

Resources: ● Android MVI reactive architecture pattern ● Unidirectional state flow patterns ● Battle of the android architecture patterns