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

Fix location error propagation #55

Merged
merged 4 commits into from
Jan 11, 2025
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
5 changes: 3 additions & 2 deletions LifeSpace/Location/LocationModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class LocationModule: NSObject, CLLocationManagerDelegate, Module, Defaul
self.manager.requestAlwaysAuthorization()
}

public func fetchLocations() async {
public func fetchLocations() async throws {
do {
if let locations = try await standard?.fetchLocations() {
await storage.updateAllLocations(locations)
Expand All @@ -79,6 +79,7 @@ public class LocationModule: NSObject, CLLocationManagerDelegate, Module, Defaul
}
} catch {
logger.error("Error fetching locations: \(error.localizedDescription)")
throw error
}
}

Expand Down Expand Up @@ -111,7 +112,7 @@ public class LocationModule: NSObject, CLLocationManagerDelegate, Module, Defaul
/// Check if the date of the current point is a different day then the last saved point. If so,
/// Refresh the locations array and save this point.
if Date().startOfDay != lastSaved.date.startOfDay {
await fetchLocations()
try? await fetchLocations()
return true
}

Expand Down
23 changes: 17 additions & 6 deletions LifeSpace/Map/LifeSpaceMapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ import SwiftUI
struct LifeSpaceMapView: View {
@AppStorage(StorageKeys.trackingPreference) private var trackingOn = true
@Environment(LocationModule.self) private var locationModule
@Environment(\.scenePhase) var scenePhase

@State private var presentedContext: EventContext?
@Binding private var presentingAccount: Bool

@State private var showingSurveyAlert = false
@State private var showingAlert = false
@State private var alertMessage = ""
@State private var showingSurvey = false
@State private var optionsPanelOpen = true
Expand Down Expand Up @@ -60,13 +59,16 @@ struct LifeSpaceMapView: View {
Image(systemName: "arrow.clockwise")
.accessibilityLabel("REFRESHING_MAP")
}
.disabled(isRefreshing)
}
}
}
.onChange(of: scenePhase) { _, newPhase in
if newPhase == .active {
refreshMap()
.alert("Error", isPresented: $showingAlert) {
Button("OK", role: .cancel) {
showingAlert = false
}
} message: {
Text(alertMessage)
}
.onAppear {
refreshMap()
Expand Down Expand Up @@ -111,9 +113,18 @@ struct LifeSpaceMapView: View {
}

private func refreshMap() {
guard !isRefreshing else {
return
}

Task {
isRefreshing = true
await locationModule.fetchLocations()
do {
try await locationModule.fetchLocations()
} catch {
alertMessage = error.localizedDescription
showingAlert = true
}
isRefreshing = false
}
}
Expand Down
Loading