SessionInitiator.swift 3.3 KB

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