Skip to content

Instantly share code, notes, and snippets.

@nyxee
Created April 6, 2021 22:21
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/3f4719c95bd374a2a7fc6f22f50a7a6d to your computer and use it in GitHub Desktop.
Save nyxee/3f4719c95bd374a2a7fc6f22f50a7a6d to your computer and use it in GitHub Desktop.
//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