SessionInitiator.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2022 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 os(iOS) || os(tvOS)
  16. import UIKit
  17. #elseif os(macOS)
  18. import Cocoa
  19. import AppKit
  20. #elseif os(watchOS)
  21. import WatchKit
  22. #endif // os(iOS) || os(tvOS)
  23. // swift(>=5.9) implies Xcode 15+
  24. // Need to have this Swift version check to use os(visionOS) macro, VisionOS support.
  25. // TODO: Remove this check and add `os(visionOS)` to the `os(iOS) || os(tvOS)` conditional above
  26. // when Xcode 15 is the minimum supported by Firebase.
  27. #if swift(>=5.9)
  28. #if os(visionOS)
  29. import UIKit
  30. #endif // os(visionOS)
  31. #endif // swift(>=5.9)
  32. ///
  33. /// The SessionInitiator is responsible for:
  34. /// 1) Running the initiate callback whenever a Session Start Event should
  35. /// begin sending. This can happen at a cold start of the app, and when it
  36. /// been in the background for a period of time (originally set at 30 mins)
  37. /// and comes to the foreground.
  38. ///
  39. class SessionInitiator {
  40. let currentTime: () -> Date
  41. var settings: SettingsProtocol
  42. var backgroundTime = Date.distantFuture
  43. var initiateSessionStart: () -> Void = {}
  44. init(settings: SettingsProtocol, currentTimeProvider: @escaping () -> Date = Date.init) {
  45. currentTime = currentTimeProvider
  46. self.settings = settings
  47. }
  48. func beginListening(initiateSessionStart: @escaping () -> Void) {
  49. self.initiateSessionStart = initiateSessionStart
  50. self.initiateSessionStart()
  51. let notificationCenter = NotificationCenter.default
  52. #if os(iOS) || os(tvOS)
  53. notificationCenter.addObserver(
  54. self,
  55. selector: #selector(appBackgrounded),
  56. name: UIApplication.didEnterBackgroundNotification,
  57. object: nil
  58. )
  59. notificationCenter.addObserver(
  60. self,
  61. selector: #selector(appForegrounded),
  62. name: UIApplication.didBecomeActiveNotification,
  63. object: nil
  64. )
  65. #elseif os(macOS)
  66. notificationCenter.addObserver(
  67. self,
  68. selector: #selector(appBackgrounded),
  69. name: NSApplication.didResignActiveNotification,
  70. object: nil
  71. )
  72. notificationCenter.addObserver(
  73. self,
  74. selector: #selector(appForegrounded),
  75. name: NSApplication.didBecomeActiveNotification,
  76. object: nil
  77. )
  78. #elseif os(watchOS)
  79. // Versions below WatchOS 7 do not support lifecycle events
  80. if #available(watchOSApplicationExtension 7.0, *) {
  81. notificationCenter.addObserver(
  82. self,
  83. selector: #selector(appBackgrounded),
  84. name: WKExtension.applicationDidEnterBackgroundNotification,
  85. object: nil
  86. )
  87. notificationCenter.addObserver(
  88. self,
  89. selector: #selector(appForegrounded),
  90. name: WKExtension.applicationDidBecomeActiveNotification,
  91. object: nil
  92. )
  93. }
  94. #endif // os(iOS) || os(tvOS)
  95. // swift(>=5.9) implies Xcode 15+
  96. // Need to have this Swift version check to use os(visionOS) macro, VisionOS support.
  97. // TODO: Remove this check and add `os(visionOS)` to the `os(iOS) || os(tvOS)` conditional above
  98. // when Xcode 15 is the minimum supported by Firebase.
  99. #if swift(>=5.9)
  100. #if os(visionOS)
  101. notificationCenter.addObserver(
  102. self,
  103. selector: #selector(appBackgrounded),
  104. name: UIApplication.didEnterBackgroundNotification,
  105. object: nil
  106. )
  107. notificationCenter.addObserver(
  108. self,
  109. selector: #selector(appForegrounded),
  110. name: UIApplication.didBecomeActiveNotification,
  111. object: nil
  112. )
  113. #endif // os(visionOS)
  114. #endif // swift(>=5.9)
  115. }
  116. @objc private func appBackgrounded() {
  117. backgroundTime = currentTime()
  118. }
  119. @objc private func appForegrounded() {
  120. let interval = currentTime().timeIntervalSince(backgroundTime)
  121. // If the interval is greater the the session timeout duration, generate a new session.
  122. if interval > settings.sessionTimeout {
  123. initiateSessionStart()
  124. }
  125. }
  126. }