freeletics / RxRedux

Redux implementation based on RxJava

Home Page:https://freeletics.engineering/2018/08/16/rxredux.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Communication between Multiple State Machines

abhimuktheeswarar opened this issue · comments

I used RxRedux in a project with a state machine per ViewModel and another project with a single state machine for the whole app.

In a Single Activity, multi Fragment approach, I used an Activity scoped StateMachine as the AppStateMachine and Fragment scoped StateMachine with ViewModel. So in a fragment that requires to know the user logged in state, I subscribed to both the state machines. Though it does the job, I'm not happy with the implementation.

The latest approach I tried/trying has an app level state machine (AppStateMachine) that takes care of stuff like navigation, user login state... and state machine per viewModel. In this scenario, what is the best way to communicate between appStateMachine and a ViewModel level state machine?

Hi,
what do you want to achieve? Can you give us a concrete example?

You could have an action StateChangedAction to propagate one State change from one StateMachine to another? Is something like that what you are looking for?

Currently, I use this approach to propagate changes in the app level state machine's state to activity/fragment scoped state machines.

class ScreenStateMachine(appStateMachine : AppStateMachine) : StateMachine<State> {

    val input: Relay<Action> = PublishRelay.create()

init {

        appStateMachine.inputReplay.distinctUntilChanged().subscribe { appAction ->

            if ((appAction is AppAction.DeviceIdRegisteredAction)) {

                input.accept(ContentAction.LoadPlaylistsAction)
            }
        }
    }

}

Though the above setup does the job, not sure if it is the right/good way to do. I would like to have some suggestions to have a better implementation for communication between stateMachines?