SessionInitiator.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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) || os(visionOS)
  16. import UIKit
  17. #elseif os(macOS)
  18. import AppKit
  19. import Cocoa
  20. #elseif os(watchOS)
  21. import WatchKit
  22. #endif // os(iOS) || os(tvOS)
  23. /// The SessionInitiator is responsible for:
  24. /// 1) Running the initiate callback whenever a Session Start Event should
  25. /// begin sending. This can happen at a cold start of the app, and when it
  26. /// been in the background for a period of time (originally set at 30 mins)
  27. /// and comes to the foreground.
  28. ///
  29. class SessionInitiator {
  30. let currentTime: () -> Date
  31. let settings: SettingsProtocol
  32. private var backgroundTime = Date.distantFuture
  33. private var initiateSessionStart: () -> Void = {}
  34. init(settings: SettingsProtocol, currentTimeProvider: @escaping () -> Date = Date.init) {
  35. currentTime = currentTimeProvider
  36. self.settings = settings
  37. }
  38. func beginListening(initiateSessionStart: @escaping () -> Void) {
  39. self.initiateSessionStart = initiateSessionStart
  40. self.initiateSessionStart()
  41. let notificationCenter = NotificationCenter.default
  42. #if os(iOS) || os(tvOS) || os(visionOS)
  43. notificationCenter.addObserver(
  44. self,
  45. selector: #selector(appBackgrounded),
  46. name: UIApplication.didEnterBackgroundNotification,
  47. object: nil
  48. )
  49. notificationCenter.addObserver(
  50. self,
  51. selector: #selector(appForegrounded),
  52. name: UIApplication.didBecomeActiveNotification,
  53. object: nil
  54. )
  55. #elseif os(macOS)
  56. notificationCenter.addObserver(
  57. self,
  58. selector: #selector(appBackgrounded),
  59. name: NSApplication.didResignActiveNotification,
  60. object: nil
  61. )
  62. notificationCenter.addObserver(
  63. self,
  64. selector: #selector(appForegrounded),
  65. name: NSApplication.didBecomeActiveNotification,
  66. object: nil
  67. )
  68. #elseif os(watchOS)
  69. // Versions below WatchOS 7 do not support lifecycle events
  70. if #available(watchOSApplicationExtension 7.0, *) {
  71. notificationCenter.addObserver(
  72. self,
  73. selector: #selector(appBackgrounded),
  74. name: WKExtension.applicationDidEnterBackgroundNotification,
  75. object: nil
  76. )
  77. notificationCenter.addObserver(
  78. self,
  79. selector: #selector(appForegrounded),
  80. name: WKExtension.applicationDidBecomeActiveNotification,
  81. object: nil
  82. )
  83. }
  84. #endif // os(iOS) || os(tvOS)
  85. }
  86. @objc private func appBackgrounded() {
  87. backgroundTime = currentTime()
  88. }
  89. @objc private func appForegrounded() {
  90. let interval = currentTime().timeIntervalSince(backgroundTime)
  91. // If the interval is greater the the session timeout duration, generate a new session.
  92. if interval > settings.sessionTimeout {
  93. initiateSessionStart()
  94. }
  95. }
  96. }