Last active
June 22, 2019 09:13
-
-
Save vitoksmile/c9940b890f3b5654df434fb2baa0c67c to your computer and use it in GitHub Desktop.
Custom LiveData to works with FirebaseFirestore snapshots
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.lifecycle.LiveData | |
import com.google.firebase.firestore.DocumentSnapshot | |
import com.google.firebase.firestore.FirebaseFirestore | |
import com.google.firebase.firestore.ListenerRegistration | |
import com.google.firebase.firestore.QuerySnapshot | |
/** | |
* Custom [LiveData] to works with [FirebaseFirestore] snapshots | |
* | |
* @param collection The current collection's name | |
* @param transformer Transform [DocumentSnapshot] to [T] | |
* @param query Query conditions | |
*/ | |
class FirestoreLiveData<T : Any>( | |
private val collection: String, | |
private val transformer: Transformer<T>, | |
private val query: (CollectionReference.() -> Query)? = null | |
) : LiveData<List<T>>() { | |
interface Transformer<T> { | |
fun transform(document: DocumentSnapshot): T | |
} | |
/** | |
* Firebase Firestore | |
*/ | |
private val db by lazy { FirebaseFirestore.getInstance() } | |
/** | |
* Snapshot listener | |
*/ | |
private var listener: ListenerRegistration? = null | |
override fun onActive() { | |
subscribe() | |
} | |
override fun onInactive() { | |
unSubscribe() | |
} | |
/** | |
* Subscribe to snapshot updates | |
*/ | |
private fun subscribe() { | |
unSubscribe() | |
listener = db.collection(collection) | |
.run { | |
query?.invoke(this) ?: this | |
} | |
.addSnapshotListener { querySnapshot, firebaseFirestoreException -> | |
querySnapshot?.let(this::onSnapshotGot) | |
} | |
} | |
private fun onSnapshotGot(snapshot: QuerySnapshot) { | |
value = snapshot.documents.mapNotNull { | |
try { | |
transformer.transform(it) | |
} catch (error: Throwable) { | |
error.printStackTrace() | |
null | |
} | |
} | |
} | |
/** | |
* Unsubscribe from updates | |
*/ | |
private fun unSubscribe() { | |
listener?.remove() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment