Skip to content

Add 'toFlow()' extensions to DocumentSnapshot and Query #1252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
af0f469
Add toFlow extensions to DocumentSnapshot and Query
michgauz Feb 18, 2020
931e73f
Add toFlow extensions to DocumentSnapshot and Query
michgauz Feb 19, 2020
57fbe74
Update API Txt file
michgauz Feb 19, 2020
c87ea63
Implement 'com.google.android.gms:play-services-base' to retrieve Fir…
michgauz Feb 19, 2020
fe0916c
Add MetadataChanges optional parameter
michgauz Feb 25, 2020
15bb8b9
Wrap 'offer()' in runCatching
michgauz Feb 26, 2020
fcfb4b1
Use 'flow' and 'Channel' instead of unstable 'callbackFlow'
michgauz Jun 22, 2020
ad9db4d
pull the version from @svenjacobs
martinbonnin Jun 2, 2022
4d7237d
add "bufferCapacity" as a parameter
martinbonnin Jun 2, 2022
de32290
remove wildcard imports
martinbonnin Jun 2, 2022
cccf86f
use cancel(message, cause)
martinbonnin Jun 6, 2022
c91731a
make compile
martinbonnin Aug 4, 2022
a3d04ac
update public API
martinbonnin Aug 4, 2022
31d1553
fix lint
martinbonnin Aug 4, 2022
2e51a63
return non nullable snapshots
martinbonnin Aug 4, 2022
288ae9a
toFlow -> snapshots
martinbonnin Aug 9, 2022
bcb69e5
remove wrong BuildConfig
martinbonnin Aug 9, 2022
f6e1aeb
add play-services-basement to ktx
martinbonnin Aug 10, 2022
fbd4864
Update firebase-firestore/ktx/src/main/kotlin/com/google/firebase/fir…
martinbonnin Aug 10, 2022
31d3b61
Update firebase-firestore/ktx/src/main/kotlin/com/google/firebase/fir…
martinbonnin Aug 10, 2022
8cd52cd
let the user decide the buffering
martinbonnin Aug 10, 2022
253fbca
remove wildcard import
martinbonnin Aug 11, 2022
692114c
make lint happy
martinbonnin Aug 11, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions firebase-firestore/ktx/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ package com.google.firebase.firestore.ktx {
method @Nullable public static inline <reified T> T getField(@NonNull com.google.firebase.firestore.DocumentSnapshot, @NonNull com.google.firebase.firestore.FieldPath fieldPath);
method @Nullable public static inline <reified T> T getField(@NonNull com.google.firebase.firestore.DocumentSnapshot, @NonNull com.google.firebase.firestore.FieldPath fieldPath, @NonNull com.google.firebase.firestore.DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior);
method @NonNull public static com.google.firebase.firestore.FirebaseFirestore getFirestore(@NonNull com.google.firebase.ktx.Firebase);
method @NonNull public static kotlinx.coroutines.flow.Flow<com.google.firebase.firestore.DocumentSnapshot> snapshots(@NonNull com.google.firebase.firestore.DocumentReference, @NonNull com.google.firebase.firestore.MetadataChanges metadataChanges = com.google.firebase.firestore.MetadataChanges.EXCLUDE);
method @NonNull public static kotlinx.coroutines.flow.Flow<com.google.firebase.firestore.QuerySnapshot> snapshots(@NonNull com.google.firebase.firestore.Query, @NonNull com.google.firebase.firestore.MetadataChanges metadataChanges = com.google.firebase.firestore.MetadataChanges.EXCLUDE);
method @Nullable public static inline <reified T> T toObject(@NonNull com.google.firebase.firestore.DocumentSnapshot);
method @Nullable public static inline <reified T> T toObject(@NonNull com.google.firebase.firestore.DocumentSnapshot, @NonNull com.google.firebase.firestore.DocumentSnapshot.ServerTimestampBehavior serverTimestampBehavior);
method @NonNull public static inline <reified T> T toObject(@NonNull com.google.firebase.firestore.QueryDocumentSnapshot);
Expand Down
2 changes: 2 additions & 0 deletions firebase-firestore/ktx/ktx.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ dependencies {
implementation project(':firebase-common:ktx')
implementation project(':firebase-firestore')
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2'
implementation 'com.google.android.gms:play-services-basement:18.0.0'
testImplementation project(':firebase-database-collection')
testImplementation 'org.mockito:mockito-core:2.25.0'
testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.9.8'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,23 @@ import androidx.annotation.Keep
import com.google.firebase.FirebaseApp
import com.google.firebase.components.Component
import com.google.firebase.components.ComponentRegistrar
import com.google.firebase.firestore.DocumentReference
import com.google.firebase.firestore.DocumentSnapshot
import com.google.firebase.firestore.FieldPath
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.FirebaseFirestoreSettings
import com.google.firebase.firestore.MetadataChanges
import com.google.firebase.firestore.Query
import com.google.firebase.firestore.QueryDocumentSnapshot
import com.google.firebase.firestore.QuerySnapshot
import com.google.firebase.firestore.util.Executors.BACKGROUND_EXECUTOR
import com.google.firebase.ktx.Firebase
import com.google.firebase.platforminfo.LibraryVersionComponent
import kotlinx.coroutines.cancel
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.channels.trySendBlocking
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow

/** Returns the [FirebaseFirestore] instance of the default [FirebaseApp]. */
val Firebase.firestore: FirebaseFirestore
Expand Down Expand Up @@ -162,3 +171,49 @@ class FirebaseFirestoreKtxRegistrar : ComponentRegistrar {
override fun getComponents(): List<Component<*>> =
listOf(LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME))
}

/**
* Starts listening to the document referenced by this `DocumentReference` with the given options and emits its values via a [Flow].
*
* - When the returned flow starts being collected, an [EventListener] will be attached.
* - When the flow completes, the listener will be removed.
*
* @param metadataChanges controls metadata-only changes. Default: [MetadataChanges.EXCLUDE]
*/
fun DocumentReference.snapshots(
metadataChanges: MetadataChanges = MetadataChanges.EXCLUDE
): Flow<DocumentSnapshot> {
return callbackFlow {
val registration = addSnapshotListener(BACKGROUND_EXECUTOR, metadataChanges) { snapshot, exception ->
if (exception != null) {
cancel(message = "Error getting DocumentReference snapshot", cause = exception)
} else if (snapshot != null) {
trySendBlocking(snapshot)
}
}
awaitClose { registration.remove() }
}
}

/**
* Starts listening to this query with the given options and emits its values via a [Flow].
*
* - When the returned flow starts being collected, an [EventListener] will be attached.
* - When the flow completes, the listener will be removed.
*
* @param metadataChanges controls metadata-only changes. Default: [MetadataChanges.EXCLUDE]
*/
fun Query.snapshots(
metadataChanges: MetadataChanges = MetadataChanges.EXCLUDE
): Flow<QuerySnapshot> {
return callbackFlow {
val registration = addSnapshotListener(BACKGROUND_EXECUTOR, metadataChanges) { snapshot, exception ->
if (exception != null) {
cancel(message = "Error getting Query snapshot", cause = exception)
} else if (snapshot != null) {
trySendBlocking(snapshot)
}
}
awaitClose { registration.remove() }
}
}