Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use data from one ViewModel in another ViewModel

I have an AddressesViewModel which holds the user's favorite addresses and another SearchViewModel which holds searched addresses. when user searched an address I have to check whether this address is favorite or not by checking in the favorites array. what is the proper way to do it?

I've already tried subscribing to the AddressesViewModel from the SearchViewModel but I'm looking for other options as it's creating too much dependency between those view models.

like image 939
Ofek Regev Avatar asked Jan 07 '19 08:01

Ofek Regev


People also ask

Can ViewModel contain other ViewModels?

It is perfectly right to have ViewModel contains ViewModel. Actually that is the recommended practice: To have one main ViewModel for the whole page, the main ViewModel could contain ViewModels for the sub views in the page.

Can a ViewModel be shared between activities?

In android, we can use ViewModel to share data between various fragments or activities by sharing the same ViewModel among all the fragments and they can access everything defined in the ViewModel. This is one way to have communication between fragments or activities.

Can we have multiple ViewModel in a fragment?

In fact you can have multiple view models for a single fragments doing different things for you. Keeping everything in one view model would make testing harder and also you have to keep viewmodel for all 20 fragments may be scoping to activity.


2 Answers

Another alternative if I understand the question correctly. Assuming that you first have this:

ViewModelChild(constructor etc) : ViewModelParent(){

    // you can create a var/val to observe a variable in viewmodel parent.
    // upon observation of
    //this you can change other variables assigned here. 

}
like image 58
SlowLearnah Avatar answered Sep 22 '22 11:09

SlowLearnah


You will have to attach two ViewModels to the same lifecycle owner. eg You have an activity named MainActivity, two ViewModels named AddressesViewModel and SearchViewModel and you need to get a variable named address for SearchViewModel to AddressesViewModel

class MyActivity: AppCompactAvtivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        .
        .
        // Attach the ViewModels
        val addressViewModel = ViewModelProviders.of(this).get(AddressesViewModel::class.java)
        val searchViewModel = ViewModelProviders.of(this).get(SearchViewModel::class.java)

        // Listen to address which is in SearchViewModel
        searchViewModel.address.observe(this, Observer { address ->
            // Send the variable to AddressesViewModel using a public method
            val favOrNot addressViewModel.isAddressFavourite(address)
            // or 
            addressViewModel.favouriteAddress = address
        })
    }
}

Hope this answers your question.

like image 24
murad Avatar answered Sep 20 '22 11:09

murad