Skip to content

Commit

Permalink
feat(firestore): add addCollectionGroupSnapshotListener(...) method (
Browse files Browse the repository at this point in the history
  • Loading branch information
robingenz committed Jun 18, 2024
1 parent fcf75db commit 5ff6573
Show file tree
Hide file tree
Showing 12 changed files with 425 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-doors-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@capacitor-firebase/firestore': minor
---

feat: add `addCollectionGroupSnapshotListener(...)` method
107 changes: 107 additions & 0 deletions packages/firestore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,35 @@ const getCollection = async () => {
return snapshots;
};

const getCollectionGroup = async () => {
const { snapshots } = await FirebaseFirestore.getCollectionGroup({
reference: 'users',
compositeFilter: {
type: 'and',
queryConstraints: [
{
type: 'where',
fieldPath: 'born',
opStr: '==',
value: 1912,
},
],
},
queryConstraints: [
{
type: 'orderBy',
fieldPath: 'born',
directionStr: 'desc',
},
{
type: 'limit',
limit: 10,
},
],
});
return snapshots;
};

const enableNetwork = async () => {
await FirebaseFirestore.enableNetwork();
};
Expand Down Expand Up @@ -176,6 +205,44 @@ const addCollectionSnapshotListener = async () => {
return callbackId;
};

const addCollectionGroupSnapshotListener = async () => {
const callbackId = await FirebaseFirestore.addCollectionGroupSnapshotListener(
{
reference: 'users',
compositeFilter: {
type: 'and',
queryConstraints: [
{
type: 'where',
fieldPath: 'born',
opStr: '==',
value: 1912,
},
],
},
queryConstraints: [
{
type: 'orderBy',
fieldPath: 'born',
directionStr: 'desc',
},
{
type: 'limit',
limit: 10,
},
],
},
(event, error) => {
if (error) {
console.error(error);
} else {
console.log(event);
}
}
);
return callbackId;
};

const removeSnapshotListener = async (callbackId: string) => {
await FirebaseFirestore.removeSnapshotListener({
callbackId,
Expand Down Expand Up @@ -204,6 +271,7 @@ const removeAllListeners = async () => {
* [`disableNetwork()`](#disablenetwork)
* [`addDocumentSnapshotListener(...)`](#adddocumentsnapshotlistener)
* [`addCollectionSnapshotListener(...)`](#addcollectionsnapshotlistener)
* [`addCollectionGroupSnapshotListener(...)`](#addcollectiongroupsnapshotlistener)
* [`removeSnapshotListener(...)`](#removesnapshotlistener)
* [`removeAllListeners()`](#removealllisteners)
* [Interfaces](#interfaces)
Expand Down Expand Up @@ -438,6 +506,26 @@ Adds a listener for collection snapshot events.
--------------------


### addCollectionGroupSnapshotListener(...)

```typescript
addCollectionGroupSnapshotListener<T extends DocumentData = DocumentData>(options: AddCollectionGroupSnapshotListenerOptions, callback: AddCollectionGroupSnapshotListenerCallback<T>) => Promise<CallbackId>
```

Adds a listener for collection group snapshot events.

| Param | Type |
| -------------- | -------------------------------------------------------------------------------------------------------------------------- |
| **`options`** | <code><a href="#addcollectiongroupsnapshotlisteneroptions">AddCollectionGroupSnapshotListenerOptions</a></code> |
| **`callback`** | <code><a href="#addcollectiongroupsnapshotlistenercallback">AddCollectionGroupSnapshotListenerCallback</a>&lt;T&gt;</code> |

**Returns:** <code>Promise&lt;string&gt;</code>

**Since:** 6.1.0

--------------------


### removeSnapshotListener(...)

```typescript
Expand Down Expand Up @@ -659,6 +747,15 @@ Remove all listeners for this plugin.
| **`queryConstraints`** | <code>QueryNonFilterConstraint[]</code> | Narrow or order the set of documents to retrieve, but do not explicitly filter for document fields. | 5.2.0 |


#### AddCollectionGroupSnapshotListenerOptions

| Prop | Type | Description | Since |
| ---------------------- | ----------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------- | ----- |
| **`reference`** | <code>string</code> | The reference as a string, with path components separated by a forward slash (`/`). | 6.1.0 |
| **`compositeFilter`** | <code><a href="#querycompositefilterconstraint">QueryCompositeFilterConstraint</a></code> | The filter to apply. | 6.1.0 |
| **`queryConstraints`** | <code>QueryNonFilterConstraint[]</code> | Narrow or order the set of documents to retrieve, but do not explicitly filter for document fields. | 6.1.0 |


#### RemoveSnapshotListenerOptions

| Prop | Type | Since |
Expand Down Expand Up @@ -713,6 +810,16 @@ Remove all listeners for this plugin.

<code><a href="#getcollectionresult">GetCollectionResult</a>&lt;T&gt;</code>


#### AddCollectionGroupSnapshotListenerCallback

<code>(event: <a href="#addcollectiongroupsnapshotlistenercallbackevent">AddCollectionGroupSnapshotListenerCallbackEvent</a>&lt;T&gt; | null, error: any): void</code>


#### AddCollectionGroupSnapshotListenerCallbackEvent

<code><a href="#getcollectiongroupresult">GetCollectionGroupResult</a>&lt;T&gt;</code>

</docgen-api>

## Changelog
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.google.firebase.firestore.SetOptions;
import com.google.firebase.firestore.WriteBatch;
import io.capawesome.capacitorjs.plugins.firebase.firestore.classes.constraints.QueryCompositeFilterConstraint;
import io.capawesome.capacitorjs.plugins.firebase.firestore.classes.options.AddCollectionGroupSnapshotListenerOptions;
import io.capawesome.capacitorjs.plugins.firebase.firestore.classes.options.AddCollectionSnapshotListenerOptions;
import io.capawesome.capacitorjs.plugins.firebase.firestore.classes.options.AddDocumentOptions;
import io.capawesome.capacitorjs.plugins.firebase.firestore.classes.options.AddDocumentSnapshotListenerOptions;
Expand Down Expand Up @@ -284,6 +285,39 @@ public void addCollectionSnapshotListener(
this.listenerRegistrationMap.put(callbackId, listenerRegistration);
}

public void addCollectionGroupSnapshotListener(
@NonNull AddCollectionGroupSnapshotListenerOptions options,
@NonNull NonEmptyResultCallback callback
) throws Exception {
String reference = options.getReference();
QueryCompositeFilterConstraint compositeFilter = options.getCompositeFilter();
QueryNonFilterConstraint[] queryConstraints = options.getQueryConstraints();
String callbackId = options.getCallbackId();

Query query = getFirebaseFirestoreInstance().collectionGroup(reference);
if (compositeFilter != null) {
Filter filter = compositeFilter.toFilter();
query = query.where(filter);
}
if (queryConstraints.length > 0) {
for (QueryNonFilterConstraint queryConstraint : queryConstraints) {
query = queryConstraint.toQuery(query, getFirebaseFirestoreInstance());
}
}

ListenerRegistration listenerRegistration = query.addSnapshotListener(
(querySnapshot, exception) -> {
if (exception != null) {
callback.error(exception);
} else {
GetCollectionResult result = new GetCollectionResult(querySnapshot);
callback.success(result);
}
}
);
this.listenerRegistrationMap.put(callbackId, listenerRegistration);
}

public void removeSnapshotListener(@NonNull RemoveSnapshotListenerOptions options) {
String callbackId = options.getCallbackId();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;
import io.capawesome.capacitorjs.plugins.firebase.firestore.classes.options.AddCollectionGroupSnapshotListenerOptions;
import io.capawesome.capacitorjs.plugins.firebase.firestore.classes.options.AddCollectionSnapshotListenerOptions;
import io.capawesome.capacitorjs.plugins.firebase.firestore.classes.options.AddDocumentOptions;
import io.capawesome.capacitorjs.plugins.firebase.firestore.classes.options.AddDocumentSnapshotListenerOptions;
Expand Down Expand Up @@ -451,6 +452,48 @@ public void error(Exception exception) {
}
}

@PluginMethod(returnType = PluginMethod.RETURN_CALLBACK)
public void addCollectionGroupSnapshotListener(PluginCall call) {
try {
call.setKeepAlive(true);

String reference = call.getString("reference");
if (reference == null) {
call.reject(ERROR_REFERENCE_MISSING);
return;
}
JSObject compositeFilter = call.getObject("compositeFilter");
JSArray queryConstraints = call.getArray("queryConstraints");
String callbackId = call.getCallbackId();

this.pluginCallMap.put(callbackId, call);

AddCollectionGroupSnapshotListenerOptions options = new AddCollectionGroupSnapshotListenerOptions(
reference,
compositeFilter,
queryConstraints,
callbackId
);
NonEmptyResultCallback callback = new NonEmptyResultCallback() {
@Override
public void success(Result result) {
call.resolve(result.toJSObject());
}

@Override
public void error(Exception exception) {
Logger.error(TAG, exception.getMessage(), exception);
call.reject(exception.getMessage());
}
};

implementation.addCollectionGroupSnapshotListener(options, callback);
} catch (Exception exception) {
Logger.error(TAG, exception.getMessage(), exception);
call.reject(exception.getMessage());
}
}

@PluginMethod
public void removeSnapshotListener(PluginCall call) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.capawesome.capacitorjs.plugins.firebase.firestore.classes.options;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.getcapacitor.JSArray;
import com.getcapacitor.JSObject;
import io.capawesome.capacitorjs.plugins.firebase.firestore.FirebaseFirestoreHelper;
import io.capawesome.capacitorjs.plugins.firebase.firestore.classes.constraints.QueryCompositeFilterConstraint;
import io.capawesome.capacitorjs.plugins.firebase.firestore.interfaces.QueryNonFilterConstraint;
import org.json.JSONException;

public class AddCollectionGroupSnapshotListenerOptions {

@NonNull
private String reference;

@Nullable
private QueryCompositeFilterConstraint compositeFilter;

@NonNull
private QueryNonFilterConstraint[] queryConstraints;

private String callbackId;

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

public String getReference() {
return reference;
}

@Nullable
public QueryCompositeFilterConstraint getCompositeFilter() {
return compositeFilter;
}

@NonNull
public QueryNonFilterConstraint[] getQueryConstraints() {
return queryConstraints;
}

public String getCallbackId() {
return callbackId;
}
}
4 changes: 4 additions & 0 deletions packages/firestore/ios/Plugin.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
7C0070402ABC2DF8000C0F28 /* FirebaseFirestoreHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C00703F2ABC2DF8000C0F28 /* FirebaseFirestoreHelper.swift */; };
7C0070422ABC97E5000C0F28 /* QueryNonFilterConstraint.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C0070412ABC97E5000C0F28 /* QueryNonFilterConstraint.swift */; };
7C0070442ABC9AE9000C0F28 /* QueryFilterConstraint.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C0070432ABC9AE9000C0F28 /* QueryFilterConstraint.swift */; };
7C90A58E2C21519B00A71389 /* AddCollectionGroupSnapshotListenerOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C90A58D2C21519B00A71389 /* AddCollectionGroupSnapshotListenerOptions.swift */; };
7CF0EFE32C207844004D910D /* WriteBatchOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CF0EFE22C207844004D910D /* WriteBatchOptions.swift */; };
7CF0EFE52C20784E004D910D /* WriteBatchOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CF0EFE42C20784E004D910D /* WriteBatchOperation.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -91,6 +92,7 @@
7C00703F2ABC2DF8000C0F28 /* FirebaseFirestoreHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FirebaseFirestoreHelper.swift; sourceTree = "<group>"; };
7C0070412ABC97E5000C0F28 /* QueryNonFilterConstraint.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QueryNonFilterConstraint.swift; sourceTree = "<group>"; };
7C0070432ABC9AE9000C0F28 /* QueryFilterConstraint.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QueryFilterConstraint.swift; sourceTree = "<group>"; };
7C90A58D2C21519B00A71389 /* AddCollectionGroupSnapshotListenerOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddCollectionGroupSnapshotListenerOptions.swift; sourceTree = "<group>"; };
7CF0EFE22C207844004D910D /* WriteBatchOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WriteBatchOptions.swift; sourceTree = "<group>"; };
7CF0EFE42C20784E004D910D /* WriteBatchOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WriteBatchOperation.swift; sourceTree = "<group>"; };
91781294A431A2A7CC6EB714 /* Pods-Plugin.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Plugin.release.xcconfig"; path = "Pods/Target Support Files/Pods-Plugin/Pods-Plugin.release.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -200,6 +202,7 @@
7C00701F2ABC1F42000C0F28 /* GetDocumentOptions.swift */,
7CF0EFE22C207844004D910D /* WriteBatchOptions.swift */,
7CF0EFE42C20784E004D910D /* WriteBatchOperation.swift */,
7C90A58D2C21519B00A71389 /* AddCollectionGroupSnapshotListenerOptions.swift */,
);
path = Options;
sourceTree = "<group>";
Expand Down Expand Up @@ -476,6 +479,7 @@
7C0070122ABC1F03000C0F28 /* AddDocumentResult.swift in Sources */,
7C0070402ABC2DF8000C0F28 /* FirebaseFirestoreHelper.swift in Sources */,
2F98D68224C9AAE500613A4C /* FirebaseFirestore.swift in Sources */,
7C90A58E2C21519B00A71389 /* AddCollectionGroupSnapshotListenerOptions.swift in Sources */,
7C0070222ABC1F4A000C0F28 /* GetDocumentResult.swift in Sources */,
7C0070262ABC1F62000C0F28 /* QueryEndAtConstraint.swift in Sources */,
7C00701C2ABC1F31000C0F28 /* GetCollectionOptions.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Foundation
import Capacitor

@objc public class AddCollectionGroupSnapshotListenerOptions: NSObject {
private var reference: String
private var compositeFilter: QueryCompositeFilterConstraint?
private var queryConstraints: [QueryNonFilterConstraint]
private var callbackId: String

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

func getReference() -> String {
return reference
}

func getCompositeFilter() -> QueryCompositeFilterConstraint? {
return compositeFilter
}

func getQueryConstraints() -> [QueryNonFilterConstraint] {
return queryConstraints
}

func getCallbackId() -> String {
return callbackId
}
}
Loading

0 comments on commit 5ff6573

Please sign in to comment.