ww-tech/roxie

Question: Suggested way to deal with "external" inputs?

curioustechizen opened this issue · 5 comments

Hello,

Thanks for Roxie. We took inspiration from this library for coming up with our own architecture. The separation between Actions and Changes really makes the code clean.

I have a question about how you would go about reacting to external inputs? If I understand the concept correctly, Actions are always user-initiated. How about things like - say, internet connection lost/regained? How would these be modelled?

We are considering making the ViewModel or domain layers deal with such inputs and emit either an Action or a Change.

Making it an Action enables easy testing (since all you need is to create the ViewModel under test and dispatch the appropriate action). However, this Action is internal to the ViewModel and it should not be exposed to the Activity.

Making it a Change is cleaner but it makes testing more difficult.

Do you have any inputs? Beyond the practical aspects of testing, are there any philosophical aspects to think about?

So what are some of the uses for it besides connection lost/regained? And what kind of output (UI update, etc) do you expect to get from this? For the latter, depending on your requirements, you can just inject some connection manager into the VewModel and each Action that needs a connection can "query" it before attempting to hit a network API. Or current connection status could be in some sort of Rx Subject which can be used as another data source used by the ViewModel whenever user Action is dispatched in.

Connection status is just an example. It could be anything external, say Bluetooth, battery status etc.

I guess my question comes from this statement in the wiki (emphasis mine):

Actions (aka Intentions) are the result of the User interacting with the UI. Each Action gets dispatched to its corresponding ViewModel. An action is the only trigger that can initiate the data flow cycle which produces new States.

In my situation, there is a trigger which is not a result of user interacting with the UI - it is external. Yet, it initiates the data flow cycle producing new states. My confusion is whether this "something external occurred" qualifies as an Action or should it just be a listener that the ViewModel subscribes to and reacts internally, producing a change.

The reason the wiki describes Action as user-initiated is that this is the only use case we encountered for it so far.
With Action being just a sealed class, my initial inclination is to use it for those external events as well and let it generate a Change and a State (or a set of States) as it normally would. The name Action is not ideal when this use case is supported though. I've seen Actions being called Events which would make more sense for external inputs.

Thinking of other approaches...

Sorry I did not search existing issues. Let's continue discussing in #7