From ea4091b7eee972456f6df0b1bf00c19152f69565 Mon Sep 17 00:00:00 2001 From: milaGGL <107142260+milaGGL@users.noreply.github.com> Date: Tue, 28 Nov 2023 20:49:08 -0500 Subject: [PATCH 1/8] demo code --- .../API/FIRFirestoreSourceTests.mm | 14 +++++ Firestore/Source/API/FIRDocumentReference.mm | 7 +++ Firestore/Source/API/FIRQuery.mm | 7 +++ .../Source/API/FIRSnapshotListenOptions.mm | 58 +++++++++++++++++++ .../FirebaseFirestore/FIRDocumentReference.h | 7 +++ .../Public/FirebaseFirestore/FIRQuery.h | 7 +++ .../FIRSnapshotListenOptions.h | 41 +++++++++++++ .../FirebaseFirestore/FirebaseFirestore.h | 1 + 8 files changed, 142 insertions(+) create mode 100644 Firestore/Source/API/FIRSnapshotListenOptions.mm create mode 100644 Firestore/Source/Public/FirebaseFirestore/FIRSnapshotListenOptions.h diff --git a/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm b/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm index 8585e4bd91d5..d8905641a0ef 100644 --- a/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm +++ b/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm @@ -14,6 +14,7 @@ * limitations under the License. */ +#import #import #import @@ -700,4 +701,17 @@ - (void)testGetNonExistingCollectionWhileOfflineServerOnly { [self awaitExpectations]; } +// +- (void)Demo_addSnapshotListenerWithDefaultListenOptions { + FIRCollectionReference *collection = [self.db collectionWithPath:@"cities"]; + FIRQuery *query = [collection queryWhereField:@"state" isEqualTo:@ "CA"]; + FIRSnapshotListenOptions *options = [FIRSnapshotListenOptions defaultOptions]; + + [query addSnapshotListenerWithOptions:options + listener:^(FIRQuerySnapshot *snapshot, NSError *error) { + XCTAssertNil(error); + XCTAssertEqual(snapshot.count, 0); + }]; +} + @end diff --git a/Firestore/Source/API/FIRDocumentReference.mm b/Firestore/Source/API/FIRDocumentReference.mm index b6f326ee8d97..faadf1756eea 100644 --- a/Firestore/Source/API/FIRDocumentReference.mm +++ b/Firestore/Source/API/FIRDocumentReference.mm @@ -212,6 +212,13 @@ - (void)getDocumentWithSource:(FIRFirestoreSource)source return [self addSnapshotListenerInternalWithOptions:options listener:listener]; } +// TODO MILA +- (id)addSnapshotListenerWithOptions:(FIRSnapshotListenOptions *)options + listener:(FIRDocumentSnapshotBlock)listener { + ListenOptions listenOptions = ListenOptions::FromIncludeMetadataChanges(false); + return [self addSnapshotListenerInternalWithOptions:listenOptions listener:listener]; +} + - (id)addSnapshotListenerInternalWithOptions:(ListenOptions)internalOptions listener:(FIRDocumentSnapshotBlock) listener { diff --git a/Firestore/Source/API/FIRQuery.mm b/Firestore/Source/API/FIRQuery.mm index 985181830498..ab3ef0992ed0 100644 --- a/Firestore/Source/API/FIRQuery.mm +++ b/Firestore/Source/API/FIRQuery.mm @@ -191,6 +191,13 @@ - (void)getDocumentsWithSource:(FIRFirestoreSource)publicSource return [self addSnapshotListenerInternalWithOptions:options listener:listener]; } +// TODO MILA +- (id)addSnapshotListenerWithOptions:(FIRSnapshotListenOptions *)options + listener:(FIRQuerySnapshotBlock)listener { + auto listenOptions = ListenOptions::FromIncludeMetadataChanges(false); + return [self addSnapshotListenerInternalWithOptions:listenOptions listener:listener]; +} + - (id)addSnapshotListenerInternalWithOptions:(ListenOptions)internalOptions listener: (FIRQuerySnapshotBlock)listener { diff --git a/Firestore/Source/API/FIRSnapshotListenOptions.mm b/Firestore/Source/API/FIRSnapshotListenOptions.mm new file mode 100644 index 000000000000..9486ab6c365b --- /dev/null +++ b/Firestore/Source/API/FIRSnapshotListenOptions.mm @@ -0,0 +1,58 @@ +/* + * Copyright 2023 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#import "FIRSnapshotListenOptions.h" + +#import + +#include +#include + +// #include "Firestore/core/src/api/firestore.h" +// #include "Firestore/core/src/util/exception.h" + +NS_ASSUME_NONNULL_BEGIN + +// using firebase::firestore::api::kDefaultTransactionMaxAttempts; +// using firebase::firestore::util::ThrowInvalidArgument; + +@implementation FIRSnapshotListenOptions + +- (instancetype)init:(FIRListenSource)source includeMetadataChanges:(BOOL)includeMetadataChanges { + self = [super init]; + if (self) { + _source = source; + _includeMetadataChanges = includeMetadataChanges; + } + return self; +} + ++ (FIRSnapshotListenOptions *)defaultOptions { + return [[FIRSnapshotListenOptions alloc] init:FIRListenSourceDefault includeMetadataChanges:NO]; +} + ++ (FIRSnapshotListenOptions *)optionsWithIncludeMetadataChanges:(BOOL)includeMetadataChanges { + return [[FIRSnapshotListenOptions alloc] init:FIRListenSourceDefault + includeMetadataChanges:includeMetadataChanges]; +} + ++ (FIRSnapshotListenOptions *)optionsWithSource:(FIRListenSource)source { + return [[FIRSnapshotListenOptions alloc] init:source includeMetadataChanges:NO]; +} + +@end + +NS_ASSUME_NONNULL_END diff --git a/Firestore/Source/Public/FirebaseFirestore/FIRDocumentReference.h b/Firestore/Source/Public/FirebaseFirestore/FIRDocumentReference.h index 70dbd0d02ad2..df49617cba45 100644 --- a/Firestore/Source/Public/FirebaseFirestore/FIRDocumentReference.h +++ b/Firestore/Source/Public/FirebaseFirestore/FIRDocumentReference.h @@ -18,6 +18,7 @@ #import "FIRFirestoreSource.h" #import "FIRListenerRegistration.h" +#import "FIRSnapshotListenOptions.h" @class FIRCollectionReference; @class FIRDocumentSnapshot; @@ -268,6 +269,12 @@ addSnapshotListenerWithIncludeMetadataChanges:(BOOL)includeMetadataChanges listener:(void (^)(FIRDocumentSnapshot *_Nullable snapshot, NSError *_Nullable error))listener NS_SWIFT_NAME(addSnapshotListener(includeMetadataChanges:listener:)); + +- (id)addSnapshotListenerWithOptions: + (FIRSnapshotListenOptions *)options + listener:(void (^)(FIRDocumentSnapshot *_Nullable snapshot,NSError *_Nullable error))listener + NS_SWIFT_NAME(addSnapshotListener(options:listener:)); + // clang-format on @end diff --git a/Firestore/Source/Public/FirebaseFirestore/FIRQuery.h b/Firestore/Source/Public/FirebaseFirestore/FIRQuery.h index 843dff160904..337cebf7dc92 100644 --- a/Firestore/Source/Public/FirebaseFirestore/FIRQuery.h +++ b/Firestore/Source/Public/FirebaseFirestore/FIRQuery.h @@ -18,6 +18,7 @@ #import "FIRFirestoreSource.h" #import "FIRListenerRegistration.h" +#import "FIRSnapshotListenOptions.h" @class FIRAggregateQuery; @class FIRAggregateField; @@ -104,6 +105,12 @@ NS_SWIFT_NAME(Query) NSError *_Nullable error))listener NS_SWIFT_NAME(addSnapshotListener(includeMetadataChanges:listener:)); +- (id) + addSnapshotListenerWithOptions:(FIRSnapshotListenOptions *)options + listener:(void (^)(FIRQuerySnapshot *_Nullable snapshot, + NSError *_Nullable error))listener + NS_SWIFT_NAME(addSnapshotListener(options:listener:)); + #pragma mark - Filtering Data /** * Creates and returns a new Query with the additional filter. diff --git a/Firestore/Source/Public/FirebaseFirestore/FIRSnapshotListenOptions.h b/Firestore/Source/Public/FirebaseFirestore/FIRSnapshotListenOptions.h new file mode 100644 index 000000000000..92c0b0601640 --- /dev/null +++ b/Firestore/Source/Public/FirebaseFirestore/FIRSnapshotListenOptions.h @@ -0,0 +1,41 @@ +/* + * Copyright 2023 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#import + +NS_ASSUME_NONNULL_BEGIN + +typedef NS_ENUM(NSUInteger, FIRListenSource) { + FIRListenSourceDefault, + FIRListenSourceCache +} NS_SWIFT_NAME(ListenSource); + +NS_SWIFT_NAME(SnapshotListenOptions) +@interface FIRSnapshotListenOptions : NSObject + +@property(nonatomic, readonly) FIRListenSource source; +@property(nonatomic, readonly) BOOL includeMetadataChanges; + +- (instancetype)init:(FIRListenSource)source + includeMetadataChanges:(BOOL)includeMetadataChanges NS_DESIGNATED_INITIALIZER; + ++ (FIRSnapshotListenOptions *)defaultOptions; ++ (FIRSnapshotListenOptions *)optionsWithIncludeMetadataChanges:(BOOL)includeMetadataChanges; ++ (FIRSnapshotListenOptions *)optionsWithSource:(FIRListenSource)source; + +@end + +NS_ASSUME_NONNULL_END \ No newline at end of file diff --git a/Firestore/Source/Public/FirebaseFirestore/FirebaseFirestore.h b/Firestore/Source/Public/FirebaseFirestore/FirebaseFirestore.h index 6746aa489e8e..9a0574a82075 100644 --- a/Firestore/Source/Public/FirebaseFirestore/FirebaseFirestore.h +++ b/Firestore/Source/Public/FirebaseFirestore/FirebaseFirestore.h @@ -34,6 +34,7 @@ #import "FIRLocalCacheSettings.h" #import "FIRQuery.h" #import "FIRQuerySnapshot.h" +#import "FIRSnapshotListenOptions.h" #import "FIRSnapshotMetadata.h" #import "FIRTimestamp.h" #import "FIRTransaction.h" From 59f2e00bc9751caf56ed7667f481934435798c11 Mon Sep 17 00:00:00 2001 From: milaGGL <107142260+milaGGL@users.noreply.github.com> Date: Wed, 29 Nov 2023 10:33:52 -0500 Subject: [PATCH 2/8] format --- Firestore/Source/API/FIRSnapshotListenOptions.mm | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Firestore/Source/API/FIRSnapshotListenOptions.mm b/Firestore/Source/API/FIRSnapshotListenOptions.mm index 9486ab6c365b..ee46557b2990 100644 --- a/Firestore/Source/API/FIRSnapshotListenOptions.mm +++ b/Firestore/Source/API/FIRSnapshotListenOptions.mm @@ -21,14 +21,8 @@ #include #include -// #include "Firestore/core/src/api/firestore.h" -// #include "Firestore/core/src/util/exception.h" - NS_ASSUME_NONNULL_BEGIN -// using firebase::firestore::api::kDefaultTransactionMaxAttempts; -// using firebase::firestore::util::ThrowInvalidArgument; - @implementation FIRSnapshotListenOptions - (instancetype)init:(FIRListenSource)source includeMetadataChanges:(BOOL)includeMetadataChanges { From 6603e6068b7c75b0730e2a601ba386331d738142 Mon Sep 17 00:00:00 2001 From: milaGGL <107142260+milaGGL@users.noreply.github.com> Date: Wed, 29 Nov 2023 22:07:53 +0400 Subject: [PATCH 3/8] update FIRSnapshotListenOptions --- .../FIRSnapshotListenOptions.h | 15 +++++++ .../API/FIRFirestoreSourceTests.mm | 40 +++++++++++++++++++ .../Source/API/FIRSnapshotListenOptions.mm | 21 +++++----- .../FIRSnapshotListenOptions.h | 5 ++- 4 files changed, 68 insertions(+), 13 deletions(-) create mode 100644 FirebaseFirestoreInternal/FirebaseFirestore/FIRSnapshotListenOptions.h diff --git a/FirebaseFirestoreInternal/FirebaseFirestore/FIRSnapshotListenOptions.h b/FirebaseFirestoreInternal/FirebaseFirestore/FIRSnapshotListenOptions.h new file mode 100644 index 000000000000..b41ccbc429db --- /dev/null +++ b/FirebaseFirestoreInternal/FirebaseFirestore/FIRSnapshotListenOptions.h @@ -0,0 +1,15 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#import diff --git a/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm b/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm index d8905641a0ef..f89599ea1a35 100644 --- a/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm +++ b/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm @@ -714,4 +714,44 @@ - (void)Demo_addSnapshotListenerWithDefaultListenOptions { }]; } +//- (void)Demo_addSnapshotListenerWithMetadataChanges { +// FIRCollectionReference *collection = [self.db collectionWithPath:@"cities"]; +// FIRQuery *query = [collection queryWhereField:@"state" isEqualTo:@ "CA"]; +// FIRSnapshotListenOptions* options = [ +// FIRSnapshotListenOptions optionsWithIncludeMetadataChanges:YES +// ]; +// +// [query addSnapshotListenerWithOptions:options +// listener:^(FIRQuerySnapshot *snapshot, NSError *error) { +// XCTAssertNil(error); +// XCTAssertEqual(snapshot.count, 0); +// }]; +//} +// +//- (void)Demo_addSnapshotListenerFromCache { +// FIRCollectionReference *collection = [self.db collectionWithPath:@"cities"]; +// FIRQuery *query = [collection queryWhereField:@"state" isEqualTo:@ "CA"]; +// FIRSnapshotListenOptions* options = [ +// FIRSnapshotListenOptions optionsWithSource:FIRListenSourceCache +// ]; +// +// [query addSnapshotListenerWithOptions:options +// listener:^(FIRQuerySnapshot *snapshot, NSError *error) { +// XCTAssertNil(error); +// XCTAssertEqual(snapshot.count, 0); +// }]; +//} +// +//- (void)Demo_addSnapshotListenerFromCacheAndIncludeMetadataChanges { +// FIRCollectionReference *collection = [self.db collectionWithPath:@"cities"]; +// FIRQuery *query = [collection queryWhereField:@"state" isEqualTo:@ "CA"]; +// +// FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] initWithSource:FIRListenSourceCache includeMetadataChanges:YES]; +// +// [query addSnapshotListenerWithOptions:options +// listener:^(FIRQuerySnapshot *snapshot, NSError *error) { +// XCTAssertNil(error); +// XCTAssertEqual(snapshot.count, 0); +// }]; +//} @end diff --git a/Firestore/Source/API/FIRSnapshotListenOptions.mm b/Firestore/Source/API/FIRSnapshotListenOptions.mm index ee46557b2990..ed0cd86eda09 100644 --- a/Firestore/Source/API/FIRSnapshotListenOptions.mm +++ b/Firestore/Source/API/FIRSnapshotListenOptions.mm @@ -25,26 +25,25 @@ @implementation FIRSnapshotListenOptions -- (instancetype)init:(FIRListenSource)source includeMetadataChanges:(BOOL)includeMetadataChanges { - self = [super init]; - if (self) { - _source = source; - _includeMetadataChanges = includeMetadataChanges; - } - return self; +- (instancetype)initWithSource:(FIRListenSource)source includeMetadataChanges:(BOOL)includeMetadataChanges { + self = [super init]; + if (self) { + _source = source; + _includeMetadataChanges = includeMetadataChanges; + } + return self; } + (FIRSnapshotListenOptions *)defaultOptions { - return [[FIRSnapshotListenOptions alloc] init:FIRListenSourceDefault includeMetadataChanges:NO]; + return [[FIRSnapshotListenOptions alloc] initWithSource:FIRListenSourceDefault includeMetadataChanges:NO]; } + (FIRSnapshotListenOptions *)optionsWithIncludeMetadataChanges:(BOOL)includeMetadataChanges { - return [[FIRSnapshotListenOptions alloc] init:FIRListenSourceDefault - includeMetadataChanges:includeMetadataChanges]; + return [[FIRSnapshotListenOptions alloc] initWithSource:FIRListenSourceDefault includeMetadataChanges:includeMetadataChanges]; } + (FIRSnapshotListenOptions *)optionsWithSource:(FIRListenSource)source { - return [[FIRSnapshotListenOptions alloc] init:source includeMetadataChanges:NO]; + return [[FIRSnapshotListenOptions alloc] initWithSource:source includeMetadataChanges:NO]; } @end diff --git a/Firestore/Source/Public/FirebaseFirestore/FIRSnapshotListenOptions.h b/Firestore/Source/Public/FirebaseFirestore/FIRSnapshotListenOptions.h index 92c0b0601640..e9d7b2a48caa 100644 --- a/Firestore/Source/Public/FirebaseFirestore/FIRSnapshotListenOptions.h +++ b/Firestore/Source/Public/FirebaseFirestore/FIRSnapshotListenOptions.h @@ -29,8 +29,9 @@ NS_SWIFT_NAME(SnapshotListenOptions) @property(nonatomic, readonly) FIRListenSource source; @property(nonatomic, readonly) BOOL includeMetadataChanges; -- (instancetype)init:(FIRListenSource)source - includeMetadataChanges:(BOOL)includeMetadataChanges NS_DESIGNATED_INITIALIZER; +- (instancetype)init NS_UNAVAILABLE; + +- (instancetype)initWithSource:(FIRListenSource)source includeMetadataChanges:(BOOL)includeMetadataChanges NS_DESIGNATED_INITIALIZER; + (FIRSnapshotListenOptions *)defaultOptions; + (FIRSnapshotListenOptions *)optionsWithIncludeMetadataChanges:(BOOL)includeMetadataChanges; From 1dd2c4962900d9324888508ee1bbea834d4461f1 Mon Sep 17 00:00:00 2001 From: milaGGL <107142260+milaGGL@users.noreply.github.com> Date: Wed, 29 Nov 2023 22:23:37 +0400 Subject: [PATCH 4/8] format --- .../API/FIRFirestoreSourceTests.mm | 3 ++- .../Source/API/FIRSnapshotListenOptions.mm | 23 +++++++++++-------- .../FIRSnapshotListenOptions.h | 3 ++- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm b/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm index f89599ea1a35..3843b8b4fcbb 100644 --- a/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm +++ b/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm @@ -746,7 +746,8 @@ - (void)Demo_addSnapshotListenerWithDefaultListenOptions { // FIRCollectionReference *collection = [self.db collectionWithPath:@"cities"]; // FIRQuery *query = [collection queryWhereField:@"state" isEqualTo:@ "CA"]; // -// FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] initWithSource:FIRListenSourceCache includeMetadataChanges:YES]; +// FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] +// initWithSource:FIRListenSourceCache includeMetadataChanges:YES]; // // [query addSnapshotListenerWithOptions:options // listener:^(FIRQuerySnapshot *snapshot, NSError *error) { diff --git a/Firestore/Source/API/FIRSnapshotListenOptions.mm b/Firestore/Source/API/FIRSnapshotListenOptions.mm index ed0cd86eda09..cb183d9fa322 100644 --- a/Firestore/Source/API/FIRSnapshotListenOptions.mm +++ b/Firestore/Source/API/FIRSnapshotListenOptions.mm @@ -25,25 +25,28 @@ @implementation FIRSnapshotListenOptions -- (instancetype)initWithSource:(FIRListenSource)source includeMetadataChanges:(BOOL)includeMetadataChanges { - self = [super init]; - if (self) { - _source = source; - _includeMetadataChanges = includeMetadataChanges; - } - return self; +- (instancetype)initWithSource:(FIRListenSource)source + includeMetadataChanges:(BOOL)includeMetadataChanges { + self = [super init]; + if (self) { + _source = source; + _includeMetadataChanges = includeMetadataChanges; + } + return self; } + (FIRSnapshotListenOptions *)defaultOptions { - return [[FIRSnapshotListenOptions alloc] initWithSource:FIRListenSourceDefault includeMetadataChanges:NO]; + return [[FIRSnapshotListenOptions alloc] initWithSource:FIRListenSourceDefault + includeMetadataChanges:NO]; } + (FIRSnapshotListenOptions *)optionsWithIncludeMetadataChanges:(BOOL)includeMetadataChanges { - return [[FIRSnapshotListenOptions alloc] initWithSource:FIRListenSourceDefault includeMetadataChanges:includeMetadataChanges]; + return [[FIRSnapshotListenOptions alloc] initWithSource:FIRListenSourceDefault + includeMetadataChanges:includeMetadataChanges]; } + (FIRSnapshotListenOptions *)optionsWithSource:(FIRListenSource)source { - return [[FIRSnapshotListenOptions alloc] initWithSource:source includeMetadataChanges:NO]; + return [[FIRSnapshotListenOptions alloc] initWithSource:source includeMetadataChanges:NO]; } @end diff --git a/Firestore/Source/Public/FirebaseFirestore/FIRSnapshotListenOptions.h b/Firestore/Source/Public/FirebaseFirestore/FIRSnapshotListenOptions.h index e9d7b2a48caa..ce2c28ac5e4e 100644 --- a/Firestore/Source/Public/FirebaseFirestore/FIRSnapshotListenOptions.h +++ b/Firestore/Source/Public/FirebaseFirestore/FIRSnapshotListenOptions.h @@ -31,7 +31,8 @@ NS_SWIFT_NAME(SnapshotListenOptions) - (instancetype)init NS_UNAVAILABLE; -- (instancetype)initWithSource:(FIRListenSource)source includeMetadataChanges:(BOOL)includeMetadataChanges NS_DESIGNATED_INITIALIZER; +- (instancetype)initWithSource:(FIRListenSource)source + includeMetadataChanges:(BOOL)includeMetadataChanges NS_DESIGNATED_INITIALIZER; + (FIRSnapshotListenOptions *)defaultOptions; + (FIRSnapshotListenOptions *)optionsWithIncludeMetadataChanges:(BOOL)includeMetadataChanges; From c77cac2922233f4803281a6a6bb2457df85d361a Mon Sep 17 00:00:00 2001 From: milaGGL <107142260+milaGGL@users.noreply.github.com> Date: Wed, 29 Nov 2023 22:49:45 +0400 Subject: [PATCH 5/8] format --- .../API/FIRFirestoreSourceTests.mm | 42 ------------------- Firestore/Source/API/FIRDocumentReference.mm | 1 - Firestore/Source/API/FIRQuery.mm | 1 - 3 files changed, 44 deletions(-) diff --git a/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm b/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm index 3843b8b4fcbb..880443b78387 100644 --- a/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm +++ b/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm @@ -713,46 +713,4 @@ - (void)Demo_addSnapshotListenerWithDefaultListenOptions { XCTAssertEqual(snapshot.count, 0); }]; } - -//- (void)Demo_addSnapshotListenerWithMetadataChanges { -// FIRCollectionReference *collection = [self.db collectionWithPath:@"cities"]; -// FIRQuery *query = [collection queryWhereField:@"state" isEqualTo:@ "CA"]; -// FIRSnapshotListenOptions* options = [ -// FIRSnapshotListenOptions optionsWithIncludeMetadataChanges:YES -// ]; -// -// [query addSnapshotListenerWithOptions:options -// listener:^(FIRQuerySnapshot *snapshot, NSError *error) { -// XCTAssertNil(error); -// XCTAssertEqual(snapshot.count, 0); -// }]; -//} -// -//- (void)Demo_addSnapshotListenerFromCache { -// FIRCollectionReference *collection = [self.db collectionWithPath:@"cities"]; -// FIRQuery *query = [collection queryWhereField:@"state" isEqualTo:@ "CA"]; -// FIRSnapshotListenOptions* options = [ -// FIRSnapshotListenOptions optionsWithSource:FIRListenSourceCache -// ]; -// -// [query addSnapshotListenerWithOptions:options -// listener:^(FIRQuerySnapshot *snapshot, NSError *error) { -// XCTAssertNil(error); -// XCTAssertEqual(snapshot.count, 0); -// }]; -//} -// -//- (void)Demo_addSnapshotListenerFromCacheAndIncludeMetadataChanges { -// FIRCollectionReference *collection = [self.db collectionWithPath:@"cities"]; -// FIRQuery *query = [collection queryWhereField:@"state" isEqualTo:@ "CA"]; -// -// FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] -// initWithSource:FIRListenSourceCache includeMetadataChanges:YES]; -// -// [query addSnapshotListenerWithOptions:options -// listener:^(FIRQuerySnapshot *snapshot, NSError *error) { -// XCTAssertNil(error); -// XCTAssertEqual(snapshot.count, 0); -// }]; -//} @end diff --git a/Firestore/Source/API/FIRDocumentReference.mm b/Firestore/Source/API/FIRDocumentReference.mm index faadf1756eea..9a6e3fd94e69 100644 --- a/Firestore/Source/API/FIRDocumentReference.mm +++ b/Firestore/Source/API/FIRDocumentReference.mm @@ -212,7 +212,6 @@ - (void)getDocumentWithSource:(FIRFirestoreSource)source return [self addSnapshotListenerInternalWithOptions:options listener:listener]; } -// TODO MILA - (id)addSnapshotListenerWithOptions:(FIRSnapshotListenOptions *)options listener:(FIRDocumentSnapshotBlock)listener { ListenOptions listenOptions = ListenOptions::FromIncludeMetadataChanges(false); diff --git a/Firestore/Source/API/FIRQuery.mm b/Firestore/Source/API/FIRQuery.mm index ab3ef0992ed0..4adba59744f3 100644 --- a/Firestore/Source/API/FIRQuery.mm +++ b/Firestore/Source/API/FIRQuery.mm @@ -191,7 +191,6 @@ - (void)getDocumentsWithSource:(FIRFirestoreSource)publicSource return [self addSnapshotListenerInternalWithOptions:options listener:listener]; } -// TODO MILA - (id)addSnapshotListenerWithOptions:(FIRSnapshotListenOptions *)options listener:(FIRQuerySnapshotBlock)listener { auto listenOptions = ListenOptions::FromIncludeMetadataChanges(false); From 5737dda6b09b657837a62b5b22192e5d74255cd2 Mon Sep 17 00:00:00 2001 From: milaGGL <107142260+milaGGL@users.noreply.github.com> Date: Tue, 2 Jan 2024 09:54:35 -0500 Subject: [PATCH 6/8] update interface --- .../API/FIRFirestoreSourceTests.mm | 43 ++++++++++++++++++- .../Source/API/FIRSnapshotListenOptions.mm | 21 ++++----- .../FIRSnapshotListenOptions.h | 9 ++-- 3 files changed, 56 insertions(+), 17 deletions(-) diff --git a/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm b/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm index 880443b78387..c9ff1252e0ca 100644 --- a/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm +++ b/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm @@ -705,7 +705,7 @@ - (void)testGetNonExistingCollectionWhileOfflineServerOnly { - (void)Demo_addSnapshotListenerWithDefaultListenOptions { FIRCollectionReference *collection = [self.db collectionWithPath:@"cities"]; FIRQuery *query = [collection queryWhereField:@"state" isEqualTo:@ "CA"]; - FIRSnapshotListenOptions *options = [FIRSnapshotListenOptions defaultOptions]; + FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] initWithDefaultOptions]; [query addSnapshotListenerWithOptions:options listener:^(FIRQuerySnapshot *snapshot, NSError *error) { @@ -713,4 +713,45 @@ - (void)Demo_addSnapshotListenerWithDefaultListenOptions { XCTAssertEqual(snapshot.count, 0); }]; } + +void Demo_addSnapshotListenerWithMetadataChanges(FIRFirestore* db) { + FIRCollectionReference *collection = [self.db collectionWithPath:@"cities"]; + FIRQuery *query = [collection queryWhereField:@"state" isEqualTo:@ "CA"]; + FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] initWithDefaultOptions]; + FIRSnapshotListenOptions *optionsWithMetadata = [options withIncludeMetadataChanges:YES]; + + [query addSnapshotListenerWithOptions:options + listener:^(FIRQuerySnapshot *snapshot, NSError *error) { + XCTAssertNil(error); + XCTAssertEqual(snapshot.count, 0); + }]; +} + +void Demo_addSnapshotListenerFromCache(FIRFirestore* db) { + FIRCollectionReference *collection = [self.db collectionWithPath:@"cities"]; + FIRQuery *query = [collection queryWhereField:@"state" isEqualTo:@ "CA"]; + FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] initWithDefaultOptions]; + FIRSnapshotListenOptions *optionsFromCache = [options withSource:FIRListenSourceCache]; + + [query addSnapshotListenerWithOptions:options + listener:^(FIRQuerySnapshot *snapshot, NSError *error) { + XCTAssertNil(error); + XCTAssertEqual(snapshot.count, 0); + }]; +} + +void Demo_addSnapshotListenerFromCache(FIRFirestore* db) { + FIRCollectionReference *collection = [self.db collectionWithPath:@"cities"]; + FIRQuery *query = [collection queryWhereField:@"state" isEqualTo:@ "CA"]; + FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] initWithDefaultOptions]; + FIRSnapshotListenOptions *optionsWithMetadata = [options withIncludeMetadataChanges:YES]; + FIRSnapshotListenOptions *optionsFromCacheAndWithMetadata = [optionsWithMetadata withSource:FIRListenSourceCache]; + + [query addSnapshotListenerWithOptions:optionsFromCacheAndWithMetadata + listener:^(FIRQuerySnapshot *snapshot, NSError *error) { + XCTAssertNil(error); + XCTAssertEqual(snapshot.count, 0); + }]; +} + @end diff --git a/Firestore/Source/API/FIRSnapshotListenOptions.mm b/Firestore/Source/API/FIRSnapshotListenOptions.mm index cb183d9fa322..96903ffeb61f 100644 --- a/Firestore/Source/API/FIRSnapshotListenOptions.mm +++ b/Firestore/Source/API/FIRSnapshotListenOptions.mm @@ -25,7 +25,8 @@ @implementation FIRSnapshotListenOptions -- (instancetype)initWithSource:(FIRListenSource)source + +- (instancetype)initPrivateWithSource:(FIRListenSource)source includeMetadataChanges:(BOOL)includeMetadataChanges { self = [super init]; if (self) { @@ -35,20 +36,20 @@ - (instancetype)initWithSource:(FIRListenSource)source return self; } -+ (FIRSnapshotListenOptions *)defaultOptions { - return [[FIRSnapshotListenOptions alloc] initWithSource:FIRListenSourceDefault - includeMetadataChanges:NO]; +- (instancetype)initWithDefaultOptions { + return [self initPrivateWithSource:FIRListenSourceDefault includeMetadataChanges:NO]; } -+ (FIRSnapshotListenOptions *)optionsWithIncludeMetadataChanges:(BOOL)includeMetadataChanges { - return [[FIRSnapshotListenOptions alloc] initWithSource:FIRListenSourceDefault - includeMetadataChanges:includeMetadataChanges]; -} -+ (FIRSnapshotListenOptions *)optionsWithSource:(FIRListenSource)source { - return [[FIRSnapshotListenOptions alloc] initWithSource:source includeMetadataChanges:NO]; +- (FIRSnapshotListenOptions *)withIncludeMetadataChanges:(BOOL)includeMetadataChanges { + return [[FIRSnapshotListenOptions alloc] initPrivateWithSource:self.source + includeMetadataChanges:includeMetadataChanges]; } +- (FIRSnapshotListenOptions *)withSource:(FIRListenSource)source { + return [[FIRSnapshotListenOptions alloc] initPrivateWithSource:source + includeMetadataChanges:self.includeMetadataChanges];} + @end NS_ASSUME_NONNULL_END diff --git a/Firestore/Source/Public/FirebaseFirestore/FIRSnapshotListenOptions.h b/Firestore/Source/Public/FirebaseFirestore/FIRSnapshotListenOptions.h index ce2c28ac5e4e..099016a23623 100644 --- a/Firestore/Source/Public/FirebaseFirestore/FIRSnapshotListenOptions.h +++ b/Firestore/Source/Public/FirebaseFirestore/FIRSnapshotListenOptions.h @@ -30,13 +30,10 @@ NS_SWIFT_NAME(SnapshotListenOptions) @property(nonatomic, readonly) BOOL includeMetadataChanges; - (instancetype)init NS_UNAVAILABLE; +- (instancetype)initWithDefaultOptions NS_DESIGNATED_INITIALIZER; -- (instancetype)initWithSource:(FIRListenSource)source - includeMetadataChanges:(BOOL)includeMetadataChanges NS_DESIGNATED_INITIALIZER; - -+ (FIRSnapshotListenOptions *)defaultOptions; -+ (FIRSnapshotListenOptions *)optionsWithIncludeMetadataChanges:(BOOL)includeMetadataChanges; -+ (FIRSnapshotListenOptions *)optionsWithSource:(FIRListenSource)source; +- (FIRSnapshotListenOptions *)withIncludeMetadataChanges:(BOOL)includeMetadataChanges; +- (FIRSnapshotListenOptions *)withSource:(FIRListenSource)source; @end From 6598a65f33b8dba8be1d277ab8a8a3136de114b0 Mon Sep 17 00:00:00 2001 From: milaGGL <107142260+milaGGL@users.noreply.github.com> Date: Tue, 2 Jan 2024 12:21:24 -0500 Subject: [PATCH 7/8] update tests --- .../API/FIRFirestoreSourceTests.mm | 67 ++++++++++++++++--- .../Source/API/FIRSnapshotListenOptions.mm | 27 +++++--- 2 files changed, 77 insertions(+), 17 deletions(-) diff --git a/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm b/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm index c9ff1252e0ca..8a88f0b4efab 100644 --- a/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm +++ b/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm @@ -701,8 +701,58 @@ - (void)testGetNonExistingCollectionWhileOfflineServerOnly { [self awaitExpectations]; } -// -- (void)Demo_addSnapshotListenerWithDefaultListenOptions { +// Document Reference +- (void)DocumentReferenceDemo_addSnapshotListenerWithDefaultListenOptions { + FIRDocumentReference *doc = [self.db documentWithPath:@"cities/SF"]; + FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] initWithDefaultOptions]; + + [doc addSnapshotListenerWithOptions:options + listener:^(FIRDocumentSnapshot *snapshot, NSError *error) { + XCTAssertNil(error); + XCTAssertTrue(snapshot.exists); + }]; +} + +- (void)DocumentReferenceDemo_addSnapshotListenerWithMetadataChanges { + FIRDocumentReference *doc = [self.db documentWithPath:@"cities/SF"]; + FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] initWithDefaultOptions]; + FIRSnapshotListenOptions *optionsWithMetadata = [options withIncludeMetadataChanges:YES]; + + [doc addSnapshotListenerWithOptions:optionsWithMetadata + listener:^(FIRDocumentSnapshot *snapshot, NSError *error) { + XCTAssertNil(error); + XCTAssertTrue(snapshot.exists); + }]; +} + +- (void)DocumentReferenceDemo_addSnapshotListenerFromCache { + FIRDocumentReference *doc = [self.db documentWithPath:@"cities/SF"]; + FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] initWithDefaultOptions]; + FIRSnapshotListenOptions *optionsFromCache = [options withSource:FIRListenSourceCache]; + + [doc addSnapshotListenerWithOptions:optionsFromCache + listener:^(FIRDocumentSnapshot *snapshot, NSError *error) { + XCTAssertNil(error); + XCTAssertTrue(snapshot.exists); + }]; +} + +- (void)DocumentReferenceDemo_addSnapshotListenerFromCacheAndIncludeMetadataChanges { + FIRDocumentReference *doc = [self.db documentWithPath:@"cities/SF"]; + FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] initWithDefaultOptions]; + FIRSnapshotListenOptions *optionsWithMetadata = [options withIncludeMetadataChanges:YES]; + FIRSnapshotListenOptions *optionsFromCacheAndWithMetadata = + [optionsWithMetadata withSource:FIRListenSourceCache]; + + [doc addSnapshotListenerWithOptions:optionsFromCacheAndWithMetadata + listener:^(FIRDocumentSnapshot *snapshot, NSError *error) { + XCTAssertNil(error); + XCTAssertTrue(snapshot.exists); + }]; +} + +// Query +- (void)QueryDemo_addSnapshotListenerWithDefaultListenOptions { FIRCollectionReference *collection = [self.db collectionWithPath:@"cities"]; FIRQuery *query = [collection queryWhereField:@"state" isEqualTo:@ "CA"]; FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] initWithDefaultOptions]; @@ -714,38 +764,39 @@ - (void)Demo_addSnapshotListenerWithDefaultListenOptions { }]; } -void Demo_addSnapshotListenerWithMetadataChanges(FIRFirestore* db) { +- (void)QueryDemo_addSnapshotListenerWithMetadataChanges { FIRCollectionReference *collection = [self.db collectionWithPath:@"cities"]; FIRQuery *query = [collection queryWhereField:@"state" isEqualTo:@ "CA"]; FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] initWithDefaultOptions]; FIRSnapshotListenOptions *optionsWithMetadata = [options withIncludeMetadataChanges:YES]; - [query addSnapshotListenerWithOptions:options + [query addSnapshotListenerWithOptions:optionsWithMetadata listener:^(FIRQuerySnapshot *snapshot, NSError *error) { XCTAssertNil(error); XCTAssertEqual(snapshot.count, 0); }]; } -void Demo_addSnapshotListenerFromCache(FIRFirestore* db) { +- (void)QueryDemo_addSnapshotListenerFromCache { FIRCollectionReference *collection = [self.db collectionWithPath:@"cities"]; FIRQuery *query = [collection queryWhereField:@"state" isEqualTo:@ "CA"]; FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] initWithDefaultOptions]; FIRSnapshotListenOptions *optionsFromCache = [options withSource:FIRListenSourceCache]; - [query addSnapshotListenerWithOptions:options + [query addSnapshotListenerWithOptions:optionsFromCache listener:^(FIRQuerySnapshot *snapshot, NSError *error) { XCTAssertNil(error); XCTAssertEqual(snapshot.count, 0); }]; } -void Demo_addSnapshotListenerFromCache(FIRFirestore* db) { +- (void)QueryDemo_addSnapshotListenerFromCacheAndIncludeMetadataChanges { FIRCollectionReference *collection = [self.db collectionWithPath:@"cities"]; FIRQuery *query = [collection queryWhereField:@"state" isEqualTo:@ "CA"]; FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] initWithDefaultOptions]; FIRSnapshotListenOptions *optionsWithMetadata = [options withIncludeMetadataChanges:YES]; - FIRSnapshotListenOptions *optionsFromCacheAndWithMetadata = [optionsWithMetadata withSource:FIRListenSourceCache]; + FIRSnapshotListenOptions *optionsFromCacheAndWithMetadata = + [optionsWithMetadata withSource:FIRListenSourceCache]; [query addSnapshotListenerWithOptions:optionsFromCacheAndWithMetadata listener:^(FIRQuerySnapshot *snapshot, NSError *error) { diff --git a/Firestore/Source/API/FIRSnapshotListenOptions.mm b/Firestore/Source/API/FIRSnapshotListenOptions.mm index 96903ffeb61f..23f22565ac32 100644 --- a/Firestore/Source/API/FIRSnapshotListenOptions.mm +++ b/Firestore/Source/API/FIRSnapshotListenOptions.mm @@ -25,10 +25,10 @@ @implementation FIRSnapshotListenOptions - +// private method - (instancetype)initPrivateWithSource:(FIRListenSource)source - includeMetadataChanges:(BOOL)includeMetadataChanges { - self = [super init]; + includeMetadataChanges:(BOOL)includeMetadataChanges { + self = [self initWithDefaultOptions]; if (self) { _source = source; _includeMetadataChanges = includeMetadataChanges; @@ -37,18 +37,27 @@ - (instancetype)initPrivateWithSource:(FIRListenSource)source } - (instancetype)initWithDefaultOptions { - return [self initPrivateWithSource:FIRListenSourceDefault includeMetadataChanges:NO]; + self = [super init]; + if (self) { + _source = FIRListenSourceDefault; + _includeMetadataChanges = NO; + } + return self; } - - (FIRSnapshotListenOptions *)withIncludeMetadataChanges:(BOOL)includeMetadataChanges { - return [[FIRSnapshotListenOptions alloc] initPrivateWithSource:self.source - includeMetadataChanges:includeMetadataChanges]; + FIRSnapshotListenOptions *newOptions = + [[FIRSnapshotListenOptions alloc] initPrivateWithSource:self.source + includeMetadataChanges:includeMetadataChanges]; + return newOptions; } - (FIRSnapshotListenOptions *)withSource:(FIRListenSource)source { - return [[FIRSnapshotListenOptions alloc] initPrivateWithSource:source - includeMetadataChanges:self.includeMetadataChanges];} + FIRSnapshotListenOptions *newOptions = + [[FIRSnapshotListenOptions alloc] initPrivateWithSource:source + includeMetadataChanges:self.includeMetadataChanges]; + return newOptions; +} @end From 19ee4874184f3a2f71e5c9606488e4a9e9e03fd6 Mon Sep 17 00:00:00 2001 From: milaGGL <107142260+milaGGL@users.noreply.github.com> Date: Wed, 3 Jan 2024 10:49:26 -0500 Subject: [PATCH 8/8] Update FIRFirestoreSourceTests.mm --- .../Tests/Integration/API/FIRFirestoreSourceTests.mm | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm b/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm index 8a88f0b4efab..012baad990b1 100644 --- a/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm +++ b/Firestore/Example/Tests/Integration/API/FIRFirestoreSourceTests.mm @@ -740,9 +740,8 @@ - (void)DocumentReferenceDemo_addSnapshotListenerFromCache { - (void)DocumentReferenceDemo_addSnapshotListenerFromCacheAndIncludeMetadataChanges { FIRDocumentReference *doc = [self.db documentWithPath:@"cities/SF"]; FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] initWithDefaultOptions]; - FIRSnapshotListenOptions *optionsWithMetadata = [options withIncludeMetadataChanges:YES]; FIRSnapshotListenOptions *optionsFromCacheAndWithMetadata = - [optionsWithMetadata withSource:FIRListenSourceCache]; + [[options withIncludeMetadataChanges:YES] withSource:FIRListenSourceCache]; [doc addSnapshotListenerWithOptions:optionsFromCacheAndWithMetadata listener:^(FIRDocumentSnapshot *snapshot, NSError *error) { @@ -794,9 +793,8 @@ - (void)QueryDemo_addSnapshotListenerFromCacheAndIncludeMetadataChanges { FIRCollectionReference *collection = [self.db collectionWithPath:@"cities"]; FIRQuery *query = [collection queryWhereField:@"state" isEqualTo:@ "CA"]; FIRSnapshotListenOptions *options = [[FIRSnapshotListenOptions alloc] initWithDefaultOptions]; - FIRSnapshotListenOptions *optionsWithMetadata = [options withIncludeMetadataChanges:YES]; FIRSnapshotListenOptions *optionsFromCacheAndWithMetadata = - [optionsWithMetadata withSource:FIRListenSourceCache]; + [[options withIncludeMetadataChanges:YES] withSource:FIRListenSourceCache]; [query addSnapshotListenerWithOptions:optionsFromCacheAndWithMetadata listener:^(FIRQuerySnapshot *snapshot, NSError *error) {