0

In my app I have some data that will be used across all app in the different fragments. According to the Official Android Guides we should use LiveData and SharedViewModel That documentations shows just how to use data from SharedViewModel in fragment. But ... How to use that data in the FragmentViewModel?

Use case #1: using the SharedInfo from SharedViewModel I need to make some request to the server and to do smth with response from server in the FragmentViewModel Use case #2: I have some screen (fragment) that shows info both from FragmentVM and SharedVM Use case #3: When user click on SomeButton I need to pass some data from SharedViewModel to the ViewModel

I have found two possibles ways how to do it (maybe their are very similar), but I seems that it can be done more clearly 1) Subscribe to LiveData from SharedViewModel in the fragment and call some method in the ViewModel 2) Use the "CombineLatest" approach like in the RX ( thanks for https://github.com/adibfara/Lives )

Some example to reproduce:


class SharedViewModel(app: Application) : ViewModel(app) {
  val sharedInfo = MutableLiveData<InfoModel>()
}


class MyFragmentViewModel(app: Application) : ViewModel(app) {
  val otherInfo = MutableLiveData<OtherModel>()
}

class StartFragment : Fragment(){

  lateinit var viewModel: MyFragmentViewModel
  lateinit var sharedViewModel: SharedViewModel


 override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
// Create Shared ViewModel in the Activity Scope
        activity?.let {
            sharedViewModel = ViewModelProviders.of(it).get(SharedViewModel::class.java)
        }
// Create simple ViewModel forFragment
 viewModel = ViewModelProviders.of(this).get(MyFragmentViewModel::class.java)

// Way #1
     sharedViewModel.sharedInfo.observe(this, Observer{
         viewModel.toDoSmth(it)
     })
     viewModel.otherInfo.observe(this, Observer{
         sharedViewModel.toDoSmth(it)
     })


// Way #2
     combineLatest(sharedViewModel.sharedInfo, viewModel.otherInfo){s,o -> Pair(s,o)}.observe(this, Observe{
         viewModel.doSmth(it)
//        or for example
         sharedViewModel.refreshInfo(it)
     })
    }
}

I expect to found some clear way to access to LiveData from SharedVM from FragmentVm and vise versa. Or maybe I think wrong and this is a bad approach to do that and I shouldn't do it

GreenJay
  • 93
  • 6
  • shared viewmodel could be dependency of fragment viemodel – Blackbelt Jul 29 '19 at 16:44
  • @Blackbelt hmm.. but how can I do this? I don't think pass Shared VM to the Fragment VM is the good way. Create some setter\getter.. hmm..no... I just need data from one VM to another. Data contains in the LiveData object that's why the only way to access to that data is observe it . I can't observe LiveData in the ViewModel (can do transformation but it only for LiveDatas in the same VM) – GreenJay Jul 29 '19 at 18:41
  • It is either that or you have to deal with both VM on the fragment. – Blackbelt Jul 30 '19 at 05:21

0 Answers0