Skip to content

Commit e68a01f

Browse files
committed
Realtime API
1 parent 3065e65 commit e68a01f

File tree

3 files changed

+95
-5
lines changed

3 files changed

+95
-5
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/ListenerRegistration.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
package com.google.firebase.firestore;
1616

17+
import android.app.Activity;
18+
import androidx.annotation.NonNull;
19+
import com.google.firebase.firestore.core.ActivityScope;
20+
1721
/** Represents a listener that can be removed by calling {@link #remove()}. */
1822
public interface ListenerRegistration {
1923

@@ -22,4 +26,13 @@ public interface ListenerRegistration {
2226
* call, subsequent calls have no effect.
2327
*/
2428
void remove();
29+
30+
/**
31+
* The listener will be automatically removed during {@link Activity#onStop}.
32+
*
33+
* @param activity The activity to scope the listener to.
34+
*/
35+
default void scope(@NonNull Activity activity) {
36+
ActivityScope.bind(activity, this);
37+
}
2538
}

firebase-firestore/src/main/java/com/google/firebase/firestore/Pipeline.kt

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,12 @@ import com.google.firebase.firestore.pipeline.Stage
5252
import com.google.firebase.firestore.pipeline.UnionStage
5353
import com.google.firebase.firestore.pipeline.UnnestStage
5454
import com.google.firebase.firestore.pipeline.WhereStage
55+
import com.google.firebase.firestore.util.Executors
5556
import com.google.firestore.v1.ExecutePipelineRequest
5657
import com.google.firestore.v1.StructuredPipeline
5758
import com.google.firestore.v1.Value
59+
import java.util.concurrent.Executor
60+
import kotlinx.coroutines.flow.Flow
5861

5962
open class AbstractPipeline
6063
internal constructor(
@@ -775,6 +778,38 @@ internal constructor(
775778

776779
fun execute(options: RealtimePipelineOptions): Task<PipelineSnapshot> = execute(options.options)
777780

781+
fun snapshots(): Flow<RealtimePipelineSnapshot> = snapshots(RealtimePipelineOptions.DEFAULT)
782+
783+
fun snapshots(options: RealtimePipelineOptions): Flow<RealtimePipelineSnapshot> {
784+
throw NotImplementedError()
785+
}
786+
787+
fun addSnapshotListener(listener: EventListener<RealtimePipelineSnapshot>): ListenerRegistration =
788+
addSnapshotListener(
789+
Executors.DEFAULT_CALLBACK_EXECUTOR,
790+
RealtimePipelineOptions.DEFAULT,
791+
listener
792+
)
793+
794+
fun addSnapshotListener(
795+
options: RealtimePipelineOptions,
796+
listener: EventListener<RealtimePipelineSnapshot>
797+
): ListenerRegistration =
798+
addSnapshotListener(Executors.DEFAULT_CALLBACK_EXECUTOR, options, listener)
799+
800+
fun addSnapshotListener(
801+
executor: Executor,
802+
listener: EventListener<RealtimePipelineSnapshot>
803+
): ListenerRegistration = addSnapshotListener(executor, RealtimePipelineOptions.DEFAULT, listener)
804+
805+
fun addSnapshotListener(
806+
executor: Executor,
807+
options: RealtimePipelineOptions,
808+
listener: EventListener<RealtimePipelineSnapshot>
809+
): ListenerRegistration {
810+
throw NotImplementedError()
811+
}
812+
778813
fun limit(limit: Int): RealtimePipeline = append(LimitStage(limit))
779814

780815
fun offset(offset: Int): RealtimePipeline = append(OffsetStage(offset))
@@ -818,8 +853,6 @@ internal constructor(
818853
}
819854
}
820855

821-
/**
822-
*/
823856
class PipelineSnapshot
824857
internal constructor(executionTime: Timestamp, results: List<PipelineResult>) :
825858
Iterable<PipelineResult> {
@@ -833,6 +866,25 @@ internal constructor(executionTime: Timestamp, results: List<PipelineResult>) :
833866
override fun iterator() = results.iterator()
834867
}
835868

869+
class RealtimePipelineSnapshot
870+
internal constructor(
871+
executionTime: Timestamp?,
872+
metadata: SnapshotMetadata,
873+
results: List<PipelineResult>
874+
) : Iterable<PipelineResult> {
875+
876+
/** The time at which the pipeline producing this result is executed. */
877+
val executionTime: Timestamp? = executionTime
878+
879+
/** Metadata about this snapshot, concerning its source and if it has local modifications. */
880+
val metadata: SnapshotMetadata = metadata
881+
882+
/** List of all the results */
883+
val results: List<PipelineResult> = results
884+
885+
override fun iterator() = results.iterator()
886+
}
887+
836888
class PipelineResult
837889
internal constructor(
838890
private val firestore: FirebaseFirestore,

firebase-firestore/src/main/java/com/google/firebase/firestore/pipeline/options.kt

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package com.google.firebase.firestore.pipeline
1616

1717
import com.google.common.collect.ImmutableMap
18+
import com.google.firebase.firestore.DocumentSnapshot
19+
import com.google.firebase.firestore.MetadataChanges
1820
import com.google.firebase.firestore.model.Values
1921
import com.google.firestore.v1.ArrayValue
2022
import com.google.firestore.v1.MapValue
@@ -156,8 +158,31 @@ class PipelineOptions private constructor(options: InternalOptions) :
156158
fun withIndexMode(indexMode: IndexMode): PipelineOptions = with("index_mode", indexMode.value)
157159
}
158160

159-
class RealtimePipelineOptions private constructor(options: InternalOptions) :
160-
AbstractOptions<RealtimePipelineOptions>(options) {
161+
class RealtimePipelineOptions
162+
private constructor(
163+
internal val offlineServerTimestamps: DocumentSnapshot.ServerTimestampBehavior,
164+
internal val metadataChanges: MetadataChanges,
165+
options: InternalOptions
166+
) : AbstractOptions<RealtimePipelineOptions>(options) {
161167

162-
override fun self(options: InternalOptions) = RealtimePipelineOptions(options)
168+
override fun self(options: InternalOptions) =
169+
RealtimePipelineOptions(offlineServerTimestamps, metadataChanges, options)
170+
171+
companion object {
172+
@JvmField
173+
val DEFAULT: RealtimePipelineOptions =
174+
RealtimePipelineOptions(
175+
DocumentSnapshot.ServerTimestampBehavior.NONE,
176+
MetadataChanges.EXCLUDE,
177+
InternalOptions.EMPTY
178+
)
179+
}
180+
181+
fun withOfflineServerTimestamps(
182+
offlineServerTimestamps: DocumentSnapshot.ServerTimestampBehavior
183+
): RealtimePipelineOptions =
184+
RealtimePipelineOptions(offlineServerTimestamps, metadataChanges, options)
185+
186+
fun withMetadataChanges(metadataChanges: MetadataChanges): RealtimePipelineOptions =
187+
RealtimePipelineOptions(offlineServerTimestamps, metadataChanges, options)
163188
}

0 commit comments

Comments
 (0)