RemoteConfigProperty.swift 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. #if compiler(>=5.5.2) && canImport(SwiftUI) && (arch(arm64) || arch(x86_64))
  19. /// A property wrapper that listens to a Remote Config value.
  20. @available(iOS 14.0, macOS 11.0, macCatalyst 14.0, tvOS 14.0, watchOS 7.0, *)
  21. @propertyWrapper
  22. public struct RemoteConfigProperty<T: Decodable>: DynamicProperty {
  23. @StateObject private var configValueObserver: RemoteConfigValueObservable<T>
  24. /// Remote Config key name for this property
  25. public let key: String
  26. public var wrappedValue: T {
  27. configValueObserver.configValue
  28. }
  29. /// Creates an instance by defining key.
  30. ///
  31. /// - Parameter key: key name
  32. /// - Parameter placeholder: fall back placeholder
  33. public init(key: String, placeholder: T) {
  34. self.key = key
  35. _configValueObserver = StateObject(
  36. wrappedValue: RemoteConfigValueObservable<T>(
  37. key: key,
  38. fallbackValue: placeholder
  39. )
  40. )
  41. }
  42. }
  43. #endif