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

Kotlin State Machines by Étienne Caron

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/

GDG Montreal

September 29, 2019
Tweet

More Decks by GDG Montreal

Other Decks in Programming

Transcript

  1. OFF

  2. ON

  3. 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
  4. 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
  5. Model ModelState store «data class» TasksModelState tasks: List<Task> Task id:

    String lastUpdate: Long title: String description: String completed: Boolean tasks
  6. «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
  7. «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
  8. «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
  9. CLOSED EDITING task error? SAVING task DELETING taskId addTask() editTask()

    cancel() edit() save() error(msg) delete() error(msg) deleted() saved()