Skip to content

Instantly share code, notes, and snippets.

View thealmikey's full-sized avatar
🎯
Focusing

Michael Gikaru thealmikey

🎯
Focusing
View GitHub Profile
import com.tinder.StateMachine
sealed class HumanState {
object PureBliss : HumanState()
object Happy : HumanState()
object Bored : HumanState()
object Sad : HumanState()
}
sealed class HumanAction {

Using Tinder's Finite State Machine Library to wrap Android's MediaPlayer

When developing software, it's sometimes very easy to get overwhelmed by complexity due to the number of interacting pieces.

This is a problem that's prevalent in all software development from Server-Side development, Front-End development and all the way to Android development.

In a bid to tame complexity software engineers building complex software have looked up to tools that help reduce this burden. One such tool is design using Finite State Machines.

In this tutorial we'll be using Tinder's State Machine to model an Entity and handle complexity of dealing with the many possible states it can be in a manner that makes it almost effortless.

regularMike.emotionalState.transition(HumanAction.GiveTherapy)
println(regularMike.emotionalState.state)
//Goodwork report
// HumanState$Bored@28c97a5
regularMike.emotionalState.transition(HumanAction.GiveTherapy)
println(regularMike.emotionalState.state)
//Goodwork report
// HumanState$Happy@736e9adb
fun main() {
var regularMike = Human("Mike Regular")
regularMike.emotionalState.transition(HumanAction.Entertain)
println(regularMike.emotionalState.state)
// Hapiness message
// HumanState$Happy@5b37e0d2
regularMike.emotionalState.transition(HumanAction.GiveLecture)
import com.tinder.StateMachine
sealed class HumanState {
object PureBliss : HumanState()
object Happy : HumanState()
object Bored : HumanState()
object Sad : HumanState()
}
sealed class HumanAction {
val emotionalState = StateMachine.create<HumanState, HumanAction, UseResult> {
initialState(HumanState.Bored)
state<HumanState.Bored> {
on<HumanAction.Entertain> {
transitionTo(HumanState.Happy, UseResult.ReportHappiness)
}
on<HumanAction.GiveTherapy> {
transitionTo(HumanState.Happy, UseResult.ReportGoodWork)
}
on<HumanAction.Annoy> {
onTransition {
val validTransition = it as? StateMachine.Transition.Valid ?: return@onTransition
when (validTransition.sideEffect) {
UseResult.ReportHappiness -> println("Hapiness message") //ourService.log("Hapiness message")
UseResult.ReportBoredness -> println("BoredNess message") //anotherService.log("BoredNess message")
UseResult.ReportSadness -> println("Bad vibes message") //ourService.log("Bad vibes message")
UseResult.ReportGoodWork -> println("Goodwork report") //anotherService.log("Goodwork report")
UseResult.ReportVibeKiller -> println("Please don't kill my vibe")
}
}
import com.tinder.StateMachine
sealed class HumanState {
object PureBliss : HumanState()
object Happy : HumanState()
object Bored : HumanState()
object Sad : HumanState()
}
sealed class HumanAction {
sealed class HumanState {
object PureBliss : HumanState()
object Happy : HumanState()
object Bored : HumanState()
object Sad : HumanState()
}
sealed class HumanAction {
object Entertain : HumanAction()
object GiveLecture : HumanAction()
import com.tinder.StateMachine
val stateMachine = StateMachine.create<State, Action, SideEffect>