FirestoreQuery.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright 2021 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import SwiftUI
  17. #if SWIFT_PACKAGE
  18. @_exported import FirebaseFirestoreInternalWrapper
  19. #else
  20. @_exported import FirebaseFirestoreInternal
  21. #endif // SWIFT_PACKAGE
  22. /// The strategy to use when an error occurs during mapping Firestore documents
  23. /// to the target type of `FirestoreQuery`.
  24. ///
  25. public enum DecodingFailureStrategy {
  26. /// Ignore any errors that occur when mapping Firestore documents.
  27. case ignore
  28. /// Raise an error when mapping a Firestore document fails.
  29. case raise
  30. }
  31. /// A property wrapper that listens to a Firestore collection.
  32. ///
  33. /// In the following example, `FirestoreQuery` will fetch all documents from the
  34. /// `fruits` collection, filtering only documents whose `isFavourite` attribute
  35. /// is equal to `true`, map members of result set to the `Fruit` type, and make
  36. /// them available via the wrapped value `fruits`.
  37. ///
  38. /// struct ContentView: View {
  39. /// @FirestoreQuery(
  40. /// collectionPath: "fruits",
  41. /// predicates: [.whereField("isFavourite", isEqualTo: true)]
  42. /// ) var fruits: [Fruit]
  43. ///
  44. /// var body: some View {
  45. /// List(fruits) { fruit in
  46. /// Text(fruit.name)
  47. /// }
  48. /// }
  49. /// }
  50. ///
  51. /// `FirestoreQuery` also supports returning a `Result` type. The `.success` case
  52. /// returns an array of elements, whereas the `.failure` case returns an error
  53. /// in case mapping the Firestore documents wasn't successful:
  54. ///
  55. /// struct ContentView: View {
  56. /// @FirestoreQuery(
  57. /// collectionPath: "fruits",
  58. /// predicates: [.whereField("isFavourite", isEqualTo: true)]
  59. /// ) var fruitResults: Result<[Fruit], Error>
  60. ///
  61. /// var body: some View {
  62. /// if case let .success(fruits) = fruitResults {
  63. /// List(fruits) { fruit in
  64. /// Text(fruit.name)
  65. /// }
  66. /// } else if case let .failure(error) = fruitResults {
  67. /// Text("Couldn't map data: \(error.localizedDescription)")
  68. /// }
  69. /// }
  70. ///
  71. /// Alternatively, the _projected value_ of the property wrapper provides access to
  72. /// the `error` as well. This allows you to display a list of all successfully mapped
  73. /// documents, as well as an error message with details about the documents that couldn't
  74. /// be mapped successfully (e.g. because of a field name mismatch).
  75. ///
  76. /// struct ContentView: View {
  77. /// @FirestoreQuery(
  78. /// collectionPath: "mappingFailure",
  79. /// decodingFailureStrategy: .ignore
  80. /// ) private var fruits: [Fruit]
  81. ///
  82. /// var body: some View {
  83. /// VStack(alignment: .leading) {
  84. /// List(fruits) { fruit in
  85. /// Text(fruit.name)
  86. /// }
  87. /// if $fruits.error != nil {
  88. /// HStack {
  89. /// Text("There was an error")
  90. /// .foregroundColor(Color(UIColor.systemBackground))
  91. /// Spacer()
  92. /// }
  93. /// .padding(30)
  94. /// .background(Color.red)
  95. /// }
  96. /// }
  97. /// }
  98. /// }
  99. ///
  100. /// Internally, `@FirestoreQuery` sets up a snapshot listener and publishes
  101. /// any incoming changes via an `@StateObject`.
  102. ///
  103. /// The projected value of this property wrapper provides access to a
  104. /// configuration object of type `FirestoreQueryConfiguration` which can be used
  105. /// to modify the query criteria. Changing the filter predicates results in the
  106. /// underlying snapshot listener being unregistered and a new one registered.
  107. ///
  108. /// Button("Show only Apples and Oranges") {
  109. /// $fruits.predicates = [.whereField("name", isIn: ["Apple", "Orange]]
  110. /// }
  111. ///
  112. /// This property wrapper does not support updating the `wrappedValue`, i.e.
  113. /// you need to use Firestore's other APIs to add, delete, or modify documents.
  114. @available(iOS 14.0, macOS 11.0, macCatalyst 14.0, tvOS 14.0, watchOS 7.0, *)
  115. @propertyWrapper
  116. public struct FirestoreQuery<T>: DynamicProperty {
  117. @StateObject private var firestoreQueryObservable: FirestoreQueryObservable<T>
  118. /// The query's configurable properties.
  119. public struct Configuration {
  120. /// The query's collection path.
  121. public var path: String
  122. /// The query's predicates.
  123. public var predicates: [QueryPredicate]
  124. /// The strategy to use in case there was a problem during the decoding phase.
  125. public var decodingFailureStrategy: DecodingFailureStrategy = .raise
  126. /// If any errors occurred, they will be exposed here as well.
  127. public var error: Error?
  128. /// The type of animation to apply when updating the view. If this is omitted then no
  129. /// animations are fired.
  130. public var animation: Animation?
  131. }
  132. /// The results of the query.
  133. ///
  134. /// This property returns an empty collection when there are no matching results.
  135. public var wrappedValue: T {
  136. firestoreQueryObservable.items
  137. }
  138. /// A binding to the request's mutable configuration properties
  139. public var projectedValue: Configuration {
  140. get {
  141. firestoreQueryObservable.configuration
  142. }
  143. nonmutating set {
  144. firestoreQueryObservable.objectWillChange.send()
  145. firestoreQueryObservable.configuration = newValue
  146. }
  147. }
  148. /// Creates an instance by defining a query based on the parameters.
  149. /// - Parameters:
  150. /// - collectionPath: The path to the Firestore collection to query.
  151. /// - predicates: An optional array of `QueryPredicate`s that defines a
  152. /// filter for the fetched results.
  153. /// - decodingFailureStrategy: The strategy to use when there is a failure
  154. /// during the decoding phase. Defaults to `DecodingFailureStrategy.raise`.
  155. /// - animation: The optional animation to apply to the transaction.
  156. public init<U: Decodable>(collectionPath: String, predicates: [QueryPredicate] = [],
  157. decodingFailureStrategy: DecodingFailureStrategy = .raise,
  158. animation: Animation? = nil)
  159. where T == [U] {
  160. let configuration = Configuration(
  161. path: collectionPath,
  162. predicates: predicates,
  163. decodingFailureStrategy: decodingFailureStrategy,
  164. animation: animation
  165. )
  166. _firestoreQueryObservable =
  167. StateObject(wrappedValue: FirestoreQueryObservable<T>(configuration: configuration))
  168. }
  169. /// Creates an instance by defining a query based on the parameters.
  170. /// - Parameters:
  171. /// - collectionPath: The path to the Firestore collection to query.
  172. /// - predicates: An optional array of `QueryPredicate`s that defines a
  173. /// filter for the fetched results.
  174. /// - decodingFailureStrategy: The strategy to use when there is a failure
  175. /// during the decoding phase. Defaults to `DecodingFailureStrategy.raise`.
  176. /// - animation: The optional animation to apply to the transaction.
  177. public init<U: Decodable>(collectionPath: String, predicates: [QueryPredicate] = [],
  178. decodingFailureStrategy: DecodingFailureStrategy = .raise,
  179. animation: Animation? = nil)
  180. where T == Result<[U], Error> {
  181. let configuration = Configuration(
  182. path: collectionPath,
  183. predicates: predicates,
  184. decodingFailureStrategy: decodingFailureStrategy,
  185. animation: animation
  186. )
  187. _firestoreQueryObservable =
  188. StateObject(wrappedValue: FirestoreQueryObservable<T>(configuration: configuration))
  189. }
  190. }