SessionsSettings.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. // TODO: Remove this block before release
  45. // Disabling the SDK because some customers seem to be building from master
  46. // Uncomment the code below before release
  47. if let sessionEnabled = localOverrides.sessionsEnabled {
  48. return sessionEnabled
  49. }
  50. return false
  51. // TODO: uncomment before release
  52. // // Order of precendence LocalOverrides > Remote Settings > SDK Defaults
  53. // if let sessionEnabled = localOverrides.sessionsEnabled {
  54. // return sessionEnabled
  55. // } else if let sessionEnabled = remoteSettings.sessionsEnabled {
  56. // return sessionEnabled
  57. // }
  58. // return sdkDefaults.sessionsEnabled!
  59. }
  60. var sessionTimeout: TimeInterval {
  61. // Order of precendence LocalOverrides > Remote Settings > SDK Defaults
  62. if let sessionTimeout = localOverrides.sessionTimeout {
  63. return sessionTimeout
  64. } else if let sessionTimeout = remoteSettings.sessionTimeout {
  65. return sessionTimeout
  66. }
  67. return sdkDefaults.sessionTimeout!
  68. }
  69. var samplingRate: Double {
  70. // Order of precendence LocalOverrides > Remote Settings > SDK Defaults
  71. if let samplingRate = localOverrides.samplingRate {
  72. return samplingRate
  73. } else if let samplingRate = remoteSettings.samplingRate {
  74. return samplingRate
  75. }
  76. return sdkDefaults.samplingRate!
  77. }
  78. func updateSettings() {
  79. // Update the settings for all the settings providers
  80. sdkDefaults.updateSettings()
  81. remoteSettings.updateSettings()
  82. localOverrides.updateSettings()
  83. }
  84. }