I've been unable to create an Android Architecuture Components ViewModel that composes multiple LiveData models into one LiveData class for my Fragment to observe. I want to hide the model details from the Fragment and respond to external data changes through the individual models.
The problem is I need the ViewModel to observe the model changes but ViewModel is not a LifecycleOwner so it can't observe. Since I don't want to pass the LiveData objects through to the UI, I'm stuck.
Is this possible? Do I need to abandon LiveData for my models and resort to a different observation pattern / tool?
Edit: Pseudocode added. My actual classes are much more complex and lengthy. I hope my intent is understandable.
// OneDataModel.kt
class oneDataModel {
val oneDataElement = ""
}
// AnotherDataModel.kt
class anotherDataModel {
val anotherDataElement = 19
}
// OneDataRepository.kt
class OneDataRepository {
val oneDataSet = MutableLiveData<MutableList<oneDataModel>>()
private val dataListener = object: ChildEventListener {
override fun onChildAdded(snapshot: DataSnapshot, p1: String?) {
val newChild = snapshot.getValue(oneDataModel::class.java)
if (newChild != null) {
oneDataSet.value?.add(newChild)
}
}
}
init {
oneDataSet.value = mutableListOf<oneDataModel>()
OneNetworkDataTable.addListener(dataListener)
}
}
// AnotherDataRepository.kt
class AnotherDataRepository {
var anotherDataSet = MutableLiveData<MutableList<anotherDataModel>>()
private val dataListener = object: ChildEventListener {
override fun onChildAdded(snapshot: DataSnapshot, p1: String?) {
val newChild = snapshot.getValue(anotherDataModel::class.java)
if (newChild != null) {
anotherDataSet.value?.add(newChild)
}
}
}
init {
anotherDataSet.value = mutableListOf<anotherDataModel>()
AnotherNetworkDataTable.addListener(dataListener)
}
}
// ComposedViewModel.kt
class ComposedViewModel: ViewModel() {
class ComposedItem {
var dataName: String = "" // From OneDataRepository items
var dataValue: Int = -1 // From AnotherDataRepository items
}
var publishedDataSet = MutableLiveData<MutableList<ComposedItem>>()
//***
//*** WHAT GOES HERE? HOW DO I LISTEN TO EACH OF THE DATA REPOSITORIES AND BUILD UP COMPOSED
//*** ITEMS FOR THE UI?
//***
}
// MyFragment.kt
class MyFragment : Fragment() {
private val composedViewModel: ComposedViewModel by lazy { ViewModelProviders.of(activity).get(ComposedViewModel::class.java) }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_feed, container, false)
recyclerView.adapter = UIAdapter
composedViewModel.publishedDataSet.observe(this, Observer {
UIAdapter.notifyDataSetChanged()
})
return view
}
}