Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

Riding the State Flow AndroidMakers

Slide 3

Slide 3 text

! Software Eng @ BlaBlaCar Mobile First Kotlin Open Source Arnaud GIULIANI

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Building mobile applications…

Slide 6

Slide 6 text

View Model Presenter ViewModel …

Slide 7

Slide 7 text

business logic & data layers (database, networking…) Model Presenter ViewModel … Request data to Model and prepare it for UI

Slide 8

Slide 8 text

Why are we still having issues with MVP, MVVM?

Slide 9

Slide 9 text

View, State, Action … … to help us solve them

Slide 10

Slide 10 text

MVP

Slide 11

Slide 11 text

View: display data, notified by the presenter & notify for actions Presenter: manage UI logic, data & interactions View Presenter 1 .. 1 - Contract MVP

Slide 12

Slide 12 text

Problems with MVP ⚠

Slide 13

Slide 13 text

Tight coupling logic View/Presenter Data inconsistency Not reactive approach?

Slide 14

Slide 14 text

MVVM

Slide 15

Slide 15 text

View: observe streams to display data & uses actions ViewModel: exposes actions & expose streams of data View ViewModel actions data MVVM

Slide 16

Slide 16 text

From MVP to MVVM

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Architecture Components Lifecycle ViewModel LiveData

Slide 19

Slide 19 text

Just use streams, what else?

Slide 20

Slide 20 text

Still got some problems ⚠

Slide 21

Slide 21 text

View & business logic coupling Data streams inconsistency

Slide 22

Slide 22 text

Why is it hard to write? UI binding with incomplete data? Bad separation of concerns? is MVVM less formal?

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Let’s think states over interactions

Slide 25

Slide 25 text

Unidirectional data flow ♻

Slide 26

Slide 26 text

•- the state change is passed to the view •- actions can update the state •- actions are triggered by the view •- state is passed to the view * aka Redux

Slide 27

Slide 27 text

Single source of truth Immutable data Testability, Logging & Replayability

Slide 28

Slide 28 text

Redux-ing UI bugs - Christina Lee @RunChristinaRun

Slide 29

Slide 29 text

MVI - Mosby http:/ /hannesdorfmann.com/mosby/

Slide 30

Slide 30 text

@dggonzalez

Slide 31

Slide 31 text

@dggonzalez

Slide 32

Slide 32 text

Slide 33

Slide 33 text

…well

Slide 34

Slide 34 text

View ViewModel Trigger action Dispatch action Action Reducer State Store Run action & get new state Apply & Store new state Notify new state Notify state

Slide 35

Slide 35 text

View ViewModel Trigger action Dispatch action Action Reducer State Store Run action & Got event No state Notify new event Notify event

Slide 36

Slide 36 text

Can we avoid to write Reducer & Store?

Slide 37

Slide 37 text

Just focus on writing Actions?

Slide 38

Slide 38 text

ViewModel Event State View action State - UI State (Main Data) Event - Side events

Slide 39

Slide 39 text

ViewModel offers Actions

Slide 40

Slide 40 text

Action emits States

Slide 41

Slide 41 text

Testing—action’s sequence of states & events

Slide 42

Slide 42 text

Coroutines inside ✅

Slide 43

Slide 43 text

Simplify Async Code

Slide 44

Slide 44 text

View ViewModel Trigger action Dispatch action Action Reducer State Store Run action on current state (Actor & FlowAction) Apply & Store new state (LiveData) Notify new state Notify state

Slide 45

Slide 45 text

View AndroidDataFlow Trigger action Dispatch action Action Reducer State Store Run action on current state (Actor & FlowAction) Apply & Store new state (LiveData) Notify new state Notify state Provided by Uniflow

Slide 46

Slide 46 text

Let’s go

Slide 47

Slide 47 text

#1 - A simple “Action”

Slide 48

Slide 48 text

#1 - A simple “Action”

Slide 49

Slide 49 text

