RemoteConfigProperty.swift 1.5 KB

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