Skip to content

Instantly share code, notes, and snippets.

@prasadshirvandkar
Created July 26, 2020 16:46
Show Gist options
  • Save prasadshirvandkar/d9bffe9bd8fdb4721057f480da663621 to your computer and use it in GitHub Desktop.
Save prasadshirvandkar/d9bffe9bd8fdb4721057f480da663621 to your computer and use it in GitHub Desktop.
import android.util.Log
import com.google.firebase.firestore.CollectionReference
import com.google.firebase.firestore.Query
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase
import com.midsizemango.scribblergame.Constants.Companion.FIRESTORE
class FirestoreAPI(collectionName: String) {
private val db = Firebase.firestore
private var collectionRef: CollectionReference
init {
collectionRef = db.collection(collectionName)
}
fun addDocument(data: Any, documentId: String) {
collectionRef.document(documentId).set(data)
.addOnSuccessListener {
Log.d(FIRESTORE, "Data Added")
}
.addOnFailureListener {
Log.e(FIRESTORE, it.toString())
}
}
fun addMultiCollectionDocument(
data: Any?,
documentIdFirst: String,
collectionName: String
) {
collectionRef.document(documentIdFirst).collection(collectionName)
.document().set(data!!)
.addOnSuccessListener {
Log.d(FIRESTORE, "Data Added")
}
.addOnFailureListener {
Log.e(FIRESTORE, it.toString())
}
}
fun addMultiCollectionDocument(
data: Any?,
documentIdFirst: String,
collectionName: String,
documentIdSecond: String
) {
collectionRef.document(documentIdFirst).collection(collectionName)
.document(documentIdSecond).set(data!!)
.addOnSuccessListener {
Log.d(FIRESTORE, "Data Added")
}
.addOnFailureListener {
Log.e(FIRESTORE, it.toString())
}
}
fun updateDocument(data: Map<String, Any>, documentId: String) {
collectionRef.document(documentId).update(data)
.addOnSuccessListener {
Log.d(FIRESTORE, "Data Updated")
}
.addOnFailureListener {
Log.e(FIRESTORE, it.toString())
}
}
fun updateDocumentSingleField(documentId: String, fieldName: String, value: Any) {
collectionRef.document(documentId).update(fieldName, value)
.addOnSuccessListener {
Log.d(FIRESTORE, "Data Updated")
}
.addOnFailureListener {
Log.e(FIRESTORE, it.toString())
}
}
fun updateMultiCollectionDocument(
data: Map<String, Any?>,
documentIdFirst: String,
collectionName: String,
documentIdSecond: String
) {
collectionRef.document(documentIdFirst).collection(collectionName)
.document(documentIdSecond).update(data)
.addOnSuccessListener {
Log.d(FIRESTORE, "Data Updated")
}
.addOnFailureListener {
Log.e(FIRESTORE, it.toString())
}
}
fun updateMultiCollectionDocumentSingleField(
documentIdFirst: String,
collectionName: String,
documentIdSecond: String,
fieldName: String,
value: Any
) {
collectionRef.document(documentIdFirst).collection(collectionName)
.document(documentIdSecond)
.update(fieldName, value)
.addOnSuccessListener {
Log.d(FIRESTORE, "Data Updated")
}
.addOnFailureListener {
Log.e(FIRESTORE, it.toString())
}
}
fun getDocumentById(documentId: String, firestoreSnapshotListener: FirestoreSnapshotListener) {
collectionRef.document(documentId).addSnapshotListener { snapshot, error ->
if (error != null) {
Log.w(FIRESTORE, "Listen failed.", error)
return@addSnapshotListener
}
if (snapshot != null && snapshot.exists()) {
firestoreSnapshotListener.onSnapshotListener(snapshot)
} else {
Log.d(FIRESTORE, "Current data: null")
}
}
}
fun getDocumentByIdOnce(documentId: String, firestoreEventListener: FirestoreEventListener) {
collectionRef.document(documentId).get()
.addOnSuccessListener {
firestoreEventListener.onSuccess(it)
}
.addOnFailureListener {
firestoreEventListener.onError(it)
}
}
fun getMultiCollectionDocument(
documentIdFirst: String,
collectionName: String,
documentIdSecond: String,
firestoreSnapshotListener: FirestoreSnapshotListener
) {
collectionRef.document(documentIdFirst).collection(collectionName)
.document(documentIdSecond)
.addSnapshotListener { snapshot, error ->
if (error != null) {
Log.w(FIRESTORE, "Listen failed.", error)
return@addSnapshotListener
}
if (snapshot != null && snapshot.exists()) {
firestoreSnapshotListener.onSnapshotListener(snapshot)
} else {
Log.d(FIRESTORE, "Current data: null")
}
}
}
fun getMultiCollectionDocumentBasedOnCondition(
documentIdFirst: String,
collectionName: String,
fieldName: String,
value: Any,
firestoreQuerySnapshotListener: FirestoreQuerySnapshotListener
) {
collectionRef.document(documentIdFirst).collection(collectionName)
.whereEqualTo(fieldName, value).limit(1)
.addSnapshotListener { snapshot, error ->
if (error != null) {
Log.w(FIRESTORE, "Listen failed.", error)
return@addSnapshotListener
}
if (snapshot != null) {
firestoreQuerySnapshotListener.onQuerySnapshotListener(snapshot)
} else {
Log.d(FIRESTORE, "Current data: null")
}
}
}
fun getCollection(
documentIdFirst: String,
collectionName: String,
orderField: String,
firestoreQuerySnapshotListener: FirestoreQuerySnapshotListener
) {
collectionRef.document(documentIdFirst).collection(collectionName).orderBy(orderField, Query.Direction.ASCENDING)
.addSnapshotListener { snapshot, error ->
if (error != null) {
Log.w(FIRESTORE, "Listen failed.", error)
return@addSnapshotListener
}
if (snapshot != null) {
firestoreQuerySnapshotListener.onQuerySnapshotListener(snapshot)
} else {
Log.d(FIRESTORE, "Current data: null")
}
}
}
fun getSingleCallCollection(
documentIdFirst: String,
collectionName: String,
firestoreQuerySnapshotListener: FirestoreQuerySnapshotListener
) {
collectionRef.document(documentIdFirst).collection(collectionName)
.get().addOnSuccessListener {
firestoreQuerySnapshotListener.onQuerySnapshotListener(it)
}
.addOnFailureListener {
Log.e(FIRESTORE, it.toString())
}
}
fun removeDocument(documentId: String) {
collectionRef.document(documentId).delete()
.addOnSuccessListener {
Log.d(FIRESTORE, "$documentId Deleted")
}
.addOnFailureListener {
Log.e(FIRESTORE, it.toString())
}
}
fun removeCollection(documentId: String, collectionName: String) {
collectionRef.document(documentId).collection(collectionName)
.get().addOnSuccessListener {
for(document in it.documents) {
document.reference.delete()
}
}
}
fun removeMultiCollectionDocument(
documentIdFirst: String, collectionName: String, documentIdSecond: String
) {
collectionRef.document(documentIdFirst).collection(collectionName)
.document(documentIdSecond)
.delete()
.addOnSuccessListener {
Log.d(FIRESTORE, "$documentIdSecond Deleted")
}
.addOnFailureListener {
Log.e(FIRESTORE, it.toString())
}
}
}
@prasadshirvandkar
Copy link
Author

import com.google.firebase.firestore.DocumentSnapshot
import java.lang.Exception

interface FirestoreSnapshotListener {
fun onSnapshotListener(documentSnapshot: DocumentSnapshot)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment