diff --git a/Firestore/Source/API/FIRCallbackWrapper.mm b/Firestore/Source/API/FIRCallbackWrapper.mm new file mode 100644 index 00000000000..1681c6642eb --- /dev/null +++ b/Firestore/Source/API/FIRCallbackWrapper.mm @@ -0,0 +1,66 @@ +/* + * Copyright 2024 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 "FIRCallbackWrapper.h" + +#include +#include +#include + +#include "Firestore/core/interfaceForSwift/api/pipeline.h" +#include "Firestore/core/interfaceForSwift/api/pipeline_result.h" +#include "Firestore/core/src/core/event_listener.h" +#include "Firestore/core/src/util/error_apple.h" +#include "Firestore/core/src/util/statusor.h" + +using firebase::firestore::api::PipelineResult; +using firebase::firestore::api::PipelineSnapshotListener; +using firebase::firestore::core::EventListener; +using firebase::firestore::util::MakeNSError; +using firebase::firestore::util::StatusOr; + +@implementation FIRCallbackWrapper + ++ (PipelineSnapshotListener) + wrapPipelineCallback:(std::shared_ptr)firestore + completion:(void (^)(std::shared_ptr> result, + NSError *_Nullable error))completion { + class Converter : public EventListener> { + public: + explicit Converter(std::shared_ptr firestore, PipelineBlock completion) + : firestore_(firestore), completion_(completion) { + } + + void OnEvent(StatusOr> maybe_snapshot) override { + if (maybe_snapshot.ok()) { + completion_( + std::make_shared>( + std::initializer_list{PipelineResult::GetTestResult(firestore_)}), + nullptr); + } else { + completion_(nullptr, MakeNSError(maybe_snapshot.status())); + } + } + + private: + std::shared_ptr firestore_; + PipelineBlock completion_; + }; + + return absl::make_unique(firestore, completion); +} + +@end diff --git a/Firestore/Source/API/FIRDocumentSnapshot.mm b/Firestore/Source/API/FIRDocumentSnapshot.mm index acf33065273..efa0c94703e 100644 --- a/Firestore/Source/API/FIRDocumentSnapshot.mm +++ b/Firestore/Source/API/FIRDocumentSnapshot.mm @@ -86,8 +86,8 @@ - (instancetype)initWithFirestore:(FIRFirestore *)firestore metadata:(SnapshotMetadata)metadata { DocumentSnapshot wrapped; if (document.has_value()) { - wrapped = - DocumentSnapshot::FromDocument(firestore.cppFirestorePtr, document.value(), std::move(metadata)); + wrapped = DocumentSnapshot::FromDocument(firestore.cppFirestorePtr, document.value(), + std::move(metadata)); } else { wrapped = DocumentSnapshot::FromNoDocument(firestore.cppFirestorePtr, std::move(documentKey), std::move(metadata)); diff --git a/Firestore/Source/API/FIRQuery.mm b/Firestore/Source/API/FIRQuery.mm index c0af25c4c2a..2632cdd5e71 100644 --- a/Firestore/Source/API/FIRQuery.mm +++ b/Firestore/Source/API/FIRQuery.mm @@ -63,8 +63,6 @@ #include "Firestore/core/src/util/exception.h" #include "Firestore/core/src/util/statusor.h" #include "Firestore/core/src/util/string_apple.h" -#include "Firestore/core/interfaceForSwift/api/pipeline.h" -#include "Firestore/core/interfaceForSwift/api/pipeline_result.h" #include "absl/strings/match.h" namespace nanopb = firebase::firestore::nanopb; @@ -73,8 +71,6 @@ using firebase::firestore::google_firestore_v1_Value_fields; using firebase::firestore::api::Firestore; using firebase::firestore::api::MakeListenSource; -using firebase::firestore::api::PipelineResult; -using firebase::firestore::api::PipelineSnapshotListener; using firebase::firestore::api::Query; using firebase::firestore::api::QueryListenerRegistration; using firebase::firestore::api::QuerySnapshot; @@ -179,35 +175,6 @@ - (void)getDocumentsWithCompletion:(void (^)(FIRQuerySnapshot *_Nullable snapsho _query.GetDocuments(Source::Default, [self wrapQuerySnapshotBlock:completion]); } -+ (PipelineSnapshotListener) - wrapPipelineCallback:(std::shared_ptr)firestore - completion:(void (^)(std::shared_ptr> result, - NSError *_Nullable error))completion { - class Converter : public EventListener> { - public: - explicit Converter(std::shared_ptr firestore, PipelineBlock completion) - : firestore_(firestore), completion_(completion) { - } - - void OnEvent(StatusOr> maybe_snapshot) override { - if (maybe_snapshot.ok()) { - completion_( - std::make_shared>( - std::initializer_list{PipelineResult::GetTestResult(firestore_)}), - nullptr); - } else { - completion_(nullptr, MakeNSError(maybe_snapshot.status())); - } - } - - private: - std::shared_ptr firestore_; - PipelineBlock completion_; - }; - - return absl::make_unique(firestore, completion); -} - - (void)getDocumentsWithSource:(FIRFirestoreSource)publicSource completion:(void (^)(FIRQuerySnapshot *_Nullable snapshot, NSError *_Nullable error))completion { @@ -567,29 +534,6 @@ void OnEvent(StatusOr maybe_snapshot) override { return absl::make_unique(block); } -+ (QuerySnapshotListener)wrapPipelineCallbackBlock:(FIRQuerySnapshotBlock)block { - class Converter : public EventListener { - public: - explicit Converter(FIRQuerySnapshotBlock block) : block_(block) { - } - - void OnEvent(StatusOr maybe_snapshot) override { - if (maybe_snapshot.ok()) { - FIRQuerySnapshot *result = - [[FIRQuerySnapshot alloc] initWithSnapshot:std::move(maybe_snapshot).ValueOrDie()]; - block_(result, nil); - } else { - block_(nil, MakeNSError(maybe_snapshot.status())); - } - } - - private: - FIRQuerySnapshotBlock block_; - }; - - return absl::make_unique(block); -} - - (Filter)parseFieldFilter:(FSTUnaryFilter *)unaryFilter { auto describer = [&unaryFilter] { return MakeString(NSStringFromClass([unaryFilter.value class])); diff --git a/Firestore/Source/Public/FirebaseFirestore/FIRCallbackWrapper.h b/Firestore/Source/Public/FirebaseFirestore/FIRCallbackWrapper.h new file mode 100644 index 00000000000..25f8df2fa23 --- /dev/null +++ b/Firestore/Source/Public/FirebaseFirestore/FIRCallbackWrapper.h @@ -0,0 +1,58 @@ +/* + * Copyright 2024 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 +#import + +namespace firebase { +namespace firestore { +namespace api { +class Firestore; +class PipelineResult; +} // namespace api + +namespace core { +template +class EventListener; +} // namespace core + +} // namespace firestore +} // namespace firebase + +namespace api = firebase::firestore::api; +namespace core = firebase::firestore::core; + +NS_ASSUME_NONNULL_BEGIN + +typedef void (^PipelineBlock)(std::shared_ptr> result, + NSError *_Nullable error) + NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead."); + +typedef std::shared_ptr> PipelineResultVectorPtr; + +NS_SWIFT_SENDABLE +NS_SWIFT_NAME(CallbackWrapper) +@interface FIRCallbackWrapper : NSObject + ++ (std::unique_ptr>>) + wrapPipelineCallback:(std::shared_ptr)firestore + completion:(void (^)(std::shared_ptr> result, + NSError *_Nullable error))completion + NS_SWIFT_NAME(wrapPipelineCallback(firestore:completion:)); + +@end + +NS_ASSUME_NONNULL_END diff --git a/Firestore/Source/Public/FirebaseFirestore/FIRQuery.h b/Firestore/Source/Public/FirebaseFirestore/FIRQuery.h index f7430950de5..46f30beeab1 100644 --- a/Firestore/Source/Public/FirebaseFirestore/FIRQuery.h +++ b/Firestore/Source/Public/FirebaseFirestore/FIRQuery.h @@ -29,24 +29,6 @@ @class FIRQuerySnapshot; @class FIRDocumentSnapshot; -namespace firebase { -namespace firestore { -namespace api { -class Firestore; -class PipelineResult; -} // namespace api - -namespace core { -template -class EventListener; -} // namespace core - -} // namespace firestore -} // namespace firebase - -namespace api = firebase::firestore::api; -namespace core = firebase::firestore::core; - NS_ASSUME_NONNULL_BEGIN /** @@ -56,12 +38,6 @@ typedef void (^FIRQuerySnapshotBlock)(FIRQuerySnapshot *_Nullable snapshot, NSError *_Nullable error) NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead."); -typedef void (^PipelineBlock)(std::shared_ptr> result, - NSError *_Nullable error) - NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead."); - -typedef std::shared_ptr> PipelineResultVectorPtr; - /** * A `Query` refers to a query which you can read or listen to. You can also construct * refined `Query` objects by adding filters and ordering. @@ -91,11 +67,6 @@ NS_SWIFT_NAME(Query) (void (^)(FIRQuerySnapshot *_Nullable snapshot, NSError *_Nullable error))completion NS_SWIFT_NAME(getDocuments(completion:)); -+ (std::unique_ptr>>) - wrapPipelineCallback:(std::shared_ptr)firestore - completion:(void (^)(std::shared_ptr> result, - NSError *_Nullable error))completion - NS_SWIFT_NAME(wrapPipelineCallback(firestore:completion:)); /** * Reads the documents matching this query. * diff --git a/Firestore/Source/Public/FirebaseFirestore/FirebaseFirestore.h b/Firestore/Source/Public/FirebaseFirestore/FirebaseFirestore.h index 7fabad323c8..f8d62b9bddc 100644 --- a/Firestore/Source/Public/FirebaseFirestore/FirebaseFirestore.h +++ b/Firestore/Source/Public/FirebaseFirestore/FirebaseFirestore.h @@ -18,6 +18,7 @@ #import "FIRAggregateQuery.h" #import "FIRAggregateQuerySnapshot.h" #import "FIRAggregateSource.h" +#import "FIRCallbackWrapper.h" #import "FIRCollectionReference.h" #import "FIRDocumentChange.h" #import "FIRDocumentReference.h" diff --git a/Firestore/Swift/Source/AsyncAwait/Firestore+AsyncAwait.swift b/Firestore/Swift/Source/AsyncAwait/Firestore+AsyncAwait.swift index 29fb37f5f5b..d08bb8867b2 100644 --- a/Firestore/Swift/Source/AsyncAwait/Firestore+AsyncAwait.swift +++ b/Firestore/Swift/Source/AsyncAwait/Firestore+AsyncAwait.swift @@ -15,8 +15,8 @@ */ #if SWIFT_PACKAGE - @_exported import FirebaseFirestoreInternalWrapper import FirebaseFirestoreCpp + @_exported import FirebaseFirestoreInternalWrapper #else @_exported import FirebaseFirestoreInternal #endif // SWIFT_PACKAGE diff --git a/Firestore/Swift/Source/SwiftAPI/Pipeline.swift b/Firestore/Swift/Source/SwiftAPI/Pipeline.swift index 542e40acd77..60150d9af83 100644 --- a/Firestore/Swift/Source/SwiftAPI/Pipeline.swift +++ b/Firestore/Swift/Source/SwiftAPI/Pipeline.swift @@ -6,7 +6,7 @@ // #if SWIFT_PACKAGE - import FirebaseFirestoreCpp + import FirebaseFirestoreCpp #endif @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *) @@ -20,7 +20,7 @@ public class Pipeline { @discardableResult public func GetPipelineResult() async throws -> [PipelineResult] { return try await withCheckedThrowingContinuation { continuation in - let listener = Query.wrapPipelineCallback(firestore: cppObj.GetFirestore()) { + let listener = CallbackWrapper.wrapPipelineCallback(firestore: cppObj.GetFirestore()) { result, error in if let error { continuation.resume(throwing: error) diff --git a/Firestore/Swift/Source/SwiftAPI/PipelineResult.swift b/Firestore/Swift/Source/SwiftAPI/PipelineResult.swift index 253976fb4a3..fc0c6aced77 100644 --- a/Firestore/Swift/Source/SwiftAPI/PipelineResult.swift +++ b/Firestore/Swift/Source/SwiftAPI/PipelineResult.swift @@ -5,7 +5,7 @@ // Created by Cheryl Lin on 2024-12-18. // #if SWIFT_PACKAGE - import FirebaseFirestoreCpp + import FirebaseFirestoreCpp #endif @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *) diff --git a/Firestore/core/interfaceForSwift/api/pipeline.cc b/Firestore/core/interfaceForSwift/api/pipeline.cc index 619334e50e4..fde34b8794d 100644 --- a/Firestore/core/interfaceForSwift/api/pipeline.cc +++ b/Firestore/core/interfaceForSwift/api/pipeline.cc @@ -4,13 +4,13 @@ #include #include "Firestore/core/include/firebase/firestore/timestamp.h" +#include "Firestore/core/interfaceForSwift/api/pipeline_result.h" #include "Firestore/core/src/api/firestore.h" #include "Firestore/core/src/api/listener_registration.h" #include "Firestore/core/src/api/source.h" #include "Firestore/core/src/core/event_listener.h" #include "Firestore/core/src/core/listen_options.h" #include "Firestore/core/src/core/view_snapshot.h" -#include "Firestore/core/interfaceForSwift/api/pipeline_result.h" namespace firebase { namespace firestore { diff --git a/Firestore/core/interfaceForSwift/api/pipeline_source.cc b/Firestore/core/interfaceForSwift/api/pipeline_source.cc index d16650534b5..f3db5a4b565 100644 --- a/Firestore/core/interfaceForSwift/api/pipeline_source.cc +++ b/Firestore/core/interfaceForSwift/api/pipeline_source.cc @@ -1,7 +1,7 @@ #include "Firestore/core/interfaceForSwift/api/pipeline_source.h" +#include "Firestore/core/interfaceForSwift/api/collection_stage.h" #include "Firestore/core/src/api/document_reference.h" #include "Firestore/core/src/api/firestore.h" -#include "Firestore/core/interfaceForSwift/api/collection_stage.h" namespace firebase { namespace firestore { diff --git a/Firestore/core/src/api/firestore.h b/Firestore/core/src/api/firestore.h index 8f869cd6387..bb39c7a1022 100644 --- a/Firestore/core/src/api/firestore.h +++ b/Firestore/core/src/api/firestore.h @@ -21,6 +21,7 @@ #include // NOLINT(build/c++11) #include +#include "Firestore/core/interfaceForSwift/api/pipeline_source.h" #include "Firestore/core/src/api/api_fwd.h" #include "Firestore/core/src/api/load_bundle_task.h" #include "Firestore/core/src/api/settings.h" @@ -29,7 +30,6 @@ #include "Firestore/core/src/model/database_id.h" #include "Firestore/core/src/util/byte_stream.h" #include "Firestore/core/src/util/status_fwd.h" -#include "Firestore/core/interfaceForSwift/api/pipeline_source.h" namespace firebase { namespace firestore {