Skip to content
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

feat(firestore): include metadata property on snapshot (#567) #707

Merged
merged 7 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.Filter;
import com.google.firebase.firestore.ListenerRegistration;
import com.google.firebase.firestore.MetadataChanges;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.SetOptions;
import com.google.firebase.firestore.WriteBatch;
Expand Down Expand Up @@ -244,6 +245,7 @@ public void addDocumentSnapshotListener(@NonNull AddDocumentSnapshotListenerOpti
ListenerRegistration listenerRegistration = getFirebaseFirestoreInstance()
.document(reference)
.addSnapshotListener(
options.isIncludeMetadataChanges() ? MetadataChanges.INCLUDE : MetadataChanges.EXCLUDE,
(documentSnapshot, exception) -> {
if (exception != null) {
callback.error(exception);
Expand Down Expand Up @@ -277,6 +279,7 @@ public void addCollectionSnapshotListener(
}

ListenerRegistration listenerRegistration = query.addSnapshotListener(
options.isIncludeMetadataChanges() ? MetadataChanges.INCLUDE : MetadataChanges.EXCLUDE,
(querySnapshot, exception) -> {
if (exception != null) {
callback.error(exception);
Expand Down Expand Up @@ -310,6 +313,7 @@ public void addCollectionGroupSnapshotListener(
}

ListenerRegistration listenerRegistration = query.addSnapshotListener(
options.isIncludeMetadataChanges() ? MetadataChanges.INCLUDE : MetadataChanges.EXCLUDE,
(querySnapshot, exception) -> {
if (exception != null) {
callback.error(exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import androidx.annotation.Nullable;
import com.getcapacitor.JSArray;
import com.getcapacitor.JSObject;
import com.google.firebase.firestore.DocumentSnapshot;
import io.capawesome.capacitorjs.plugins.firebase.firestore.classes.constraints.QueryCompositeFilterConstraint;
import io.capawesome.capacitorjs.plugins.firebase.firestore.classes.constraints.QueryEndAtConstraint;
import io.capawesome.capacitorjs.plugins.firebase.firestore.classes.constraints.QueryLimitConstraint;
Expand Down Expand Up @@ -126,4 +127,11 @@ private static JSArray createJSArrayFromArrayList(ArrayList arrayList) {
}
return array;
}

public static JSObject createSnapshotMetadataResult(DocumentSnapshot snapshot) {
final JSObject obj = new JSObject();
obj.put("fromCache", snapshot.getMetadata().isFromCache());
obj.put("hasPendingWrites", snapshot.getMetadata().hasPendingWrites());
return obj;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -404,11 +404,16 @@ public void addDocumentSnapshotListener(PluginCall call) {
call.reject(ERROR_REFERENCE_MISSING);
return;
}
Boolean includeMetadataChanges = call.getBoolean("includeMetadataChanges");
String callbackId = call.getCallbackId();

this.pluginCallMap.put(callbackId, call);

AddDocumentSnapshotListenerOptions options = new AddDocumentSnapshotListenerOptions(reference, callbackId);
AddDocumentSnapshotListenerOptions options = new AddDocumentSnapshotListenerOptions(
reference,
includeMetadataChanges,
callbackId
);
NonEmptyResultCallback callback = new NonEmptyResultCallback() {
@Override
public void success(Result result) {
Expand Down Expand Up @@ -441,6 +446,7 @@ public void addCollectionSnapshotListener(PluginCall call) {
}
JSObject compositeFilter = call.getObject("compositeFilter");
JSArray queryConstraints = call.getArray("queryConstraints");
Boolean includeMetadataChanges = call.getBoolean("includeMetadataChanges");
String callbackId = call.getCallbackId();

this.pluginCallMap.put(callbackId, call);
Expand All @@ -449,6 +455,7 @@ public void addCollectionSnapshotListener(PluginCall call) {
reference,
compositeFilter,
queryConstraints,
includeMetadataChanges,
callbackId
);
NonEmptyResultCallback callback = new NonEmptyResultCallback() {
Expand Down Expand Up @@ -483,6 +490,7 @@ public void addCollectionGroupSnapshotListener(PluginCall call) {
}
JSObject compositeFilter = call.getObject("compositeFilter");
JSArray queryConstraints = call.getArray("queryConstraints");
Boolean includeMetadataChanges = call.getBoolean("includeMetadataChanges");
String callbackId = call.getCallbackId();

this.pluginCallMap.put(callbackId, call);
Expand All @@ -491,6 +499,7 @@ public void addCollectionGroupSnapshotListener(PluginCall call) {
reference,
compositeFilter,
queryConstraints,
includeMetadataChanges,
callbackId
);
NonEmptyResultCallback callback = new NonEmptyResultCallback() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@ public class AddCollectionGroupSnapshotListenerOptions {

private String callbackId;

private boolean includeMetadataChanges;

public AddCollectionGroupSnapshotListenerOptions(
String reference,
@Nullable JSObject compositeFilter,
@Nullable JSArray queryConstraints,
@Nullable Boolean includeMetadataChanges,
String callbackId
) throws JSONException {
this.reference = reference;
this.compositeFilter = FirebaseFirestoreHelper.createQueryCompositeFilterConstraintFromJSObject(compositeFilter);
this.queryConstraints = FirebaseFirestoreHelper.createQueryNonFilterConstraintArrayFromJSArray(queryConstraints);
this.includeMetadataChanges = includeMetadataChanges == null ? false : includeMetadataChanges;
this.callbackId = callbackId;
}

Expand All @@ -48,6 +52,10 @@ public QueryNonFilterConstraint[] getQueryConstraints() {
return queryConstraints;
}

public boolean isIncludeMetadataChanges() {
return includeMetadataChanges;
}

public String getCallbackId() {
return callbackId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ public class AddCollectionSnapshotListenerOptions {

private String callbackId;

private final boolean includeMetadataChanges;

public AddCollectionSnapshotListenerOptions(
String reference,
@Nullable JSObject compositeFilter,
@Nullable JSArray queryConstraints,
@Nullable Boolean includeMetadataChanges,
String callbackId
) throws JSONException {
this.reference = reference;
this.compositeFilter = FirebaseFirestoreHelper.createQueryCompositeFilterConstraintFromJSObject(compositeFilter);
this.queryConstraints = FirebaseFirestoreHelper.createQueryNonFilterConstraintArrayFromJSArray(queryConstraints);
this.includeMetadataChanges = includeMetadataChanges == null ? false : includeMetadataChanges;
this.callbackId = callbackId;
}

Expand All @@ -49,6 +53,10 @@ public QueryNonFilterConstraint[] getQueryConstraints() {
return queryConstraints;
}

public boolean isIncludeMetadataChanges() {
return includeMetadataChanges;
}

public String getCallbackId() {
return callbackId;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
package io.capawesome.capacitorjs.plugins.firebase.firestore.classes.options;

import androidx.annotation.Nullable;

public class AddDocumentSnapshotListenerOptions {

private String reference;
private final boolean includeMetadataChanges;
private String callbackId;

public AddDocumentSnapshotListenerOptions(String reference, String callbackId) {
public AddDocumentSnapshotListenerOptions(String reference, @Nullable Boolean includeMetadataChanges, String callbackId) {
this.reference = reference;
this.includeMetadataChanges = includeMetadataChanges == null ? false : includeMetadataChanges;
this.callbackId = callbackId;
}

public String getReference() {
return reference;
}

public boolean isIncludeMetadataChanges() {
return includeMetadataChanges;
}

public String getCallbackId() {
return callbackId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public JSObject toJSObject() {
} else {
snapshotResult.put("data", snapshotDataResult);
}
snapshotResult.put("metadata", FirebaseFirestoreHelper.createSnapshotMetadataResult(document));
snapshotsResult.put(snapshotResult);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public JSObject toJSObject() {
} else {
snapshotResult.put("data", snapshotDataResult);
}
snapshotResult.put("metadata", FirebaseFirestoreHelper.createSnapshotMetadataResult(document));
snapshotsResult.put(snapshotResult);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public JSObject toJSObject() {
} else {
snapshotResult.put("data", snapshotDataResult);
}
snapshotResult.put("metadata", FirebaseFirestoreHelper.createSnapshotMetadataResult(documentSnapshot));

JSObject result = new JSObject();
result.put("snapshot", snapshotResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import Capacitor
private var reference: String
private var compositeFilter: QueryCompositeFilterConstraint?
private var queryConstraints: [QueryNonFilterConstraint]
private var includeMetadataChanges: Bool
private var callbackId: String

init(reference: String, compositeFilter: JSObject?, queryConstraints: [JSObject]?, callbackId: String) {
init(reference: String, compositeFilter: JSObject?, queryConstraints: [JSObject]?, includeMetadataChanges: Bool, callbackId: String) {
self.reference = reference
self.compositeFilter = FirebaseFirestoreHelper.createQueryCompositeFilterConstraintFromJSObject(compositeFilter)
self.queryConstraints = FirebaseFirestoreHelper.createQueryNonFilterConstraintArrayFromJSArray(queryConstraints)
self.includeMetadataChanges = includeMetadataChanges
self.callbackId = callbackId
}

Expand All @@ -26,6 +28,10 @@ import Capacitor
return queryConstraints
}

func getIncludeMetadataChanges() -> Bool {
return includeMetadataChanges
}

func getCallbackId() -> String {
return callbackId
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import Capacitor
private var reference: String
private var compositeFilter: QueryCompositeFilterConstraint?
private var queryConstraints: [QueryNonFilterConstraint]
private var includeMetadataChanges: Bool
private var callbackId: String

init(reference: String, compositeFilter: JSObject?, queryConstraints: [JSObject]?, callbackId: String) {
init(reference: String, compositeFilter: JSObject?, queryConstraints: [JSObject]?, includeMetadataChanges: Bool, callbackId: String) {
self.reference = reference
self.compositeFilter = FirebaseFirestoreHelper.createQueryCompositeFilterConstraintFromJSObject(compositeFilter)
self.queryConstraints = FirebaseFirestoreHelper.createQueryNonFilterConstraintArrayFromJSArray(queryConstraints)
self.includeMetadataChanges = includeMetadataChanges
self.callbackId = callbackId
}

Expand All @@ -26,6 +28,10 @@ import Capacitor
return queryConstraints
}

func getIncludeMetadataChanges() -> Bool {
return includeMetadataChanges
}

func getCallbackId() -> String {
return callbackId
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ import Foundation

@objc public class AddDocumentSnapshotListenerOptions: NSObject {
private var reference: String
private var includeMetadataChanges: Bool
private var callbackId: String

init(reference: String, callbackId: String) {
init(reference: String, includeMetadataChanges: Bool, callbackId: String) {
self.reference = reference
self.includeMetadataChanges = includeMetadataChanges
self.callbackId = callbackId
}

func getReference() -> String {
return reference
}

func getIncludeMetadataChanges() -> Bool {
return includeMetadataChanges
}

func getCallbackId() -> String {
return callbackId
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ import Capacitor
} else {
snapshotResult["data"] = NSNull()
}

var metadata = JSObject()
metadata["fromCache"] = documentSnapshot.metadata.isFromCache
metadata["hasPendingWrites"] = documentSnapshot.metadata.hasPendingWrites
snapshotResult["metadata"] = metadata

snapshotsResult.append(snapshotResult)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ import Capacitor
} else {
snapshotResult["data"] = NSNull()
}

var metadata = JSObject()
metadata["fromCache"] = documentSnapshot.metadata.isFromCache
metadata["hasPendingWrites"] = documentSnapshot.metadata.hasPendingWrites
snapshotResult["metadata"] = metadata

snapshotsResult.append(snapshotResult)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ import Capacitor
snapshotResult["data"] = NSNull()
}

var metadata = JSObject()
metadata["fromCache"] = documentSnapshot.metadata.isFromCache
metadata["hasPendingWrites"] = documentSnapshot.metadata.hasPendingWrites
snapshotResult["metadata"] = metadata

var result = JSObject()
result["snapshot"] = snapshotResult
return result as AnyObject
Expand Down
9 changes: 6 additions & 3 deletions packages/firestore/ios/Plugin/FirebaseFirestorePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,15 @@ public class FirebaseFirestorePlugin: CAPPlugin {
call.reject(errorReferenceMissing)
return
}
let includeMetadataChanges = call.getBool("includeMetadataChanges", false)
guard let callbackId = call.callbackId else {
call.reject(errorCallbackIdMissing)
return
}

self.pluginCallMap[callbackId] = call

let options = AddDocumentSnapshotListenerOptions(reference: reference, callbackId: callbackId)
let options = AddDocumentSnapshotListenerOptions(reference: reference, includeMetadataChanges: includeMetadataChanges, callbackId: callbackId)

implementation?.addDocumentSnapshotListener(options, completion: { result, error in
if let error = error {
Expand All @@ -270,14 +271,15 @@ public class FirebaseFirestorePlugin: CAPPlugin {
}
let compositeFilter = call.getObject("compositeFilter")
let queryConstraints = call.getArray("queryConstraints", JSObject.self)
let includeMetadataChanges = call.getBool("includeMetadataChanges", false)
guard let callbackId = call.callbackId else {
call.reject(errorCallbackIdMissing)
return
}

self.pluginCallMap[callbackId] = call

let options = AddCollectionSnapshotListenerOptions(reference: reference, compositeFilter: compositeFilter, queryConstraints: queryConstraints, callbackId: callbackId)
let options = AddCollectionSnapshotListenerOptions(reference: reference, compositeFilter: compositeFilter, queryConstraints: queryConstraints, includeMetadataChanges: includeMetadataChanges, callbackId: callbackId)

do {
implementation?.addCollectionSnapshotListener(options, completion: { result, error in
Expand Down Expand Up @@ -305,14 +307,15 @@ public class FirebaseFirestorePlugin: CAPPlugin {
}
let compositeFilter = call.getObject("compositeFilter")
let queryConstraints = call.getArray("queryConstraints", JSObject.self)
let includeMetadataChanges = call.getBool("includeMetadataChanges", false)
guard let callbackId = call.callbackId else {
call.reject(errorCallbackIdMissing)
return
}

self.pluginCallMap[callbackId] = call

let options = AddCollectionGroupSnapshotListenerOptions(reference: reference, compositeFilter: compositeFilter, queryConstraints: queryConstraints, callbackId: callbackId)
let options = AddCollectionGroupSnapshotListenerOptions(reference: reference, compositeFilter: compositeFilter, queryConstraints: queryConstraints, includeMetadataChanges: includeMetadataChanges, callbackId: callbackId)

do {
implementation?.addCollectionGroupSnapshotListener(options, completion: { result, error in
Expand Down
2 changes: 2 additions & 0 deletions packages/firestore/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ export interface SnapshotListenerOptions {
* Set the source the query listens to.
* The source `default` listens to both cache and server.
*
* Only available for Web.
*
* @since 6.2.0
* @default "default"
*/
Expand Down