Skip to content

Is something wrong with my state machine? #25

Open
@Shrikant-B

Description

Hi, amazing library. I'm trying to implement it in my own project.

These are my states

sealed class State {
    object Loading : State()
    class Success(val response: SomeDataClass) : State()
    class Failure(val exception: Exception) : State()
}

These are the events:

sealed class Event {
    class OnApiSuccess(val response: SomeDataClass) : Event()
    class OnApiFailure(val exception: Exception) : Event()
}

Finally this is the side effect:

sealed class SideEffect {
    object AfterApiSuccess : SideEffect()
    object AfterApiFailure : SideEffect()
}

This is the implementation of state machine:

val stateMachine = StateMachine.create<State, Event, SideEffect> {
    initialState(State.Loading)
    state<State.Loading> {
        on<Event.OnApiSuccess> {
            transitionTo(State.Success(it.response), SideEffect.AfterApiSuccess)
        }
        on<Event.OnApiFailure> {
            transitionTo(State.Failure(it.exception), SideEffect.AfterApiFailure)
        }
    }
    state<State.Success> { }
    state<State.Failure> { }

    onTransition {
        if (it is StateMachine.Transition.Valid) {
            when (it.sideEffect) {
                SideEffect.AfterApiSuccess ->
                    Log.e("StateMachine", "Current State is : ${it.toState.javaClass.simpleName}")
                SideEffect.AfterApiFailure ->
                    Log.e("StateMachine", "Current State is : ${it.toState.javaClass.simpleName}")
            }
        } else if (it is StateMachine.Transition.Invalid) {
            Log.e("StateMachine", "Something went wrong")
        }
    }
}

I have a MutableLiveData in my viewmodel which observes State
val stateObserver: MutableLiveData<State> = MutableLiveData()
to which I'm posting value as
stateObserver.postValue(stateMachine.transition(Event.OnApiSuccess(response)).fromState)
or stateObserver.postValue(stateMachine.transition(Event.OnApiFailure(exception)).fromState)

And my implementation of LiveData observer is as below:

someViewModel.stateObserver.observe(this, Observer { state ->
            when (state) {
                is State.Loading -> {
                    progress.visibility = View.VISIBLE
                    list.visibility = View.GONE
                    error.visibility = View.GONE
                }
                is State.Success -> {
                    progress.visibility = View.GONE
                    list.visibility = View.VISIBLE
                    error.visibility = View.GONE
                    Log.e("TAG", "Response is :${state.response.results}")
                }
                is State.Failure -> {
                    progress.visibility = View.GONE
                    list.visibility = View.GONE
                    error.visibility = View.VISIBLE
                    Log.e("TAG", "Error is :${state.exception.message}")
                }
            }
        })

I don't what is I'm doing wrong but my app is getting stuck on loading. Help is much appreciated. Thanks.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions