Skip to content

Commit 576d7f1

Browse files
committed
More robust location manager delegate handling
1 parent 0e08058 commit 576d7f1

1 file changed

Lines changed: 31 additions & 31 deletions

File tree

Sources/SkipDevice/LocationProvider.swift

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,11 @@ public class LocationProvider: NSObject {
7575
let (stream, continuation) = AsyncThrowingStream.makeStream(of: LocationEvent.self)
7676

7777
#if SKIP
78-
listener = LocListener(callback: { continuation.yield(with: .success($0)) })
79-
let intervalMillis = Int64(0)
78+
listener = LocListener(callback: { location in
79+
logger.info("location update: \(location.latitude) \(location.longitude)")
80+
continuation.yield(with: .success(location))
81+
})
82+
let intervalMillis = Int64(1_000)
8083
// https://developer.android.com/reference/android/location/LocationRequest.Builder
8184
let request = LocationRequest.Builder(intervalMillis).build() // TODO: setQuality, etc.
8285
do {
@@ -89,13 +92,14 @@ public class LocationProvider: NSObject {
8992
self.callback = { result in
9093
switch result {
9194
case .success(let location):
95+
logger.info("location update: \(location.latitude) \(location.longitude)")
9296
continuation.yield(with: .success(location))
9397
case .failure(let error):
9498
continuation.yield(with: .failure(error))
9599
self.callback = nil
96100
}
97101
}
98-
requestLocationOrAuthorization()
102+
locationManager.startUpdatingLocation()
99103
#endif
100104

101105
continuation.onTermination = { [weak self] _ in
@@ -108,40 +112,43 @@ public class LocationProvider: NSObject {
108112

109113
/// Issues a single-shot request for the current location
110114
public func fetchCurrentLocation() async throws -> LocationEvent {
111-
logger.debug("fetchCurrentLocation")
115+
logger.info("fetchCurrentLocation")
112116
#if !SKIP
113117
return try await withCheckedThrowingContinuation { continuation in
114118
self.callback = { result in
115119
switch result {
116120
case .success(let location):
117121
continuation.resume(returning: location)
122+
self.locationManager.stopUpdatingLocation()
118123
self.callback = nil
119124
case .failure(let error):
120125
continuation.resume(throwing: error)
126+
self.locationManager.stopUpdatingLocation()
121127
self.callback = nil
122128
}
123129
}
124-
requestLocationOrAuthorization()
130+
locationManager.startUpdatingLocation()
125131
}
126132
#else
127-
return suspendCancellableCoroutine { continuation in
128-
let context = ProcessInfo.processInfo.androidContext
129-
let locationManager = context.getSystemService(Context.LOCATION_SERVICE) as android.location.LocationManager
130-
131-
let locationListener = LocListener()
132-
133+
let context = ProcessInfo.processInfo.androidContext
134+
let locationManager = context.getSystemService(Context.LOCATION_SERVICE) as android.location.LocationManager
135+
let locationListener = LocListener()
136+
let location = suspendCancellableCoroutine { continuation in
133137
locationListener.callback = {
134138
locationManager.removeUpdates(locationListener)
135139
continuation.resume($0)
136140
}
137141

138-
locationManager.requestSingleUpdate(android.location.LocationManager.GPS_PROVIDER, locationListener, android.os.Looper.getMainLooper())
139-
140142
continuation.invokeOnCancellation { _ in
141143
locationManager.removeUpdates(locationListener)
142144
continuation.cancel()
143145
}
146+
147+
logger.info("locationManager.requestSingleUpdate")
148+
locationManager.requestSingleUpdate(android.location.LocationManager.GPS_PROVIDER, locationListener, Looper.getMainLooper())
144149
}
150+
let _ = locationListener // need to hold the reference so it doesn't get gc'd
151+
return location
145152
#endif
146153
}
147154
}
@@ -160,33 +167,26 @@ struct LocListener : LocationListener {
160167
}
161168
#else
162169
extension LocationProvider: CLLocationManagerDelegate {
163-
private func requestLocationOrAuthorization() {
164-
switch locationManager.authorizationStatus {
165-
case .notDetermined:
166-
locationManager.requestWhenInUseAuthorization()
167-
default:
168-
locationManager.startUpdatingLocation()
169-
}
170-
}
171-
172-
public func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
173-
logger.info("locationManager.locationManagerDidChangeAuthorization")
174-
if callback != nil {
175-
requestLocationOrAuthorization()
176-
}
177-
}
178-
179170
public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
180-
logger.debug("locationManager.didUpdateLocations: \(locations)")
171+
logger.info("LocationProvider.didUpdateLocations: \(locations)")
181172
for location in locations {
182173
callback?(.success(LocationEvent(location: location)))
183174
}
184175
}
185176

186177
public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
187-
logger.error("locationManager.didFailWithError: \(error)")
178+
logger.error("LocationProvider.didFailWithError: \(error)")
188179
callback?(.failure(error))
189180
}
181+
182+
public func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: any Error) {
183+
logger.error("LocationProvider.monitoringDidFailFor: \(error)")
184+
callback?(.failure(error))
185+
}
186+
187+
public func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
188+
logger.info("LocationProvider.locationManagerDidChangeAuthorization: \(manager.authorizationStatus.rawValue)")
189+
}
190190
}
191191
#endif
192192

0 commit comments

Comments
 (0)