SessionInitiator.swift 3.1 KB

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