@@ -3,26 +3,41 @@ import UIKit
33
44// We need an abstract state handler to make the Notifications (didBecomeActiveNotification, ...) easier to test
55class InstanaApplicationStateHandler {
6+
7+ enum State {
8+ case active
9+ case inactive
10+ case background
11+ }
12+
613 static let shared = InstanaApplicationStateHandler ( )
714
8- @Atomic var state : UIApplication . State = UIApplication . shared . applicationState {
15+ @Atomic var state : State = . inactive {
916 didSet { stateUpdateHandler. forEach { $0 ( state) } }
1017 }
1118
12- typealias StateUpdater = ( UIApplication . State ) -> Void
19+ typealias StateUpdater = ( State ) -> Void
1320 private var stateUpdateHandler = [ StateUpdater] ( )
1421
1522 init ( ) {
1623 _ = NotificationCenter . default. addObserver ( forName: UIApplication . didBecomeActiveNotification, object: nil , queue: nil ) { _ in
1724 self . state = . active
1825 }
26+ _ = NotificationCenter . default. addObserver ( forName: NSNotification . Name. NSExtensionHostDidBecomeActive, object: nil , queue: nil ) { _ in
27+ self . state = . active
28+ }
1929 _ = NotificationCenter . default. addObserver ( forName: UIApplication . didEnterBackgroundNotification, object: nil , queue: nil ) { _ in
2030 self . state = . background
2131 }
22-
32+ _ = NotificationCenter . default. addObserver ( forName: NSNotification . Name. NSExtensionHostDidEnterBackground, object: nil , queue: nil ) { _ in
33+ self . state = . background
34+ }
2335 _ = NotificationCenter . default. addObserver ( forName: UIApplication . willResignActiveNotification, object: nil , queue: nil ) { _ in
2436 self . state = . inactive
2537 }
38+ _ = NotificationCenter . default. addObserver ( forName: NSNotification . Name. NSExtensionHostWillResignActive, object: nil , queue: nil ) { _ in
39+ self . state = . inactive
40+ }
2641 }
2742
2843 func listen( _ handler: @escaping StateUpdater ) {
0 commit comments