@@ -9,10 +9,8 @@ import CoreLocation
99import Foundation
1010
1111class LocationManagerDelegate : NSObject , ObservableObject , CLLocationManagerDelegate {
12- var locationManager = CLLocationManager ( )
12+ private var locationManager = CLLocationManager ( )
1313 @Published var location : CLLocation ?
14- @Published var isLocationDenied : Bool = false
15- @Published var isLocationServicesOff : Bool = false
1614 var locationUpdateHandler : ( ( CLLocationCoordinate2D ) -> Void ) ?
1715 var headingUpdateHandler : ( ( Double ) -> Void ) ?
1816
@@ -23,91 +21,58 @@ class LocationManagerDelegate: NSObject, ObservableObject, CLLocationManagerDele
2321 override init ( ) {
2422 super. init ( )
2523 locationManager. delegate = self
26- checkInitialLocationStatus ( )
2724 }
2825
2926 func requestLocationAuthorization( ) {
30- DispatchQueue . main. async {
31- self . locationManager. requestWhenInUseAuthorization ( )
32- }
27+ self . locationManager. requestWhenInUseAuthorization ( )
3328 }
3429
3530 func startUpdatingLocation( ) {
36- // Perform location services check on a background thread
37- DispatchQueue . global ( qos: . background) . async {
38- let locationServicesEnabled = CLLocationManager . locationServicesEnabled ( )
39- DispatchQueue . main. async {
40- if !locationServicesEnabled {
41- self . isLocationServicesOff = true
42- return
43- }
44-
45- self . locationManager. desiredAccuracy = kCLLocationAccuracyNearestTenMeters
46- self . locationManager. distanceFilter = 150
47- self . locationManager. startUpdatingLocation ( )
48- self . isLocationServicesOff = false
49- }
50- }
31+ self . locationManager. desiredAccuracy = kCLLocationAccuracyNearestTenMeters
32+ self . locationManager. distanceFilter = 150
33+ self . locationManager. startUpdatingLocation ( )
34+ }
35+
36+ func startUpdatingHeading( ) {
37+ locationManager. startUpdatingHeading ( )
38+ }
39+
40+ func stopUpdatingHeading( ) {
41+ locationManager. stopUpdatingHeading ( )
5142 }
5243
5344
5445 func stopUpdatingLocation( ) {
55- DispatchQueue . main. async {
56- self . locationManager. stopUpdatingLocation ( )
57- }
46+ self . locationManager. stopUpdatingLocation ( )
5847 }
5948
6049 func locationManagerDidChangeAuthorization( _ manager: CLLocationManager ) {
6150 // Check the authorization status and location services in background
62- DispatchQueue . global ( qos: . background) . async {
63- let isLocationServicesEnabled = CLLocationManager . locationServicesEnabled ( )
64- let authorizationStatus = manager. authorizationStatus
65-
66- DispatchQueue . main. async {
67- switch authorizationStatus {
68- case . authorizedWhenInUse, . authorizedAlways:
69- self . isLocationDenied = false
70- self . isLocationServicesOff = !isLocationServicesEnabled
71- self . startUpdatingLocation ( )
72- case . denied, . restricted:
73- self . isLocationDenied = true
74- self . isLocationServicesOff = false
75- self . stopUpdatingLocation ( )
76- default :
77- self . isLocationDenied = false
78- self . isLocationServicesOff = !isLocationServicesEnabled
79- self . requestLocationAuthorization ( )
80- }
81- }
51+ let authorizationStatus = manager. authorizationStatus
52+
53+ switch authorizationStatus {
54+ case . authorizedWhenInUse, . authorizedAlways:
55+ self . startUpdatingLocation ( )
56+ case . denied, . restricted:
57+ self . stopUpdatingLocation ( )
58+ default :
59+ self . requestLocationAuthorization ( )
8260 }
8361 }
8462
8563
8664 func locationManager( _ manager: CLLocationManager , didUpdateLocations locations: [ CLLocation ] ) {
65+ print ( " didUpdateLocations \( locations. count) , \( String ( describing: locations. last) ) , \( String ( describing: locationUpdateHandler) ) \( Thread . current) " )
8766 guard let mostRecentLocation = locations. last else { return }
88- DispatchQueue . main. async {
89- self . location = mostRecentLocation
90- self . locationUpdateHandler ? ( mostRecentLocation. coordinate)
91- }
67+ self . location = mostRecentLocation
68+ self . locationUpdateHandler ? ( mostRecentLocation. coordinate)
9269 }
9370
9471 func locationManager( _ manager: CLLocationManager , didUpdateHeading newHeading: CLHeading ) {
95- DispatchQueue . main. async {
96- self . headingUpdateHandler ? ( newHeading. trueHeading)
97- }
72+ self . headingUpdateHandler ? ( newHeading. trueHeading)
9873 }
9974
10075 func locationManager( _ manager: CLLocationManager , didFailWithError error: Error ) {
10176 print ( " Error with location manager is ---- \( error. localizedDescription) " )
10277 }
103-
104- private func checkInitialLocationStatus( ) {
105- // Perform initial status check asynchronously
106- DispatchQueue . global ( qos: . background) . async {
107- let servicesEnabled = CLLocationManager . locationServicesEnabled ( )
108- DispatchQueue . main. async {
109- self . isLocationServicesOff = !servicesEnabled
110- }
111- }
112- }
11378}
0 commit comments