Settings.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. import Foundation
  16. /// Extends ApplicationInfoProtocol to string-format a combined appDisplayVersion and appBuildVersion
  17. extension ApplicationInfoProtocol {
  18. var synthesizedVersion: String { return "\(appDisplayVersion) (\(appBuildVersion))" }
  19. }
  20. /// Provides the APIs to access Settings and their configuration values
  21. protocol SettingsProtocol {
  22. func isCacheExpired(currentTime: Date) -> Bool
  23. var sessionsEnabled: Bool { get }
  24. var samplingRate: Double { get }
  25. var sessionTimeout: TimeInterval { get }
  26. }
  27. class Settings: SettingsProtocol {
  28. private static let cacheDurationSecondsDefault: TimeInterval = 60 * 60
  29. private static let flagSessionsEnabled = "sessions_enabled"
  30. private static let flagSamplingRate = "sampling_rate"
  31. private static let flagSessionTimeout = "session_timeout"
  32. private static let flagCacheDuration = "cache_duration"
  33. private let cache: SettingsCacheClient
  34. private let appInfo: ApplicationInfoProtocol
  35. var sessionsEnabled: Bool {
  36. guard let enabled = cache.cacheContent?[Settings.flagSessionsEnabled] as? Bool else {
  37. return true
  38. }
  39. return enabled
  40. }
  41. var samplingRate: Double {
  42. guard let rate = cache.cacheContent?[Settings.flagSamplingRate] as? Double else {
  43. return 1.0
  44. }
  45. return rate
  46. }
  47. var sessionTimeout: TimeInterval {
  48. guard let timeout = cache.cacheContent?[Settings.flagSessionTimeout] as? Double else {
  49. return 30 * 60
  50. }
  51. return timeout
  52. }
  53. private var cacheDurationSeconds: TimeInterval {
  54. guard let duration = cache.cacheContent?[Settings.flagCacheDuration] as? Double else {
  55. return Settings.cacheDurationSecondsDefault
  56. }
  57. return duration
  58. }
  59. init(cache: SettingsCacheClient = SettingsCache(),
  60. appInfo: ApplicationInfoProtocol) {
  61. self.cache = cache
  62. self.appInfo = appInfo
  63. }
  64. func isCacheExpired(currentTime: Date) -> Bool {
  65. guard cache.cacheContent != nil else {
  66. cache.removeCache()
  67. return true
  68. }
  69. guard let cacheKey = cache.cacheKey else {
  70. Logger.logError("[Settings] Could not load settings cache key")
  71. cache.removeCache()
  72. return true
  73. }
  74. guard cacheKey.googleAppID == appInfo.appID else {
  75. Logger
  76. .logDebug("[Settings] Cache expired because Google App ID changed")
  77. cache.removeCache()
  78. return true
  79. }
  80. if currentTime.timeIntervalSince(cacheKey.createdAt) > cacheDurationSeconds {
  81. Logger.logDebug("[Settings] Cache TTL expired")
  82. return true
  83. }
  84. if appInfo.synthesizedVersion != cacheKey.appVersion {
  85. Logger.logDebug("[Settings] Cache expired because app version changed")
  86. return true
  87. }
  88. return false
  89. }
  90. }