Day Weather Icon Weather Description Temperature Humidity Wind

Slide 50

Slide 50 text

Day Weather Icon Weather Description Temperature Humidity Wind Empty WeatherDetail

Slide 51

Slide 51 text

Immutable Data

Slide 52

Slide 52 text

object class Empty() data class WeatherDetail(val day: String, val icon: String, val description: String, val wind: String, val temperature: String, val humidity: String)

Slide 53

Slide 53 text

object class Empty() : UIState() data class WeatherDetail(val day: String, val icon: String, val description: String, val wind: String, val temperature: String, val humidity: String) : UIState()

Slide 54

Slide 54 text

sealed class GetWeatherState : UIState(){ object class Empty() : GetWeatherState() data class WeatherDetail(val day: String, val icon: String, val description: String, val wind: String, val temperature: String, val humidity: String) : GetWeatherState() }

Slide 55

Slide 55 text

sealed class GetWeatherState : UIState(){ object class Empty() : GetWeatherState() data class WeatherDetail(val day: String, val icon: String, val description: String, val wind: String, val temperature: String, val humidity: String) : GetWeatherState() }

Slide 56

Slide 56 text

ViewModel

Slide 57

Slide 57 text

class WeatherDetailViewModel( ) : ViewModel() { }

Slide 58

Slide 58 text

class WeatherDetailViewModel( ) : ViewModel() { }

Slide 59

Slide 59 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : ViewModel() { }

Slide 60

Slide 60 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : ViewModel() { fun getDetail() { } }

Slide 61

Slide 61 text

Action as function

Slide 62

Slide 62 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : ViewModel() { fun getDetail() { } }

Slide 63

Slide 63 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : AndroidDataFlow() { fun getDetail() { } } UDF ViewModel

Slide 64

Slide 64 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : AndroidDataFlow() { fun getDetail() = action { } }

Slide 65

Slide 65 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : AndroidDataFlow() { fun getDetail() = action { } } Emit states & events ♻

Slide 66

Slide 66 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : AndroidDataFlow() { fun getDetail() = action { } } Run coroutines on IO ⚡

Slide 67

Slide 67 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : AndroidDataFlow() { fun getDetail() = action { currentState : UIState -> } } Run on current state ♻

Slide 68

Slide 68 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : AndroidDataFlow() { fun getDetail() = action { val dailyForecast = repo.getDailyForecast(id) setState { dailyForecast.mapToWeatherState() } } }

Slide 69

Slide 69 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : AndroidDataFlow() { fun getDetail() = action { val dailyForecast = repo.getDailyForecast(id) setState { dailyForecast.mapToWeatherState() } } } async call ⚡

Slide 70

Slide 70 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : AndroidDataFlow() { fun getDetail() = action { val dailyForecast = repo.getDailyForecast(id) setState { dailyForecast.mapToWeatherState() } } }

Slide 71

Slide 71 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : AndroidDataFlow() { fun getDetail() = action { val dailyForecast = repo.getDailyForecast(id) setState { dailyForecast.mapToWeatherState() } } } emit new state ♻

Slide 72

Slide 72 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : AndroidDataFlow() { fun getDetail() = action { val dailyForecast = repo.getDailyForecast(id) setState { dailyForecast.mapToWeatherState() } } }

Slide 73

Slide 73 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : AndroidDataFlow() { fun getDetail() = action { val dailyForecast = repo.getDailyForecast(id) setState { dailyForecast.mapToWeatherState() } } } fun DailyForecast.mapToWeatherState() = WeatherDetail(...)

Slide 74

Slide 74 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : AndroidDataFlow() { fun getDetail() = action { val dailyForecast = repo.getDailyForecast(id) setState { dailyForecast.mapToWeatherState() } } }

Slide 75

Slide 75 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : AndroidDataFlow(defaultState = Empty) { fun getDetail() = action { val dailyForecast = repo.getDailyForecast(id) setState { dailyForecast.mapToWeatherState() } } }

