Skip to content

Instantly share code, notes, and snippets.

@nyxee
Last active April 6, 2021 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nyxee/1e0df9a1fa57e1364238357b19248292 to your computer and use it in GitHub Desktop.
Save nyxee/1e0df9a1fa57e1364238357b19248292 to your computer and use it in GitHub Desktop.
The Problem has been solved
//CURRENT POSITION
class ClubsViewModel<T> (clazz: Class<T>) : BaseViewModel<T>(clazz) { //<< THIS IS WHERE THE CURRENT PROBLEM WAS.
private var _mClubs = MutableLiveData<List<T>>()
listenToFireStoreCollection("Clubs", _mClubs)
...
}
class BViewModel<T> (clazz: Class<T>) : BaseViewModel<T>(clazz) { //<< THIS IS WHERE THE CURRENT PROBLEM WAS.
private var _mBs = MutableLiveData<List<T>>()
listenToFireStoreCollection("Bname", _mBs)
...
}
class BaseViewModel<T>(val clazz: Class<T>) {
protected val mFirestore = Firebase.firestore
protected fun listenToFireStoreCollection(val collectionName: String, liveData: MutableLiveData<List<T>>)
mFirestore.collection(collectionName).addSnapshotListener { snapshot, e ->
// if there is an exception we want to skip.
if (e != null) {
return@addSnapshotListener
}
// if we are here, we did not encounter an exception
if (snapshot != null) {
liveData.value = snapshot.documents.mapNotNull { it.toObject(clazz) }
}
}
}
}
//FRAGMENT EXAMPLES.
class ClubsFragment : Fragment() {
private val mClubsViewModel: ClubsViewModel<ClubsFSEntity> by viewModels()
...
}
class BsFragment : Fragment() {
private val mBsViewModel: BsViewModel<BsFSEntity> by viewModels()
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment