Hi Everyone - I have a quick architecture question...
# android-architecture
a
Hi Everyone - I have a quick architecture question for the group. I'm building an app right now that uses
LiveData
heavily. Basically any dynamic element of the UI is governed by a separate
LiveData
stream that's controlled by a
ViewModel
for the feature. Most ViewModels I have in the app follow the pattern of loading up some stuff from the network when they're created and pushing a bunch of values out to those
LiveData
streams. That's all been working great, and my viewmodels have been mostly stateless. For the most part, my activity just calls methods directly on my view model whenever it needs to trigger an action - for example, if a user uses the pull to refresh ability on a page the activity will call
pageRefreshed
or some such method on the view model, which will then reload some info and pass more info through those streams. This has also been working fine. However, I've now hit a scenario where a button in the app can have different state depending on the corresponding state of the page. So now my activity can't call one static
startVisit
method on the view model, since that action may now mean
endVisit
instead. One option here would be to hold onto more state in the view model and have the activity call
buttonPressed
and let the view model figure out what that action should be. That would work fine, but I'm really trying to keep everything as stateless (at least from my perspective) as possible. One option I'm considering is actually passing a function through one of my
LiveData
streams that governs what to do when the button is pressed. So I'd have a
LiveData
that looks something like this:
val buttonClickStream = MutableLiveData<() -> Unit>()
Has anyone tried something like this before? Any pros or cons?