RemoteConfigProperty.swift 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright 2022 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. #if SWIFT_PACKAGE
  17. @_exported import FirebaseRemoteConfigInternal
  18. #endif // SWIFT_PACKAGE
  19. import SwiftUI
  20. /// A property wrapper that listens to a Remote Config value.
  21. @available(iOS 14.0, macOS 11.0, macCatalyst 14.0, tvOS 14.0, watchOS 7.0, *)
  22. @propertyWrapper
  23. public struct RemoteConfigProperty<T: Decodable>: DynamicProperty {
  24. @StateObject private var configValueObserver: RemoteConfigValueObservable<T>
  25. /// Remote Config key name for this property
  26. public let key: String
  27. public var wrappedValue: T {
  28. configValueObserver.configValue
  29. }
  30. /// Creates an instance by providing a config key.
  31. ///
  32. /// - Parameter key: key name
  33. /// - Parameter fallback: The value to fall back to if the key doesn't exist in remote or default
  34. /// configs
  35. public init(key: String, fallback: T) {
  36. self.key = key
  37. _configValueObserver = StateObject(
  38. wrappedValue: RemoteConfigValueObservable<T>(
  39. key: key,
  40. fallbackValue: fallback
  41. )
  42. )
  43. }
  44. }