airbnb / mavericks

Mavericks: Android on Autopilot
https://airbnb.io/mavericks/
Apache License 2.0
5.83k stars 500 forks source link

Do we have a global state in MvRx? #226

Closed Hesamedin closed 5 years ago

Hesamedin commented 5 years ago

Hello,

My issue is probably close to this issue: https://github.com/airbnb/MvRx/issues/174 However, I don't think the answer is generic enough (combining two ViewModel classes into one class).

My case is like this:

Based on my understanding of what has recommended in the above issue, the solution is to have one ViewModel class for both of Fragments. I don't like this approach because I don't want to have a big ViewModel class. Also, I think it is against the SoC.

A good approach, in my opinion, is what Redux does by having a global state for the whole of the app. As I am very new to MvRx, do we have such a thing(global state)? If yes, would you please show me a doc or a sample app that I refer to? If not, what is your recommendation?

Thanks again for your awesome job 👍

gpeal commented 5 years ago

@Hesamedin We don't have anything like that in MvRx but what you could do is create some sort of singleton to hold that shared state that emits some sort of observable. You could then use MvRxViewModelFactory.create to inject it as a constructor dependency of both ViewModels. Then each ViewModel could call functions on it/observe its state and both would be kept in sync. Would that work?

Hesamedin commented 5 years ago

Thanks @gpeal for your answer. It makes sense. Please let me get more info about MvRxViewModelFactory.create from the wiki and I'll come back in case I have another question. I close this ticket for the meantime.

gpeal commented 5 years ago

@Hesamedin https://github.com/airbnb/MvRx/wiki#view-model-creation