Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Mounir Boudraa - Rise of state machines

Mounir Boudraa - Rise of state machines

droidcon Berlin

July 17, 2018
Tweet

More Decks by droidcon Berlin

Other Decks in Programming

Transcript

  1. The Rise Of The State Machines an introduction to predictable

    state container based architectures for your android apps @_mboudraa
  2. @_mboudraa Problem to Solve Easy to test all the components

    of an app (even the UI) Easy to reproduce bugs to fix them
  3. @_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
  4. @_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
  5. @_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?
  6. @_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?
  7. Store View View Controller Action Reducer Click on Sign In

    f(AppState, Action) -> AppState @_mboudraa
  8. Store View View Controller Action Reducer Click on Sign In

    f(AppState, Action) -> AppState @_mboudraa
  9. Store View View Controller Action Reducer Click on Sign In

    f(AppState, Action) -> AppState @_mboudraa
  10. Store View View Controller Action Reducer Click on Sign In

    SignInAction f(AppState, Action) -> AppState @_mboudraa
  11. Store View View Controller Action Reducer Click on Sign In

    SignInAction f(AppState, Action) -> AppState @_mboudraa
  12. Store View View Controller Action Reducer Click on Sign In

    SignInAction f(AppState, Action) -> AppState New State @_mboudraa
  13. Store View View Controller Action Reducer Click on Sign In

    SignInAction f(AppState, Action) -> AppState New State @_mboudraa
  14. Store View View Controller Action Reducer Click on Sign In

    SignInAction f(AppState, Action) -> AppState New State View State @_mboudraa
  15. Store View View Controller Action Reducer Click on Sign In

    SignInAction f(AppState, Action) -> AppState New State View State @_mboudraa
  16. Store View View Controller Action Reducer Click on Sign In

    SignInAction f(AppState, Action) -> AppState New State View State @_mboudraa
  17. Store View View Controller Action Reducer Click on Sign In

    SignInAction f(AppState, Action) -> AppState New State View State Side Effect @_mboudraa
  18. Store View View Controller Action Reducer Click on Sign In

    SignInAction f(AppState, Action) -> AppState New State View State Side Effect SignInAction @_mboudraa
  19. Store View View Controller Action Reducer Click on Sign In

    SignInAction f(AppState, Action) -> AppState New State View State Side Effect SignInAction @_mboudraa
  20. Store View View Controller Action Reducer Click on Sign In

    SignInAction f(AppState, Action) -> AppState New State View State Side Effect SignInAction SuccessfullySignedInAction @_mboudraa
  21. Store View View Controller Action Reducer Click on Sign In

    SignInAction f(AppState, Action) -> AppState New State View State Side Effect SignInAction SuccessfullySignedInAction @_mboudraa
  22. Store View View Controller Action Reducer Click on Sign In

    SignInAction f(AppState, Action) -> AppState New State View State Side Effect SignInAction SuccessfullySignedInAction @_mboudraa
  23. Store @_mboudraa View View Controller Action Reducer Click on Sign

    In SignInAction f(AppState, Action) -> AppState New State View State Side Effect SignInAction SignInFailedAction
  24. Store View View Controller Action Reducer Click on Sign In

    SignInAction f(AppState, Action) -> AppState New State View State Side Effect SignInAction SignInFailedAction @_mboudraa
  25. View View Controller Action @_mboudraa Store Reducer
 State f(AppState, Action)

    -> AppState State Machine State( Action) -> NextState State Machine
  26. View View Controller Action @_mboudraa State Machine State State( Action)

    -> NextState EditText Changed ValidateFieldsAction
  27. View View Controller Action @_mboudraa State Machine State State( Action)

    -> NextState EditText Changed ValidateFieldsAction
  28. View View Controller Action @_mboudraa State Machine State State( Action)

    -> NextState EditText Changed ValidateFieldsAction ValidState
  29. View View Controller Action @_mboudraa State Machine State State( Action)

    -> NextState EditText Changed ValidateFieldsAction ValidState
  30. View View Controller Action @_mboudraa State Machine State State( Action)

    -> NextState EditText Changed ValidateFieldsAction ValidState View State
  31. View View Controller Action @_mboudraa State Machine State State( Action)

    -> NextState EditText Changed ValidateFieldsAction ValidState View State
  32. View View Controller Action @_mboudraa State Machine State State( Action)

    -> NextState EditText Changed ValidateFieldsAction View State ValidState
  33. View View Controller Action @_mboudraa State Machine State State( Action)

    -> NextState LoadingState Click on Sign In SignInAction
  34. View View Controller Action @_mboudraa State Machine State State( Action)

    -> NextState LoadingState Click on Sign In SignInAction
  35. View View Controller Action @_mboudraa State Machine State State( Action)

    -> NextState LoadingState View State Click on Sign In SignInAction
  36. View View Controller Action @_mboudraa State Machine State State( Action)

    -> NextState LoadingState View State Click on Sign In SignInAction
  37. View View Controller Action @_mboudraa State Machine State State( Action)

    -> NextState View State Click on Sign In SignInAction LoadingState
  38. View View Controller Action @_mboudraa State Machine View State Click

    on Sign In SignInAction LoadingState State State( Action) -> NextState
  39. View View Controller Action @_mboudraa State Machine View State Click

    on Sign In SignInAction State AuthenticatedState State( Action) -> NextState
  40. View View Controller Action @_mboudraa State Machine State( Action) ->

    NextState View State Click on Sign In SignInAction State AuthenticatedState
  41. @_mboudraa Implementation class DefaultState(private val stateMachine: StateMachine) { fun onAction(action:

    Action) = if (action is Action.CheckFields) if (!action.username.isBlank()) stateMachine.nextState(ValidState()) }
  42. @_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()) }
  43. @_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)) } } }
  44. @_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
  45. @_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
  46. @_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
  47. @_mboudraa Kotlin to the rescue class HTML { var title:

    String inline fun body(f: ()-> Body) { ... } } html { title = "Droidcon Berlin" body { ... } }
  48. @_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 { ... } }
  49. The Rise Of The State Machines an introduction to predictable

    state container based architectures for your android apps @_mboudraa