Slide 76

Slide 76 text

Error Handling

Slide 77

Slide 77 text

object class Empty() : UIState() data class Failed(val message: String, val error: Exception) : UIState() data class WeatherDetail(val day: String, val icon: String, val description: String, val wind: String, val temperature: String, val humidity: String) : UIState()

Slide 78

Slide 78 text

object class Empty() : UIState() data class Failed(val message: String, val error: Exception) : UIState() data class WeatherDetail(val day: String, val icon: String, val description: String, val wind: String, val temperature: String, val humidity: String) : UIState()

Slide 79

Slide 79 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : AndroidDataFlow() { fun getDetail() = action { val dailyForecast = repo.getDailyForecast(id) setState { dailyForecast.mapToWeatherState() } } }

Slide 80

Slide 80 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : AndroidDataFlow() { fun getDetail() = action { try { val dailyForecast = repo.getDailyForecast(id) setState { dailyForecast.mapToWeatherState() } } catch (error: Exception) { setState { Failed(“got error”, error) } } } }

Slide 81

Slide 81 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : AndroidDataFlow() { fun getDetail() = action { val dailyForecast = repo.getDailyForecast(id) setState { dailyForecast.mapToWeatherState() } } }

Slide 82

Slide 82 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : AndroidDataFlow() { fun getDetail() = action( onAction = { val dailyForecast = repo.getDailyForecast(id) setState { dailyForecast.mapToWeatherState() } } ) }

Slide 83

Slide 83 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : AndroidDataFlow() { fun getDetail() = action( onAction = { val dailyForecast = repo.getDailyForecast(id) setState { dailyForecast.mapToWeatherState() } }, onError = { error , state -> setState { Failed("got error", error) } } ) }

Slide 84

Slide 84 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : AndroidDataFlow() { fun getDetail() = action( onAction = { val dailyForecast = repo.getDailyForecast(id) setState { dailyForecast.mapToWeatherState() } }, onError = { error , state -> setState { Failed("got error", error) } } ) }

Slide 85

Slide 85 text

UI Binding

Slide 86

Slide 86 text

