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

VaultModel tracks LocationAuthorization #633

Merged
merged 1 commit into from
Sep 19, 2023
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
22 changes: 19 additions & 3 deletions Sources/Site/Music/VaultModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ extension VaultError: LocalizedError {
}
}

enum LocationAuthorization {
case allowed
case restricted // Locations are not possible.
case denied // Locations denied by user.
}

@MainActor public final class VaultModel: ObservableObject {
let urlString: String

Expand All @@ -36,6 +42,7 @@ extension VaultError: LocalizedError {
@Published var venuePlacemarks: [Venue.ID: CLPlacemark] = [:]
@Published var geocodedVenuesCount = 0
@Published var currentLocation: CLLocation?
@Published var locationAuthorization = LocationAuthorization.allowed

private let locationManager = LocationManager(
activityType: .other,
Expand Down Expand Up @@ -128,14 +135,23 @@ extension VaultError: LocalizedError {
defer {
Logger.vaultModel.log("end location monitoring")
}

do {
let locationStream = try await locationManager.locationStream()
for try await location in locationStream {
Logger.vaultModel.log("location received")
currentLocation = location
do {
for try await location in locationStream {
Logger.vaultModel.log("location received")
currentLocation = location
}
} catch {
Logger.vaultModel.log("location stream error: \(error, privacy: .public)")
}
} catch LocationAuthorizationError.denied {
Logger.vaultModel.log("location denied")
locationAuthorization = .denied
} catch {
Logger.vaultModel.log("location error: \(error, privacy: .public)")
locationAuthorization = .restricted
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Site/Utility/LocationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import CoreLocation

enum LocationAuthorizationError: Error {
case restricted
case denied
case restricted // Locations are not possible.
case denied // Locations denied by user.
}

extension CLAuthorizationStatus {
Expand Down