Kotlin State Machines by Étienne Caron

1b77dd441f657f5aefb3e21283b252e6?s=47 GDG Montreal
September 29, 2019

Kotlin State Machines by Étienne Caron

State Machines are a great way to map out Model behavior in your application. They can help you can catch corner cases ahead of time, and are generally a great tool when discussing requirements with product or backend teams.

Despite these advantages, there's often a perception that implementing a state machine is too much work.

In this talk, we'll see how to leverage Kotlin language features to efficiently build State Machines with minimal boilerplate.

https://devfest2019.gdgmontreal.com/speakers/etienne_caron/

1b77dd441f657f5aefb3e21283b252e6?s=128

GDG Montreal

September 29, 2019
Tweet

Transcript

  1. Rise of the State Machines

  2. State Machine Head

  3. Florence and the State Machine

  4. Welcome to the State Machine

  5. OFF

  6. ON

  7. None
  8. OFF ON switch on switch off

  9. OFF ON switch on switch off

  10. OFF ON switch on switch off

  11. https://github.com/kanawish/android-mvi-sample "

  12. https://github.com/kanawish/android-mvi-sample #

  13. https://github.com/kanawish/android-mvi-sample # Model Intent View

  14. None
  15. None
  16. IDLE PROCESS ERROR refresh check task success failed

  17. sealed class SyncState { object IDLE : SyncState() { override

    fun toString(): String = "IDLE" } data class PROCESS(val type: Type) : SyncState() { enum class Type { REFRESH, CHECK } } data class ERROR(val throwable: Throwable) : SyncState() } IDLE PROCESS ERROR refresh check task success failed
  18. sealed class SyncState { object IDLE : SyncState() { override

    fun toString(): String = "IDLE" } data class PROCESS(val type: Type) : SyncState() { enum class Type { REFRESH, CHECK } } data class ERROR(val throwable: Throwable) : SyncState() } IDLE PROCESS ERROR refresh check task success failed
  19. Model ModelState store

  20. Model ModelState store «data class» TasksModelState tasks: List<Task>

  21. Model ModelState store «data class» TasksModelState tasks: List<Task> Task id:

    String lastUpdate: Long title: String description: String completed: Boolean tasks
  22. «data class» TasksModelState tasks: List<Task> Model ModelState store Task id:

    String lastUpdate: Long title: String description: String completed: Boolean tasks «sealed class» SyncState Idle() Process(type) Error(details) syncState syncState: SyncState
  23. «data class» TasksModelState tasks: List<Task> Model ModelState store Task id:

    String lastUpdate: Long title: String description: String completed: Boolean tasks «sealed class» SyncState Idle() Process(type) Error(details) syncState syncState: SyncState
  24. «data class» TasksModelState tasks: List<Task> Model ModelState store Task id:

    String lastUpdate: Long title: String description: String completed: Boolean tasks «sealed class» SyncState Idle() Process(type) Error(details) syncState syncState: SyncState IDLE PROCESS ERROR refresh check task success failed
  25. Live demo

  26. Live demo

  27. None
  28. None
  29. CLOSED EDITING task error? SAVING task DELETING taskId addTask() editTask()

    cancel() edit() save() error(msg) delete() error(msg) deleted() saved()
  30. Live demo

  31. Live demo