class WeatherDetailActivity : AppCompatActivity() { private val detailViewModel: DetailViewModel by viewModel() override fun onCreate(savedInstanceState: Bundle?) { //... onStates(detailViewModel) { state -> when (state) { is Empty -> {} // do nothing is WeatherDetail -> showDetail(state) is Failed -> showError(state.error) } } detailViewModel.getDetail() }

Slide 87

Slide 87 text

class WeatherDetailActivity : AppCompatActivity() { private val detailViewModel: DetailViewModel by viewModel() override fun onCreate(savedInstanceState: Bundle?) { //... onStates(detailViewModel) { state -> when (state) { is Empty -> {} // do nothing is WeatherDetail -> showDetail(state) is Failed -> showError(state.error) } } detailViewModel.getDetail() }

Slide 88

Slide 88 text

class WeatherDetailActivity : AppCompatActivity() { private val detailViewModel: DetailViewModel by viewModel() override fun onCreate(savedInstanceState: Bundle?) { //... onStates(detailViewModel) { state -> when (state) { is Empty -> {} // do nothing is WeatherDetail -> showDetail(state) is Failed -> showError(state.error) } } detailViewModel.getDetail() }

Slide 89

Slide 89 text

class WeatherDetailActivity : AppCompatActivity() { private val detailViewModel: DetailViewModel by viewModel() override fun onCreate(savedInstanceState: Bundle?) { //... onStates(detailViewModel) { state -> when (state) { is Empty -> {} // do nothing is WeatherDetail -> showDetail(state) is Failed -> showError(state.error) } } detailViewModel.getDetail() }

Slide 90

Slide 90 text

class WeatherDetailActivity : AppCompatActivity() { private val detailViewModel: DetailViewModel by viewModel() override fun onCreate(savedInstanceState: Bundle?) { //... onStates(detailViewModel) { state -> when (state) { is Empty -> {} // do nothing is WeatherDetail -> showDetail(state) is Failed -> showError(state.error) } } detailViewModel.getDetail() }

Slide 91

Slide 91 text

class WeatherDetailActivity : AppCompatActivity() { private val detailViewModel: DetailViewModel by viewModel() override fun onCreate(savedInstanceState: Bundle?) { //... onStates(detailViewModel) { state -> when (state) { is Empty -> {} // do nothing is WeatherDetail -> showDetail(state) is Failed -> showError(state.error) } } detailViewModel.getDetail() }

Slide 92

Slide 92 text

class WeatherDetailActivity : AppCompatActivity() { private val detailViewModel: DetailViewModel by viewModel() override fun onCreate(savedInstanceState: Bundle?) { //... onEvents(detailViewModel) { event -> when (event) { } } detailViewModel.getDetail() }

Slide 93

Slide 93 text

Testing

Slide 94

Slide 94 text

@Test fun `get a daily forecast`() { // prepare detailViewModel = DetailViewModel(id, repo) view = detailViewModel.mockObservers() // mocks val weather = MockedData.dailyForecasts.first() coEvery { repo.getDailyForecast(id) } returns weather // call detailViewModel.getDetail() // sequence of states & events verifySequence { view.states.onChanged(UIState.Empty) view.states.onChanged(weather.mapToDetailState()) } }

Slide 95

Slide 95 text

@Test fun `get a daily forecast`() { // prepare detailViewModel = DetailViewModel(id, repo) view = detailViewModel.mockObservers() // mocks val weather = MockedData.dailyForecasts.first() coEvery { repo.getDailyForecast(id) } returns weather // call detailViewModel.getDetail() // sequence of states & events verifySequence { view.states.onChanged(UIState.Empty) view.states.onChanged(weather.mapToDetailState()) } }

Slide 96

Slide 96 text

@Test fun `get a daily forecast`() { // prepare detailViewModel = DetailViewModel(id, repo) view = detailViewModel.mockObservers() // mocks val weather = MockedData.dailyForecasts.first() coEvery { repo.getDailyForecast(id) } returns weather // call detailViewModel.getDetail() // sequence of states & events verifySequence { view.states.onChanged(UIState.Empty) view.states.onChanged(weather.mapToDetailState()) } }

Slide 97

Slide 97 text

@Test fun `get a daily forecast`() { // prepare detailViewModel = DetailViewModel(id, repo) view = detailViewModel.mockObservers() // mocks val weather = MockedData.dailyForecasts.first() coEvery { repo.getDailyForecast(id) } returns weather // call detailViewModel.getDetail() // sequence of states & events verifySequence { view.states.onChanged(UIState.Empty) view.states.onChanged(weather.mapToDetailState()) } }

Slide 98

Slide 98 text

@Test fun `get a daily forecast`() { // prepare detailViewModel = DetailViewModel(id, repo) view = detailViewModel.mockObservers() // mocks val weather = MockedData.dailyForecasts.first() coEvery { repo.getDailyForecast(id) } returns weather // call detailViewModel.getDetail() // sequence of states & events verifySequence { view.states.onChanged(UIState.Empty) view.states.onChanged(weather.mapToDetailState()) } }

Slide 99

Slide 99 text

@Test fun `get a daily forecast`() { // prepare detailViewModel = DetailViewModel(id, repo) view = detailViewModel.mockObservers() // mocks val weather = MockedData.dailyForecasts.first() coEvery { repo.getDailyForecast(id) } returns weather // call detailViewModel.getDetail() // sequence of states & events verifySequence { view.states.onChanged(Empty) view.states.onChanged(weather.mapToDetailState()) } }

Slide 100

Slide 100 text

One Coding Convention

Slide 101

Slide 101 text

fun myAction() = action { }

Slide 102

Slide 102 text

View AndroidDataFlow Trigger action Dispatch action Action Reducer State Store Run action on current state (Actor & FlowAction) Apply & Store new state (LiveData) Notify new state Notify state Provided by Uniflow

Slide 103

Slide 103 text

#2 - State Guard

Slide 104

Slide 104 text

#2 - State Guard

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

NoSuggestion NewSuggestions The user has no company or n/a The user can select a company (can’t select anything) (select an existing or provide a new one)

Slide 107

Slide 107 text

One View - Multiple States

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

sealed class SelectCompanyState : UIState() { object Empty : SelectCompanyState() data class CompanyList(val list: List) : SelectCompanyState() data class CompanySelected(val selectedCompany: CompanyItem) : SelectCompanyState() object NoCompanySelected : SelectCompanyState() }

Slide 110

Slide 110 text

sealed class SelectCompanyState : UIState() { object Empty : SelectCompanyState() data class CompanyList(val list: List) : SelectCompanyState() data class CompanySelected(val selectedCompany: CompanyItem) : SelectCompanyState() object NoCompanySelected : SelectCompanyState() }

Slide 111

Slide 111 text

sealed class SelectCompanyState : UIState() { object Empty : SelectCompanyState() data class CompanyList(val list: List) : SelectCompanyState() data class CompanySelected(val selectedCompany: CompanyItem) : SelectCompanyState() object NoCompanySelected : SelectCompanyState() }

Slide 112

Slide 112 text

sealed class SelectCompanyState : UIState() { object Empty : SelectCompanyState() data class CompanyList(val list: List) : SelectCompanyState() data class CompanySelected(val selectedCompany: CompanyItem) : SelectCompanyState() object NoCompanySelected : SelectCompanyState() }

Slide 113

Slide 113 text

sealed class SelectCompanyState : UIState() { object Empty : SelectCompanyState() data class CompanyList(val list: List) : SelectCompanyState() data class CompanySelected(val selectedCompany: CompanyItem) : SelectCompanyState() object NoCompanySelected : SelectCompanyState() }

Slide 114

Slide 114 text

class SelectCompanyViewModel( private val repo: CompanySelectionRepository ) : AndroidDataFlow(defaultState = Empty) { // Search for campanies fun searchForName(name: String) = action { // look for new suggestions ... val suggestions = repo.searchForName(name) setState { CompanyList(suggestions.mapToItems()) } } // Select No Company fun selectNoCompany() = singleActionOn { repo.setNoCompany() setState { NoCompanySelected } } // Select a Company fun selectCompany(item: CompanyItem) = singleActionOn {

Slide 115

Slide 115 text

class SelectCompanyViewModel( private val repo: CompanySelectionRepository ) : AndroidDataFlow(defaultState = Empty) { // Search for campanies fun searchForName(name: String) = action { // look for new suggestions ... val suggestions = repo.searchForName(name) setState { CompanyList(suggestions.mapToItems()) } } // Select No Company fun selectNoCompany() = singleActionOn { repo.setNoCompany() setState { NoCompanySelected } } // Select a Company fun selectCompany(item: CompanyItem) = singleActionOn {

Slide 116

Slide 116 text

class SelectCompanyViewModel( private val repo: CompanySelectionRepository ) : AndroidDataFlow(defaultState = Empty) { // Search for campanies fun searchForName(name: String) = action { // look for new suggestions ... val suggestions = repo.searchForName(name) setState { CompanyList(suggestions.mapToItems()) } } // Select No Company fun selectNoCompany() = singleActionOn { repo.setNoCompany() setState { NoCompanySelected } } // Select a Company fun selectCompany(item: CompanyItem) = singleActionOn {

Slide 117

Slide 117 text

class SelectCompanyViewModel( private val repo: CompanySelectionRepository ) : AndroidDataFlow(defaultState = Empty) { // Search for campanies fun searchForName(name: String) = action { // look for new suggestions ... val suggestions = repo.searchForName(name) setState { CompanyList(suggestions.mapToItems()) } } // Select No Company fun selectNoCompany() = singleActionOn { repo.setNoCompany() setState { NoCompanySelected } } // Select a Company fun selectCompany(item: CompanyItem) = singleActionOn {

Slide 118

Slide 118 text

) : AndroidDataFlow(default state = Empty) { // Search for campanies fun searchForName(name: String) = action { // look for new suggestions ... val suggestions = repo.searchForName(name) setState { CompanyList(suggestions.mapToItems()) } } // Select No Company fun selectNoCompany() = actionOn { repo.setNoCompany() setState { NoCompanySelected } } // Select a Company fun selectCompany(item: CompanyItem) = actionOn { repo.setCompany()(item) setState { CompanySelected(item) } }

Slide 119

Slide 119 text

) : AndroidDataFlow(default state = Empty) { // Search for campanies fun searchForName(name: String) = action { // look for new suggestions ... val suggestions = repo.searchForName(name) setState { CompanyList(suggestions.mapToItems()) } } // Select No Company fun selectNoCompany() = actionOn { repo.setNoCompany() setState { NoCompanySelected } } // Select a Company fun selectCompany(item: CompanyItem) = actionOn { repo.setCompany()(item) setState { CompanySelected(item) } } Guard on state

Slide 120

Slide 120 text

) : AndroidDataFlow(default state = Empty) { // Search for campanies fun searchForName(name: String) = action { // look for new suggestions ... val suggestions = repo.searchForName(name) setState { CompanyList(suggestions.mapToItems()) } } // Select No Company fun selectNoCompany() = actionOn { currentState: Empty -> repo.setNoCompany() setState { NoCompanySelected } } // Select a Company fun selectCompany(item: CompanyItem) = actionOn { repo.setCompany()(item) setState { CompanySelected(item) } } current state is

Slide 121

Slide 121 text

val suggestions = repo.searchForName(name) setState { CompanyList(suggestions.mapToItems()) } } // Select No Company fun selectNoCompany() = actionOn { repo.setNoCompany() setState { NoCompanySelected } } // Select a Company fun selectCompany(item: CompanyItem) = actionOn { repo.setCompany(item) setState { CompanySelected(item) } }

Slide 122

Slide 122 text

val suggestions = repo.searchForName(name) setState { CompanyList(suggestions.mapToItems()) } } // Select No Company fun selectNoCompany() = actionOn { repo.setNoCompany() setState { NoCompanySelected } } // Select a Company fun selectCompany(item: CompanyItem) = actionOn { repo.setCompany(item) setState { CompanySelected(item) } } Guard on state

Slide 123

Slide 123 text

View ViewModel Trigger action Dispatch action Action Reducer State Store Run action on current state State is Wrong. Notify with BadOrWrongState event Notify BoW Notify BoW

Slide 124

Slide 124 text

@Test fun `select a company`() { // mock ... viewModel = SelectCompanyViewModel(repo) // call viewModel.selectCompany(mockItem) // sequence of states & events verifySequence { view.states.onChanged(Empty) view.events.onChanged(BadOrWrongState(state = Empty)) } }

Slide 125

Slide 125 text

@Test fun `select a company`() { // mock ... viewModel = SelectCompanyViewModel(repo) // call viewModel.selectCompany(mockItem) // sequence of states & events verifySequence { view.states.onChanged(Empty) view.events.onChanged(BadOrWrongState(state = Empty)) } }

Slide 126

Slide 126 text

NoSuggestion NewSuggestions

Slide 127

Slide 127 text

#3 - Rooting States & Views

Slide 128

Slide 128 text

Activity -> View rooting Shared ViewModel - ViewModelStoreOwner

Slide 129

Slide 129 text

One stream -> Multiple Views

Slide 130

Slide 130 text

No content

Slide 131

Slide 131 text

WeatherActivity WeatherHeader Fragment WeatherList Fragment ViewModel Weather State Create Create Weather State Weather State ⚠ no arg passed

Slide 132

Slide 132 text

onStates(viewModel) { state -> when (state) { is Loading -> { // show loading animation } is WeatherState -> { // create Fragments } is Failed -> { // display error } } } viewModel.getWeather() onStates(viewModel) { state -> when (state) { is WeatherState -> { // display data } } }

Slide 133

Slide 133 text

One stream -> Multiple Views*

Slide 134

Slide 134 text

No content

Slide 135

Slide 135 text

NewCardActivity NewCardViewModel StartFragment Start State Start State

Slide 136

Slide 136 text

NewCardActivity NewCardViewModel EditCardFragment EditCard State EditCard State

Slide 137

Slide 137 text

NewCardActivity NewCardViewModel VerifyCardFragment VerifyCard State VerifyCard State

Slide 138

Slide 138 text

NewCardActivity NewCardViewModel CheckedCardFragment CheckedCard State CheckedCard State

Slide 139

Slide 139 text

onStates(viewModel) { state -> when (state) { is StartState -> openFragment() is EditCardState -> openFragment() is VerifyCardState -> openFragment() is CheckedCardState -> openFragment() } } viewModel.startAddCardFlow() onStates(viewModel) { state -> when (state) { is -> { // display data } } }

Slide 140

Slide 140 text

#4 - Safe & Functional

Slide 141

Slide 141 text

No content

Slide 142

Slide 142 text

Better Unsafe Values Handling With Arrow.io

Slide 143

Slide 143 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : AndroidDataFlow() { fun getDetail() = action { val dailyForecast = repo.getDailyForecast(id) setState { dailyForecast.mapToWeatherState() } } }

Slide 144

Slide 144 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : AndroidDataFlow() { fun getDetail() = action { val dailyForecast = repo.getDailyForecast(id) setState { dailyForecast.mapToWeatherState() } } }

Slide 145

Slide 145 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : AndroidDataFlow() { fun getDetail() = action { val safeState = Either.catch { repo.getDailyForecast(id) } .map { it.mapToWeatherState() } .getOrHandle { error -> Failed(“got error", error) } setState { safeState } } }

Slide 146

Slide 146 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : AndroidDataFlow() { fun getDetail() = action { val safeState = Either.catch { repo.getDailyForecast(id) } .map { it.mapToWeatherState() } .getOrHandle { error -> Failed(“got error", error) } setState { safeState } } }

Slide 147

Slide 147 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : AndroidDataFlow() { fun getDetail() = action { val safeState = Either.catch { repo.getDailyForecast(id) } .map { it.mapToWeatherState() } .getOrHandle { error -> Failed("got error", error) } setState { safeState } } }

Slide 148

Slide 148 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : AndroidDataFlow() { fun getDetail() = action { val safeState = Either.catch { repo.getDailyForecast(id) } .map { it.mapToWeatherState() } .getOrHandle { error -> Failed(“got error", error) } setState { safeState } } }

Slide 149

Slide 149 text

class WeatherDetailViewModel( private val id: DailyForecastId, private val repo: WeatherRepository ) : AndroidDataFlow() { fun getDetail() = action { val safeState = Either.catch { repo.getDailyForecast(id) } .map { it.mapToWeatherState() } .getOrHandle { error -> Failed(“got error", error) } setState { safeState } } }

Slide 150

Slide 150 text

Ready to ride the State Flow?

Slide 151

Slide 151 text

Make your own Experience

Slide 152

Slide 152 text

Think UI as States

Slide 153

Slide 153 text

Unidirectional Data Flow

Slide 154

Slide 154 text

Architecture — Decisions which are both important and hard to change Martin Fowler

Slide 155

Slide 155 text

One way of thinking But many ways to write it

Slide 156

Slide 156 text

~5 classes

Slide 157

Slide 157 text

Uniflow https:/ /github.com/uniflow-kt/

Slide 158

Slide 158 text

Arnaud Giuliani @arnogiu Thank you!

Slide 159

Slide 159 text

Any question? 3 sli.do