I currently have a project that contains a list of MyItem
, and using Firebase/LiveData. It's structured into groups, and each group has items.
I want to be able to update this list if any of the following happen:
- An item is updated (on the backend through Firebase)
- A filter is changed (a separate table on Firebase for each user)
- An item is bookmarked (a separate table on Firebase for each user)
To get the list of contents, I have a function like this to return LiveData that will update whenever an item is updated (#1).
database
getList(id: String): LiveData<List<MyItem>> {
val data = MutableLiveData<List<MyItem>>()
firestore
.collection("groups")
.document(id)
.collection("items")
.addSnapshotListener { snapshot, exception ->
val items = snapshot?.toObjects(MyItem::class.java) ?: emptyList()
// filter items
data.postValue(items)
}
return data
}
And within my ViewModel, I have logic for handling that case.
viewmodel
private val result = MediatorLiveData<Resource<List<MyItem>>>()
private var source: LiveData<List<MyItem>>? = null
val contents: LiveData<Resource<List<MyItem>>>
get() {
val group = database.group
// if the selected group is changed.
return Transformations.switchMap(group) { id ->
// showing loading indicator
result.value = Resource.loading(null)
if (id != null) {
// only 1 source for the current group
source?.let {
result.removeSource(it)
}
source = database.getList(id).also {
result.addSource(it) {
result.value = Resource.success(it)
}
}
// how to add in source of filter changes?
} else {
result.value = Resource.init(null)
}
return@switchMap result
}
}
The logic is pretty complex, and hard to follow. Is there a better way to structure this to handle multiple different changes? What's the best way to store the user's current filters?
Thanks.