Share
Embed
Transcript
88 slides
-
The Rise Of The State Machines an introduction to predictable
state container based architectures for your android apps @_mboudraa -
@_mboudraa Problem to Solve Easy to test all the components
of an app (even the UI) -
@_mboudraa Problem to Solve Easy to test all the components
of an app (even the UI) Easy to reproduce bugs to fix them -
@_mboudraa Problem to Solve Easy to test all the components
of an app (even the UI) Easy to update Easy to reproduce bugs to fix them -
@_mboudraa Problem to Solve Easy to test all the components
of an app (even the UI) Easy to update Easy to reproduce bugs to fix them Global Picture of what’s happening by reading the code -
View View Controller Action @_mboudraa Store Reducer f(AppState, Action) ->
AppState Redux / MVI -
@_mboudraa Store Reducer f(AppState, Action) -> AppState Redux / MVI
What’s a Pure function? -
@_mboudraa Store Reducer f(AppState, Action) -> AppState Redux / MVI
Pure fun add(a: Int, b: Int): Int { return a + b } What’s a Pure function? -
@_mboudraa Store Reducer f(AppState, Action) -> AppState Redux / MVI
Pure fun add(a: Int, b: Int): Int { return a + b } fun add(a: Int, b: Int): Int { val result = a + b println(result) return result } Not Pure What’s a Pure function? -
Store View View Controller Action Reducer EditText Changed f(AppState, Action)
-> AppState @_mboudraa -
Store View View Controller Action Reducer EditText Changed f(AppState, Action)
-> AppState @_mboudraa -
Store View View Controller Action Reducer EditText Changed f(AppState, Action)
-> AppState @_mboudraa -
Store View View Controller Action Reducer EditText Changed ValidateFieldsAction f(AppState,
Action) -> AppState @_mboudraa -
Store View View Controller Action Reducer EditText Changed ValidateFieldsAction f(AppState,
Action) -> AppState @_mboudraa -
Store View View Controller Action Reducer EditText Changed ValidateFieldsAction f(AppState,
Action) -> AppState New State @_mboudraa -
Store View View Controller Action Reducer EditText Changed ValidateFieldsAction f(AppState,
Action) -> AppState New State @_mboudraa -
Store View View Controller Action Reducer EditText Changed ValidateFieldsAction f(AppState,
Action) -> AppState New State View State @_mboudraa -
Store View View Controller Action Reducer EditText Changed ValidateFieldsAction f(AppState,
Action) -> AppState New State View State @_mboudraa -
Store View View Controller Action Reducer EditText Changed ValidateFieldsAction f(AppState,
Action) -> AppState New State View State @_mboudraa -
Store View View Controller Action Reducer Click on Sign In
f(AppState, Action) -> AppState @_mboudraa -
Store View View Controller Action Reducer Click on Sign In
f(AppState, Action) -> AppState @_mboudraa -
Store View View Controller Action Reducer Click on Sign In
f(AppState, Action) -> AppState @_mboudraa -
Store View View Controller Action Reducer Click on Sign In
SignInAction f(AppState, Action) -> AppState @_mboudraa -
Store View View Controller Action Reducer Click on Sign In
SignInAction f(AppState, Action) -> AppState @_mboudraa -
Store View View Controller Action Reducer Click on Sign In
SignInAction f(AppState, Action) -> AppState New State @_mboudraa -
Store View View Controller Action Reducer Click on Sign In
SignInAction f(AppState, Action) -> AppState New State @_mboudraa -
Store View View Controller Action Reducer Click on Sign In
SignInAction f(AppState, Action) -> AppState New State View State @_mboudraa -
Store View View Controller Action Reducer Click on Sign In
SignInAction f(AppState, Action) -> AppState New State View State @_mboudraa -
Store View View Controller Action Reducer Click on Sign In
SignInAction f(AppState, Action) -> AppState New State View State @_mboudraa -
Store View View Controller Action Reducer Click on Sign In
SignInAction f(AppState, Action) -> AppState New State View State Side Effect @_mboudraa -
Store View View Controller Action Reducer Click on Sign In
SignInAction f(AppState, Action) -> AppState New State View State Side Effect SignInAction @_mboudraa -
Store View View Controller Action Reducer Click on Sign In
SignInAction f(AppState, Action) -> AppState New State View State Side Effect SignInAction @_mboudraa -
Store View View Controller Action Reducer Click on Sign In
SignInAction f(AppState, Action) -> AppState New State View State Side Effect SignInAction SuccessfullySignedInAction @_mboudraa -
Store View View Controller Action Reducer Click on Sign In
SignInAction f(AppState, Action) -> AppState New State View State Side Effect SignInAction SuccessfullySignedInAction @_mboudraa -
Store View View Controller Action Reducer Click on Sign In
SignInAction f(AppState, Action) -> AppState New State View State Side Effect SignInAction SuccessfullySignedInAction @_mboudraa -
Store @_mboudraa View View Controller Action Reducer Click on Sign
In SignInAction f(AppState, Action) -> AppState New State View State Side Effect SignInAction SignInFailedAction -
Store View View Controller Action Reducer Click on Sign In
SignInAction f(AppState, Action) -> AppState New State View State Side Effect SignInAction SignInFailedAction @_mboudraa -
View View Controller Action @_mboudraa Store Reducer f(AppState, Action) ->
AppState Redux / MVI -
View View Controller Action @_mboudraa Store Reducer State f(AppState, Action)
-> AppState State Machine State( Action) -> NextState State Machine -
View View Controller Action @_mboudraa State Machine State( Action) ->
NextState State Machine State -
View View Controller Action @_mboudraa State Machine State State( Action)
-> NextState EditText Changed -
View View Controller Action @_mboudraa State Machine State State( Action)
-> NextState EditText Changed -
View View Controller Action @_mboudraa State Machine State State( Action)
-> NextState EditText Changed -
View View Controller Action @_mboudraa State Machine State State( Action)
-> NextState EditText Changed ValidateFieldsAction -
View View Controller Action @_mboudraa State Machine State State( Action)
-> NextState EditText Changed ValidateFieldsAction -
View View Controller Action @_mboudraa State Machine State State( Action)
-> NextState EditText Changed ValidateFieldsAction ValidState -
View View Controller Action @_mboudraa State Machine State State( Action)
-> NextState EditText Changed ValidateFieldsAction ValidState -
View View Controller Action @_mboudraa State Machine State State( Action)
-> NextState EditText Changed ValidateFieldsAction ValidState View State -
View View Controller Action @_mboudraa State Machine State State( Action)
-> NextState EditText Changed ValidateFieldsAction ValidState View State -
View View Controller Action @_mboudraa State Machine State State( Action)
-> NextState EditText Changed ValidateFieldsAction View State ValidState -
View View Controller Action @_mboudraa State Machine State State( Action)
-> NextState Click on Sign In -
View View Controller Action @_mboudraa State Machine State State( Action)
-> NextState Click on Sign In -
View View Controller Action @_mboudraa State Machine State State( Action)
-> NextState Click on Sign In -
View View Controller Action @_mboudraa State Machine State State( Action)
-> NextState Click on Sign In SignInAction -
View View Controller Action @_mboudraa State Machine State State( Action)
-> NextState Click on Sign In SignInAction -
View View Controller Action @_mboudraa State Machine State State( Action)
-> NextState LoadingState Click on Sign In SignInAction -
View View Controller Action @_mboudraa State Machine State State( Action)
-> NextState LoadingState Click on Sign In SignInAction -
View View Controller Action @_mboudraa State Machine State State( Action)
-> NextState LoadingState View State Click on Sign In SignInAction -
View View Controller Action @_mboudraa State Machine State State( Action)
-> NextState LoadingState View State Click on Sign In SignInAction -
View View Controller Action @_mboudraa State Machine State State( Action)
-> NextState View State Click on Sign In SignInAction LoadingState -
View View Controller Action @_mboudraa State Machine View State Click
on Sign In SignInAction LoadingState State State( Action) -> NextState -
View View Controller Action @_mboudraa State Machine View State Click
on Sign In SignInAction State AuthenticatedState State( Action) -> NextState -
View View Controller Action @_mboudraa State Machine State( Action) ->
NextState View State Click on Sign In SignInAction State AuthenticatedState -
@_mboudraa Implementation class DefaultState(private val stateMachine: StateMachine) { fun onAction(action:
Action) = if (action is Action.CheckFields) if (!action.username.isBlank()) stateMachine.nextState(ValidState()) } -
@_mboudraa Implementation class ValidState(private val stateMachine: StateMachine) { fun onAction(action:
Action) = when(action){ is Action.CheckFields-> { if (!action.username.isBlank()) stateMachine.nextState(ValidState(stateMachine)) else stateMachine.nextState(DefaultState(stateMachine)) } is Action.AttemptLogin -> { stateMachine.nexState(LoadingState(stateMachine), Action.AttemptLogin)) } } } class DefaultState(private val stateMachine: StateMachine) { fun onAction(action: Action) = if (action is Action.CheckFields) if (!action.username.isBlank()) stateMachine.nextState(ValidState()) } -
@_mboudraa Implementation class DefaultState(private val stateMachine: StateMachine) { fun onAction(action:
Action) { if (action is Action.CheckFields) { if (!action.username.isBlank()) { stateMachine.nextState(ValidState()) } } } } class LoadingState(private val stateMachine: StateMachine) { fun onAction(action: Action) = if(action is Action.AttemptLogin){ async(UI) { val account = bg { authService.signIn(action.credentials}.await() stateMachine.nextState(AuthenticatedState()) } } } class ValidState(private val stateMachine: StateMachine) { fun onAction(action: Action) = when(action){ is Action.CheckFields-> { if (!action.username.isBlank()) stateMachine.nextState(ValidState(stateMachine)) else stateMachine.nextState(DefaultState(stateMachine)) } is Action.AttemptLogin -> { stateMachine.nexState(LoadingState(stateMachine), Action.AttemptLogin)) } } } -
@_mboudraa Sum Up Pros Cons MVI / Redux Easy to
find bugs Easy to maintain Easy to add new actions Global Picture of possible actions Logic business is isolated in pure functions Side Effects are isolated ViewController need to figure out the actual state Actions can be dispatched from anywhere No Global Picture of the flow -
@_mboudraa Sum Up Pros Cons MVI / Redux Easy to
find bugs Easy to maintain Easy to add new actions Global Picture of possible actions Logic business is isolated in pure functions Side Effects are isolated ViewController need to figure out the actual state Actions can be dispatched from anywhere No Global Picture of the flow State Machine Easy to find bugs Easy to maintain Actions are carried by the state ViewController receive a state Hard to add new states No Global Picture of the flow Business Logic and Side effects are inside the state State are responsible for going to the next state -
@_mboudraa Sum Up MVI / Redux State Machine Pros Easy
to find bugs Easy to maintain Easy to add new actions Global Picture of possible actions Logic business is isolated in pure functions Side Effects are isolated Easy to find bugs Easy to maintain Actions are carried by the state ViewController receive a state Cons ViewController need to figure out the actual state Actions can be dispatched from anywhere No Global Picture of the flow Hard to add new states No Global Picture of the flow Business Logic and Side effects are inside the state State are responsible for going to the next state -
@_mboudraa Kotlin to the rescue html { title = "Droidcon
Berlin" body { ... } } -
@_mboudraa Kotlin to the rescue class HTML { var title:
String inline fun body(f: ()-> Body) { ... } } html { title = "Droidcon Berlin" body { ... } } -
@_mboudraa Kotlin to the rescue class HTML { var title:
String inline fun body(f: ()-> Body) { ... } } inline fun html(content: HTML.() -> Unit): HTML { val html = HTML() // create the receiver object html.content() // pass the receiver object to the lambda return html } html { title = "Droidcon Berlin" body { ... } } -
The Rise Of The State Machines an introduction to predictable
state container based architectures for your android apps @_mboudraa