66//
77
88import CoreLocation
9+ import Combine
10+ import SwiftUI
911
1012/// Helper class to determine permission to get access for streaming ``CLLocation``
1113@available ( iOS 15 . 0 , watchOS 7 . 0 , * )
1214final class Permission {
1315
16+ static let authorizationStatus = Notification . Name ( " authorizationStatus " )
17+
1418 // MARK: - Private properties
1519
1620 /// Current status
@@ -22,10 +26,14 @@ final class Permission{
2226 /// Check if status is determined
2327 private var isDetermined : Bool { status != . notDetermined }
2428
29+ /// Subscription to authorization status changes
30+ private var canellable : AnyCancellable ?
31+
2532 // MARK: - Life circle
2633
2734 init ( with status: CLAuthorizationStatus ) {
2835 self . status = status
36+ initSubscription ( )
2937 }
3038
3139 // MARK: - API
@@ -34,18 +42,27 @@ final class Permission{
3442 public func isGranted( for manager: CLLocationManager ) async -> Bool {
3543 let status = await requestPermission ( manager)
3644 return isAuthorized ( status)
45+ }
46+
47+ // MARK: - Private methods
48+
49+ private func initSubscription( ) {
50+ canellable = NotificationCenter . default. publisher ( for: Permission . authorizationStatus, object: nil )
51+ . sink { [ weak self] value in
52+ self ? . authorizationChanged ( value)
53+ }
3754 }
3855
3956 /// Determine status after the request permission
4057 /// - Parameter manager: Location manager
41- public func locationManagerDidChangeAuthorization( _ manager: CLLocationManager ) {
42- status = manager. authorizationStatus
43-
44- permissioning? . resume ( returning: status)
58+ private func authorizationChanged( _ value: Output ) {
59+ if let s = value. object as? CLAuthorizationStatus {
60+ status = s
61+ permissioning? . resume ( returning: status)
62+ print ( status, " authorizationStatus " )
63+ }
4564 }
4665
47- // MARK: - Private methods
48-
4966 /// Check permission status
5067 /// - Parameter status: Status for checking
5168 /// - Returns: Return `True` if is allowed
@@ -68,3 +85,7 @@ final class Permission{
6885 }
6986 }
7087}
88+
89+ // MARK: - Alias types -
90+
91+ fileprivate typealias Output = NotificationCenter . Publisher . Output
0 commit comments