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)) } } }