Skip to content

feat(rtdb, firestore): create values() and dataObjects() Flows #4804

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 8 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions firebase-database/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
# Unreleased
* [unchanged] Updated to accommodate the release of the updated
[database] Kotlin extensions library.

## Kotlin
* [feature] Added
[`Query.values<T>()`](/docs/reference/kotlin/com/google/firebase/database/ktx/package-summary#values)
Kotlin Flows to listen for realtime updates and convert its values to a specific type.

# 20.1.0
* [unchanged] Updated to accommodate the release of the updated
Expand Down
1 change: 1 addition & 0 deletions firebase-database/ktx/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ package com.google.firebase.database.ktx {
method @NonNull public static kotlinx.coroutines.flow.Flow<com.google.firebase.database.DataSnapshot> getSnapshots(@NonNull com.google.firebase.database.Query);
method public static inline <reified T> T getValue(@NonNull com.google.firebase.database.DataSnapshot);
method public static inline <reified T> T getValue(@NonNull com.google.firebase.database.MutableData);
method public static inline <reified T> kotlinx.coroutines.flow.Flow<? extends T> values(@NonNull com.google.firebase.database.Query);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ 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
import kotlinx.coroutines.flow.map

/** Returns the [FirebaseDatabase] instance of the default [FirebaseApp]. */
val Firebase.database: FirebaseDatabase
Expand Down Expand Up @@ -127,6 +129,16 @@ val Query.childEvents
awaitClose { removeEventListener(listener) }
}

/**
* Starts listening to this query and emits its values converted to a POJO via a [Flow].
*
* - When the returned flow starts being collected, a [ValueEventListener] will be attached.
* - When the flow completes, the listener will be removed.
*/
inline fun <reified T : Any> Query.values(): Flow<T?> {
return snapshots.map { it.getValue(T::class.java) }
}

internal const val LIBRARY_NAME: String = "fire-db-ktx"

/** @suppress */
Expand Down
21 changes: 20 additions & 1 deletion firebase-firestore/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
# Unreleased
* [feature] Add support for disjunctions in queries (`OR` queries).
* [fixed] Fixed stack overflow caused by deeply nested server timestamps (#4702).

## Kotlin
* [feature] Added
[`Query.dataObjects<T>()`](/docs/reference/kotlin/com/google/firebase/firestore/ktx/package-summary#dataObjects)
and
[`DocumentReference.dataObjects<T>()`](/docs/reference/kotlin/com/google/firebase/firestore/ktx/package-summary#dataObjects_1)
Kotlin Flows to listen for realtime updates and convert its values to a specific type.

# 24.4.5
* [feature] Add support for disjunctions in queries (`OR` queries).

## Kotlin
The Kotlin extensions library transitively includes the updated
`firebase-firestore` library. The Kotlin extensions library has no additional
updates.

# 24.4.4
* [changed] Relaxed certain query validations performed by the SDK (#4231).
* [changed] Updated grpc to 1.52.1 and javalite, protoc, protobufjavautil to 3.21.11.

## Kotlin
The Kotlin extensions library transitively includes the updated
`firebase-firestore` library. The Kotlin extensions library has no additional
updates.

# 24.4.3
* [fixed] Fixed a potential high-memory usage issue.
* [fixed] Fixed an issue that stopped some performance optimization from being
Expand Down
2 changes: 2 additions & 0 deletions firebase-firestore/ktx/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
package com.google.firebase.firestore.ktx {

public final class FirestoreKt {
method public static inline <reified T> kotlinx.coroutines.flow.Flow<? extends T> dataObjects(@NonNull com.google.firebase.firestore.DocumentReference, @NonNull com.google.firebase.firestore.MetadataChanges metadataChanges = com.google.firebase.firestore.MetadataChanges.EXCLUDE);
method public static inline <reified T> kotlinx.coroutines.flow.Flow<? extends java.util.List<? extends T>> dataObjects(@NonNull com.google.firebase.firestore.Query, @NonNull com.google.firebase.firestore.MetadataChanges metadataChanges = com.google.firebase.firestore.MetadataChanges.EXCLUDE);
method @NonNull public static com.google.firebase.firestore.FirebaseFirestore firestore(@NonNull com.google.firebase.ktx.Firebase, @NonNull com.google.firebase.FirebaseApp app);
method @NonNull public static com.google.firebase.firestore.FirebaseFirestoreSettings firestoreSettings(@NonNull kotlin.jvm.functions.Function1<? super com.google.firebase.firestore.FirebaseFirestoreSettings.Builder,kotlin.Unit> init);
method public static inline <reified T> T getField(@NonNull com.google.firebase.firestore.DocumentSnapshot, @NonNull String field);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.channels.trySendBlocking
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.map

/** Returns the [FirebaseFirestore] instance of the default [FirebaseApp]. */
val Firebase.firestore: FirebaseFirestore
Expand Down Expand Up @@ -240,3 +241,31 @@ fun Query.snapshots(
awaitClose { registration.remove() }
}
}

/**
* Starts listening to this query with the given options and emits its values converted to a POJO
* 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]
* @param T The type of the object to convert to.
*/
inline fun <reified T : Any> Query.dataObjects(
metadataChanges: MetadataChanges = MetadataChanges.EXCLUDE
): Flow<List<T>> = snapshots(metadataChanges).map { it.toObjects(T::class.java) }

/**
* Starts listening to the document referenced by this `DocumentReference` with the given options
* and emits its values converted to a POJO 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]
* @param T The type of the object to convert to.
*/
inline fun <reified T : Any> DocumentReference.dataObjects(
metadataChanges: MetadataChanges = MetadataChanges.EXCLUDE
): Flow<T?> = snapshots(metadataChanges).map { it.toObject(T::class.java) }