Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//INITIAL POSITION
class ClubsViewModel(): BaseViewModel() {
private var _mClubs = MutableLiveData<ArrayList<ClubFSEntity>>()
...
private fun listenToFireStoreClubs() {
mFirestore.collection("Clubs").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) {
val allClubs = ArrayList<ClubFSEntity>()
snapshot.documents.forEach {
it.toObject(ClubFSEntity::class.java)?.let {
allClubs.add(it)
}
}
_mClubs.value = allClubs
}
}
}
}
...
...
class ProjectsViewModel(): BaseViewModel() {
private var _mProjects = MutableLiveData<ArrayList<ProjectsFSEntity>>()
private fun listenToFireStoreProjects() {
//Code Here is Similar to the Code in the similar function in ClubsViewModel.
}
}
...
...
//Many Viewmodels will extend this class.
open class BaseViewModel() : ViewModel() {
protected val mFirestore = Firebase.firestore
protected var mStorageReferenence: StorageReference
protected val _networkState = MutableLiveData<NetworkState>()
protected val networkState: LiveData<NetworkState> = _networkState
//I would like to make this function private to access the above variables.
inline fun listenToFireStoreCollection[TO MOVE HERE SO THAT ALL MY OTHER VIEW MODELS CAN CALL IT]
}
...
...
@Parcelize
data class ClubFSEntity(val title: String="",
val description: String="",
val startDate: String?=null,
var clubId : String=""): Parcelable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment