Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class FirestoreViewModel : ViewModel(){
val TAG = "FIRESTORE_VIEW_MODEL"
var firebaseRepository = FirestoreRepository()
var savedAddresses : MutableLiveData<List<AddressItem>> = MutableLiveData()
// save address to firebase
fun saveAddressToFirebase(addressItem: AddressItem){
firebaseRepository.saveAddressItem(addressItem).addOnFailureListener {
Log.e(TAG,"Failed to save Address!")
}
}
// get realtime updates from firebase regarding saved addresses
fun getSavedAddresses(): LiveData<List<AddressItem>>{
firebaseRepository.getSavedAddress().addSnapshotListener(EventListener<QuerySnapshot> { value, e ->
if (e != null) {
Log.w(TAG, "Listen failed.", e)
savedAddresses.value = null
return@EventListener
}
var savedAddressList : MutableList<AddressItem> = mutableListOf()
for (doc in value!!) {
var addressItem = doc.toObject(AddressItem::class.java)
savedAddressList.add(addressItem)
}
savedAddresses.value = savedAddressList
})
return savedAddresses
}
// delete an address from firebase
fun deleteAddress(addressItem: AddressItem){
firebaseRepository.deleteAddress(addressItem).addOnFailureListener {
Log.e(TAG,"Failed to delete Address")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.