Skip to content

Commit 95ce611

Browse files
authored
Merge fcd6c06 into eaefea8
2 parents eaefea8 + fcd6c06 commit 95ce611

File tree

6 files changed

+71
-1
lines changed

6 files changed

+71
-1
lines changed

firebase-database/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
# Unreleased
2+
* [unchanged] Updated to accommodate the release of the updated
3+
[database] Kotlin extensions library.
4+
5+
## Kotlin
6+
* [feature] Added
7+
[`Query.values<T>()`](/docs/reference/kotlin/com/google/firebase/database/ktx/package-summary#values)
8+
Kotlin Flows to listen for realtime updates and convert its values to a specific type.
29

310
# 20.1.0
411
* [unchanged] Updated to accommodate the release of the updated

firebase-database/ktx/api.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ package com.google.firebase.database.ktx {
5454
method @NonNull public static kotlinx.coroutines.flow.Flow<com.google.firebase.database.DataSnapshot> getSnapshots(@NonNull com.google.firebase.database.Query);
5555
method public static inline <reified T> T getValue(@NonNull com.google.firebase.database.DataSnapshot);
5656
method public static inline <reified T> T getValue(@NonNull com.google.firebase.database.MutableData);
57+
method public static inline <reified T> kotlinx.coroutines.flow.Flow<? extends T> values(@NonNull com.google.firebase.database.Query);
5758
}
5859

5960
}

firebase-database/ktx/src/main/kotlin/com/google/firebase/database/ktx/Database.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ import com.google.firebase.platforminfo.LibraryVersionComponent
3131
import kotlinx.coroutines.cancel
3232
import kotlinx.coroutines.channels.awaitClose
3333
import kotlinx.coroutines.channels.trySendBlocking
34+
import kotlinx.coroutines.flow.Flow
3435
import kotlinx.coroutines.flow.callbackFlow
36+
import kotlinx.coroutines.flow.map
3537

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

132+
/**
133+
* Starts listening to this query and emits its values converted to a POJO via a [Flow].
134+
*
135+
* - When the returned flow starts being collected, a [ValueEventListener] will be attached.
136+
* - When the flow completes, the listener will be removed.
137+
*/
138+
inline fun <reified T : Any> Query.values(): Flow<T?> {
139+
return snapshots.map { it.getValue(T::class.java) }
140+
}
141+
130142
internal const val LIBRARY_NAME: String = "fire-db-ktx"
131143

132144
/** @suppress */

firebase-firestore/CHANGELOG.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
11
# Unreleased
2-
* [feature] Add support for disjunctions in queries (`OR` queries).
32
* [fixed] Fixed stack overflow caused by deeply nested server timestamps (#4702).
43

4+
## Kotlin
5+
* [feature] Added
6+
[`Query.dataObjects<T>()`](/docs/reference/kotlin/com/google/firebase/firestore/ktx/package-summary#dataObjects)
7+
and
8+
[`DocumentReference.dataObjects<T>()`](/docs/reference/kotlin/com/google/firebase/firestore/ktx/package-summary#dataObjects_1)
9+
Kotlin Flows to listen for realtime updates and convert its values to a specific type.
10+
11+
# 24.4.5
12+
* [feature] Add support for disjunctions in queries (`OR` queries).
13+
14+
## Kotlin
15+
The Kotlin extensions library transitively includes the updated
16+
`firebase-firestore` library. The Kotlin extensions library has no additional
17+
updates.
18+
519
# 24.4.4
620
* [changed] Relaxed certain query validations performed by the SDK (#4231).
721
* [changed] Updated grpc to 1.52.1 and javalite, protoc, protobufjavautil to 3.21.11.
822

23+
## Kotlin
24+
The Kotlin extensions library transitively includes the updated
25+
`firebase-firestore` library. The Kotlin extensions library has no additional
26+
updates.
27+
928
# 24.4.3
1029
* [fixed] Fixed a potential high-memory usage issue.
1130
* [fixed] Fixed an issue that stopped some performance optimization from being

firebase-firestore/ktx/api.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
package com.google.firebase.firestore.ktx {
33

44
public final class FirestoreKt {
5+
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);
6+
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);
57
method @NonNull public static com.google.firebase.firestore.FirebaseFirestore firestore(@NonNull com.google.firebase.ktx.Firebase, @NonNull com.google.firebase.FirebaseApp app);
68
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);
79
method public static inline <reified T> T getField(@NonNull com.google.firebase.firestore.DocumentSnapshot, @NonNull String field);

firebase-firestore/ktx/src/main/kotlin/com/google/firebase/firestore/ktx/Firestore.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import kotlinx.coroutines.channels.awaitClose
3535
import kotlinx.coroutines.channels.trySendBlocking
3636
import kotlinx.coroutines.flow.Flow
3737
import kotlinx.coroutines.flow.callbackFlow
38+
import kotlinx.coroutines.flow.map
3839

3940
/** Returns the [FirebaseFirestore] instance of the default [FirebaseApp]. */
4041
val Firebase.firestore: FirebaseFirestore
@@ -240,3 +241,31 @@ fun Query.snapshots(
240241
awaitClose { registration.remove() }
241242
}
242243
}
244+
245+
/**
246+
* Starts listening to this query with the given options and emits its values converted to a POJO
247+
* via a [Flow].
248+
*
249+
* - When the returned flow starts being collected, an [EventListener] will be attached.
250+
* - When the flow completes, the listener will be removed.
251+
*
252+
* @param metadataChanges controls metadata-only changes. Default: [MetadataChanges.EXCLUDE]
253+
* @param T The type of the object to convert to.
254+
*/
255+
inline fun <reified T : Any> Query.dataObjects(
256+
metadataChanges: MetadataChanges = MetadataChanges.EXCLUDE
257+
): Flow<List<T>> = snapshots(metadataChanges).map { it.toObjects(T::class.java) }
258+
259+
/**
260+
* Starts listening to the document referenced by this `DocumentReference` with the given options
261+
* and emits its values converted to a POJO via a [Flow].
262+
*
263+
* - When the returned flow starts being collected, an [EventListener] will be attached.
264+
* - When the flow completes, the listener will be removed.
265+
*
266+
* @param metadataChanges controls metadata-only changes. Default: [MetadataChanges.EXCLUDE]
267+
* @param T The type of the object to convert to.
268+
*/
269+
inline fun <reified T : Any> DocumentReference.dataObjects(
270+
metadataChanges: MetadataChanges = MetadataChanges.EXCLUDE
271+
): Flow<T?> = snapshots(metadataChanges).map { it.toObject(T::class.java) }

0 commit comments

Comments
 (0)