CustomSignals.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2024 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. #if SWIFT_PACKAGE
  16. @_exported import FirebaseRemoteConfigInternal
  17. #endif // SWIFT_PACKAGE
  18. /// Represents a value associated with a key in a custom signal, restricted to the allowed data
  19. /// types : String, Int, Double.
  20. public struct CustomSignalValue {
  21. private enum Kind {
  22. case string(String)
  23. case integer(Int)
  24. case double(Double)
  25. }
  26. private let kind: Kind
  27. private init(kind: Kind) {
  28. self.kind = kind
  29. }
  30. /// Returns a string backed custom signal.
  31. /// - Parameter string: The given string to back the custom signal with.
  32. /// - Returns: A string backed custom signal.
  33. public static func string(_ string: String) -> Self {
  34. Self(kind: .string(string))
  35. }
  36. /// Returns an integer backed custom signal.
  37. /// - Parameter integer: The given integer to back the custom signal with.
  38. /// - Returns: An integer backed custom signal.
  39. public static func integer(_ integer: Int) -> Self {
  40. Self(kind: .integer(integer))
  41. }
  42. /// Returns an floating-point backed custom signal.
  43. /// - Parameter double: The given floating-point value to back the custom signal with.
  44. /// - Returns: An floating-point backed custom signal
  45. public static func double(_ double: Double) -> Self {
  46. Self(kind: .double(double))
  47. }
  48. fileprivate func toNSObject() -> NSObject {
  49. switch kind {
  50. case let .string(string):
  51. return string as NSString
  52. case let .integer(int):
  53. return int as NSNumber
  54. case let .double(double):
  55. return double as NSNumber
  56. }
  57. }
  58. }
  59. extension CustomSignalValue: ExpressibleByStringInterpolation {
  60. public init(stringLiteral value: String) {
  61. self = .string(value)
  62. }
  63. }
  64. extension CustomSignalValue: ExpressibleByIntegerLiteral {
  65. public init(integerLiteral value: Int) {
  66. self = .integer(value)
  67. }
  68. }
  69. extension CustomSignalValue: ExpressibleByFloatLiteral {
  70. public init(floatLiteral value: Double) {
  71. self = .double(value)
  72. }
  73. }
  74. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  75. public extension RemoteConfig {
  76. /// Sets custom signals for this Remote Config instance.
  77. /// - Parameter customSignals: A dictionary mapping string keys to custom
  78. /// signals to be set for the app instance.
  79. ///
  80. /// When a new key is provided, a new key-value pair is added to the custom signals.
  81. /// If an existing key is provided with a new value, the corresponding signal is updated.
  82. /// If the value for a key is `nil`, the signal associated with that key is removed.
  83. func setCustomSignals(_ customSignals: [String: CustomSignalValue?]) async throws {
  84. return try await withCheckedThrowingContinuation { continuation in
  85. let customSignals = customSignals.mapValues { $0?.toNSObject() ?? NSNull() }
  86. self.__setCustomSignals(customSignals) { error in
  87. if let error {
  88. continuation.resume(throwing: error)
  89. } else {
  90. continuation.resume()
  91. }
  92. }
  93. }
  94. }
  95. }