Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CLLocationUpdate #871

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Sources/Site/Music/VaultModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,13 @@ enum LocationAuthorization {
defer {
Logger.vaultModel.log("end locationstream")
}
for try await location in locationStream {
Logger.vaultModel.log("location received")
currentLocation = location
for try await update in locationStream {
if let location = update.location {
Logger.vaultModel.log("location received")
currentLocation = location
} else {
Logger.vaultModel.log("empty location update")
}
}
} catch {
Logger.vaultModel.error("locationstream error: \(error, privacy: .public)")
Expand Down
35 changes: 2 additions & 33 deletions Sources/Site/Utility/LocationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,35 +94,16 @@ actor LocationManager {
}
}

func locationStream() async throws -> LocationStream {
func locationStream() async throws -> CLLocationUpdate.Updates {
try await requestAuthorization().isStreamable()

Logger.location.log("locationStream")

return LocationStream { continuation in
continuation.onTermination = { _ in
Task { await self.terminate() }
}

delegate.locationStreamContinuation = continuation
#if !os(tvOS)
manager.startUpdatingLocation()
#else
manager.requestLocation()
#endif
}
}

private func terminate() {
delegate.locationStreamContinuation = nil
manager.stopUpdatingLocation()
return CLLocationUpdate.liveUpdates()
}

private class Delegate: NSObject, CLLocationManagerDelegate {
typealias AuthorizationContinuation = CheckedContinuation<CLAuthorizationStatus, Never>

var authorizationStreamContinuation: AuthorizationContinuation?
var locationStreamContinuation: LocationStream.Continuation?

func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
Logger.location.log("delegate authorization")
Expand All @@ -139,18 +120,6 @@ actor LocationManager {
}

Logger.location.log("delegate error: \(error, privacy: .public)")
locationStreamContinuation?.finish(throwing: error)
locationStreamContinuation = nil
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
Logger.location.log("delegate locations: \(locations.count, privacy: .public)")

guard let continuation = locationStreamContinuation else { return }

locations.forEach {
continuation.yield($0)
}
}
}
}