SessionsSettings.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. /// Class that manages the configs related to the settings library
  17. class SessionsSettings: SettingsProtocol {
  18. private let appInfo: ApplicationInfoProtocol
  19. private let installations: InstallationsProtocol
  20. private let sdkDefaults: SDKDefaultSettings
  21. private let localOverrides: LocalOverrideSettings
  22. private let remoteSettings: RemoteSettings
  23. convenience init(appInfo: ApplicationInfoProtocol, installations: InstallationsProtocol) {
  24. self.init(appInfo: appInfo,
  25. installations: installations,
  26. sdkDefaults: SDKDefaultSettings(),
  27. localOverrides: LocalOverrideSettings(),
  28. remoteSettings: RemoteSettings(appInfo: appInfo,
  29. downloader: SettingsDownloader(appInfo: appInfo,
  30. installations: installations)))
  31. }
  32. init(appInfo: ApplicationInfoProtocol,
  33. installations: InstallationsProtocol,
  34. sdkDefaults: SDKDefaultSettings,
  35. localOverrides: LocalOverrideSettings,
  36. remoteSettings: RemoteSettings) {
  37. self.appInfo = appInfo
  38. self.installations = installations
  39. self.sdkDefaults = sdkDefaults
  40. self.localOverrides = localOverrides
  41. self.remoteSettings = remoteSettings
  42. }
  43. var sessionsEnabled: Bool {
  44. // Order of precedence LocalOverrides > Remote Settings > SDK Defaults
  45. if let sessionEnabled = localOverrides.sessionsEnabled {
  46. return sessionEnabled
  47. } else if let sessionEnabled = remoteSettings.sessionsEnabled {
  48. return sessionEnabled
  49. }
  50. return sdkDefaults.sessionsEnabled!
  51. }
  52. var sessionTimeout: TimeInterval {
  53. // Order of precedence LocalOverrides > Remote Settings > SDK Defaults
  54. if let sessionTimeout = localOverrides.sessionTimeout {
  55. return sessionTimeout
  56. } else if let sessionTimeout = remoteSettings.sessionTimeout {
  57. return sessionTimeout
  58. }
  59. return sdkDefaults.sessionTimeout!
  60. }
  61. var samplingRate: Double {
  62. // Order of precedence LocalOverrides > Remote Settings > SDK Defaults
  63. if let samplingRate = localOverrides.samplingRate {
  64. return samplingRate
  65. } else if let samplingRate = remoteSettings.samplingRate {
  66. return samplingRate
  67. }
  68. return sdkDefaults.samplingRate!
  69. }
  70. func updateSettings() {
  71. // Update the settings for all the settings providers
  72. sdkDefaults.updateSettings()
  73. remoteSettings.updateSettings()
  74. localOverrides.updateSettings()
  75. }
  76. }