Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
class FirestoreViewModel : ViewModel(){
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
var savedAddressList : MutableList<AddressItem> = mutableListOf()
for (doc in value!!) {
var addressItem = doc.toObject(
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.