SHARE
TWEET

Untitled

a guest May 22nd, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package io.github.fentonmartin.network.firebase;
  2.  
  3. import android.support.annotation.NonNull;
  4. import android.support.annotation.Nullable;
  5.  
  6. import com.google.android.gms.tasks.OnCompleteListener;
  7. import com.google.android.gms.tasks.OnFailureListener;
  8. import com.google.android.gms.tasks.OnSuccessListener;
  9. import com.google.android.gms.tasks.Task;
  10. import com.google.firebase.firestore.DocumentReference;
  11. import com.google.firebase.firestore.DocumentSnapshot;
  12. import com.google.firebase.firestore.EventListener;
  13. import com.google.firebase.firestore.FirebaseFirestore;
  14. import com.google.firebase.firestore.FirebaseFirestoreException;
  15. import com.google.firebase.firestore.ListenerRegistration;
  16. import com.google.firebase.firestore.QuerySnapshot;
  17. import com.google.firebase.firestore.SetOptions;
  18.  
  19. import java.util.Map;
  20.  
  21. public class FirebaseUtils {
  22.  
  23.     public static void sendData(String collection, String document, Map<String, Object> data, FirebaseCallback callback) {
  24.         FirebaseFirestore db = FirebaseFirestore.getInstance();
  25.         db.collection(collection)
  26.                 .document(document)
  27.                 .set(data, SetOptions.merge())
  28.                 .addOnSuccessListener(new OnSuccessListener<Void>() {
  29.                     @Override
  30.                     public void onSuccess(Void aVoid) {
  31.                         if (callback != null)
  32.                             callback.onSuccess(null);
  33.                     }
  34.                 })
  35.                 .addOnFailureListener(new OnFailureListener() {
  36.                     @Override
  37.                     public void onFailure(@NonNull Exception e) {
  38.                         if (callback != null)
  39.                             callback.onFailure(e);
  40.                     }
  41.                 });
  42.     }
  43.  
  44.     public static void readData(String collection, String id, FirebaseCallback callback) {
  45.         FirebaseFirestore db = FirebaseFirestore.getInstance();
  46.         db.collection(collection)
  47.                 .document(id)
  48.                 .get()
  49.                 .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
  50.                     @Override
  51.                     public void onSuccess(DocumentSnapshot documentSnapshot) {
  52.                         if (callback != null)
  53.                             callback.onSuccess(documentSnapshot);
  54.                     }
  55.                 })
  56.                 .addOnFailureListener(new OnFailureListener() {
  57.                     @Override
  58.                     public void onFailure(@NonNull Exception e) {
  59.                         if (callback != null)
  60.                             callback.onFailure(e);
  61.                     }
  62.                 });
  63.     }
  64.  
  65.     public static void readCollection(String collection, FirebaseCollectionCallback callback) {
  66.         FirebaseFirestore db = FirebaseFirestore.getInstance();
  67.         db.collection(collection)
  68.                 .get()
  69.                 .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
  70.                     @Override
  71.                     public void onComplete(@NonNull Task<QuerySnapshot> task) {
  72.                         callback.onComplete(task);
  73.                     }
  74.                 });
  75.     }
  76.  
  77.     private static ListenerRegistration registration;
  78.  
  79.     public static void realtimeCollection(String collection, String id, FirebaseCallback callback) {
  80.         FirebaseFirestore db = FirebaseFirestore.getInstance();
  81.         DocumentReference docRef = db.collection(collection).document(id);
  82.         if (registration != null) registration.remove();
  83.         registration = docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
  84.             @Override
  85.             public void onEvent(@Nullable DocumentSnapshot snapshot,
  86.                                 @Nullable FirebaseFirestoreException e) {
  87.                 if (callback != null) {
  88.                     callback.onSuccess(snapshot);
  89.                     callback.onFailure(e);
  90.                 }
  91.             }
  92.         });
  93.     }
  94.  
  95.     public static void realtimeCollectionRemove() {
  96.         if (registration != null) registration.remove();
  97.     }
  98. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top