SessionInitiator.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 * 60 // 30 minutes
  30. let currentTime: () -> Date
  31. var backgroundTime = Date.distantFuture
  32. var initiateSessionStart: () -> Void = {}
  33. init(currentTimeProvider: @escaping () -> Date = Date.init) {
  34. currentTime = currentTimeProvider
  35. }
  36. func beginListening(initiateSessionStart: @escaping () -> Void) {
  37. self.initiateSessionStart = initiateSessionStart
  38. self.initiateSessionStart()
  39. let notificationCenter = NotificationCenter.default
  40. #if os(iOS) || os(tvOS)
  41. notificationCenter.addObserver(
  42. self,
  43. selector: #selector(appBackgrounded),
  44. name: UIApplication.didEnterBackgroundNotification,
  45. object: nil
  46. )
  47. notificationCenter.addObserver(
  48. self,
  49. selector: #selector(appForegrounded),
  50. name: UIApplication.didBecomeActiveNotification,
  51. object: nil
  52. )
  53. #elseif os(macOS)
  54. notificationCenter.addObserver(
  55. self,
  56. selector: #selector(appBackgrounded),
  57. name: NSApplication.didResignActiveNotification,
  58. object: nil
  59. )
  60. notificationCenter.addObserver(
  61. self,
  62. selector: #selector(appForegrounded),
  63. name: NSApplication.didBecomeActiveNotification,
  64. object: nil
  65. )
  66. #elseif os(watchOS)
  67. // Versions below WatchOS 7 do not support lifecycle events
  68. if #available(watchOSApplicationExtension 7.0, *) {
  69. notificationCenter.addObserver(
  70. self,
  71. selector: #selector(appBackgrounded),
  72. name: WKExtension.applicationDidEnterBackgroundNotification,
  73. object: nil
  74. )
  75. notificationCenter.addObserver(
  76. self,
  77. selector: #selector(appForegrounded),
  78. name: WKExtension.applicationDidBecomeActiveNotification,
  79. object: nil
  80. )
  81. }
  82. #endif
  83. }
  84. @objc func appBackgrounded() {
  85. backgroundTime = currentTime()
  86. }
  87. @objc func appForegrounded() {
  88. let interval = currentTime().timeIntervalSince(backgroundTime)
  89. if interval > sessionTimeout {
  90. initiateSessionStart()
  91. }
  92. }
  93. }