You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
By looking at your code, I can see you're posting transition.fromState to the MutableLiveData. That means it would never emit the terminal states you defined (Success or Failure) unless you postValue the toState part of the transition instead.
Also, you're not actually posting the initial state (Loading) as the ViewModel gets initialized.
I hope this helps.
@lfernandosp Thanks for you suggestion, it did helped me with my issue.
So, I changed my LiveData from MutableLiveData<State> to MutableLiveData<StateMachine.Transition<State, Event, SideEffect>>() that gives me Transition with ToState & SideEffect.
I have statemachine as below:
val stateMachine = StateMachine.create<State, Event, SideEffect> {
initialState(State.Idle)
state<State.Idle> { onEnter { dontTransition(SideEffect.OnIdle) } }
onTransition {
if (it is StateMachine.Transition.Valid) {
when (it.sideEffect) {
is SideEffect.OnIdle -> {
}
}
} else {
Log.e("StateMachine", "Invalid transition")
}
}
}
As stateMachine.transition() requires Event to get Transition. How do I consume Transition from onEnter dontTransition?
Hi, amazing library. I'm trying to implement it in my own project.
These are my states
These are the events:
Finally this is the side effect:
This is the implementation of state machine:
I have a
MutableLiveData
in my viewmodel which observesState
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:
I don't what is I'm doing wrong but my app is getting stuck on loading. Help is much appreciated. Thanks.
The text was updated successfully, but these errors were encountered: