Skip to content

Commit 9df417f

Browse files
committed
Updated Location manager handling and removed unnessary thread switchings.
1 parent fdba5fe commit 9df417f

File tree

4 files changed

+37
-74
lines changed

4 files changed

+37
-74
lines changed

GoInfoGame/GoInfoGame/Kartaview/ViewModel/KartaviewViewModel.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ class KartaviewViewModel: ObservableObject {
2424
init(capturedImage: UIImage) {
2525
self.capturedImage = capturedImage
2626

27-
locationManagerDelegate.locationManager.delegate = locationManagerDelegate
28-
locationManagerDelegate.locationManager.requestWhenInUseAuthorization()
29-
locationManagerDelegate.locationManager.startUpdatingLocation()
30-
locationManagerDelegate.locationManager.startUpdatingHeading()
31-
3227
locationManagerDelegate.locationUpdateHandler = { [weak self] location in
3328
guard let self = self else { return }
3429
self.location = location
@@ -37,8 +32,12 @@ class KartaviewViewModel: ObservableObject {
3732
locationManagerDelegate.headingUpdateHandler = { [weak self] heading in
3833
guard let self = self else { return }
3934
self.heading = "\(heading)"
40-
locationManagerDelegate.locationManager.stopUpdatingHeading()
35+
locationManagerDelegate.stopUpdatingHeading()
4136
}
37+
38+
locationManagerDelegate.requestLocationAuthorization()
39+
locationManagerDelegate.startUpdatingLocation()
40+
locationManagerDelegate.startUpdatingHeading()
4241
}
4342

4443
// Step 1: Create Sequence

GoInfoGame/GoInfoGame/LocationManagerDelegate.swift

Lines changed: 27 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ import CoreLocation
99
import Foundation
1010

1111
class 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
}

GoInfoGame/GoInfoGame/UI/InitialView/InitialViewModel.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ class InitialViewModel: ObservableObject {
2323

2424

2525
init() {
26-
locationManagerDelegate.locationManager.delegate = locationManagerDelegate
27-
locationManagerDelegate.locationManager.requestWhenInUseAuthorization()
28-
locationManagerDelegate.locationManager.startUpdatingLocation()
29-
3026
locationManagerDelegate.locationUpdateHandler = { [weak self] location in
3127
guard let self = self else { return }
3228
fetchWorkspacesList()
3329
}
30+
31+
locationManagerDelegate.requestLocationAuthorization()
32+
locationManagerDelegate.startUpdatingLocation()
3433
}
3534

3635
// fetch workspaces list

GoInfoGame/GoInfoGame/UI/Map/CustomMap.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ struct CustomMap: UIViewRepresentable {
555555

556556
// calculate distance between user current location and selected annotation
557557
func calculateDistance(selectedAnnotation: CLLocationCoordinate2D) -> CLLocationDistance {
558-
guard let userCurrentLocation = locationManagerDelegate.locationManager.location?.coordinate else { return CLLocationDistance(0) }
558+
guard let userCurrentLocation = locationManagerDelegate.location?.coordinate else { return CLLocationDistance(0) }
559559

560560
let fromLocation = CLLocation(latitude: userCurrentLocation.latitude, longitude: userCurrentLocation.longitude)
561561
let toLocation = CLLocation(latitude: selectedAnnotation.latitude, longitude: selectedAnnotation.longitude)
@@ -564,7 +564,7 @@ struct CustomMap: UIViewRepresentable {
564564

565565
// infer direction
566566
func inferDirection(selectedAnnotation: CLLocationCoordinate2D) -> String {
567-
guard let userCurrentLocation = locationManagerDelegate.locationManager.location?.coordinate else { return "undetermined" }
567+
guard let userCurrentLocation = locationManagerDelegate.location?.coordinate else { return "undetermined" }
568568
let userLocationPoint = MKMapPoint(userCurrentLocation)
569569
let destinationPoint = MKMapPoint(selectedAnnotation)
570570
let angleRadians = atan2(destinationPoint.y - userLocationPoint.y, destinationPoint.x - userLocationPoint.x)

0 commit comments

Comments
 (0)