From 53a5e7b5d2390ceb07d59f229b0b559c9b58546f Mon Sep 17 00:00:00 2001 From: Vishnu Ravi Date: Sat, 11 Jan 2025 12:04:09 -0500 Subject: [PATCH 1/3] Update fetching logic --- LifeSpace/Location/LocationModule.swift | 29 ++++++++++++++----------- LifeSpace/Map/LifeSpaceMapView.swift | 14 +++++++----- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/LifeSpace/Location/LocationModule.swift b/LifeSpace/Location/LocationModule.swift index 7c3dea0..08f43e5 100644 --- a/LifeSpace/Location/LocationModule.swift +++ b/LifeSpace/Location/LocationModule.swift @@ -21,6 +21,7 @@ public class LocationModule: NSObject, CLLocationManagerDelegate, Module, Defaul private let storage = LocationStorage() public var onLocationsUpdated: (([CLLocationCoordinate2D]) -> Void)? + private var currentFetchTask: Task? public var allLocations: [CLLocationCoordinate2D] { get async { @@ -66,20 +67,22 @@ public class LocationModule: NSObject, CLLocationManagerDelegate, Module, Defaul self.manager.requestAlwaysAuthorization() } - public func fetchLocations() async { - do { - if let locations = try await standard?.fetchLocations() { - await storage.updateAllLocations(locations) - if let callback = onLocationsUpdated { - let currentLocations = await storage.getAllLocations() - await MainActor.run { - callback(currentLocations) - } - } + public func fetchLocations() async throws { + // Cancel any ongoing fetch + currentFetchTask?.cancel() + + let task = Task { @MainActor in + let locations = try await standard?.fetchLocations() ?? [] + + await storage.updateAllLocations(locations) + if let callback = onLocationsUpdated { + let currentLocations = await storage.getAllLocations() + callback(currentLocations) } - } catch { - logger.error("Error fetching locations: \(error.localizedDescription)") } + + currentFetchTask = task + try await task.value } /// Adds a new coordinate to the map and database, @@ -111,7 +114,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 } diff --git a/LifeSpace/Map/LifeSpaceMapView.swift b/LifeSpace/Map/LifeSpaceMapView.swift index 0193323..b1ccd87 100644 --- a/LifeSpace/Map/LifeSpaceMapView.swift +++ b/LifeSpace/Map/LifeSpaceMapView.swift @@ -63,11 +63,6 @@ struct LifeSpaceMapView: View { } } } - .onChange(of: scenePhase) { _, newPhase in - if newPhase == .active { - refreshMap() - } - } .onAppear { refreshMap() } @@ -111,9 +106,16 @@ 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 + showingSurveyAlert = true + } isRefreshing = false } } From 06adabd5eae0ed39ebf7bbdaf01b19d264b6a5b2 Mon Sep 17 00:00:00 2001 From: Vishnu Ravi Date: Sat, 11 Jan 2025 14:41:35 -0500 Subject: [PATCH 2/3] Rollback --- LifeSpace/Location/LocationModule.swift | 26 ++++++++++++------------- LifeSpace/Map/LifeSpaceMapView.swift | 6 ++++-- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/LifeSpace/Location/LocationModule.swift b/LifeSpace/Location/LocationModule.swift index 08f43e5..7f7c92b 100644 --- a/LifeSpace/Location/LocationModule.swift +++ b/LifeSpace/Location/LocationModule.swift @@ -21,7 +21,6 @@ public class LocationModule: NSObject, CLLocationManagerDelegate, Module, Defaul private let storage = LocationStorage() public var onLocationsUpdated: (([CLLocationCoordinate2D]) -> Void)? - private var currentFetchTask: Task? public var allLocations: [CLLocationCoordinate2D] { get async { @@ -68,21 +67,20 @@ public class LocationModule: NSObject, CLLocationManagerDelegate, Module, Defaul } public func fetchLocations() async throws { - // Cancel any ongoing fetch - currentFetchTask?.cancel() - - let task = Task { @MainActor in - let locations = try await standard?.fetchLocations() ?? [] - - await storage.updateAllLocations(locations) - if let callback = onLocationsUpdated { - let currentLocations = await storage.getAllLocations() - callback(currentLocations) + do { + if let locations = try await standard?.fetchLocations() { + await storage.updateAllLocations(locations) + if let callback = onLocationsUpdated { + let currentLocations = await storage.getAllLocations() + await MainActor.run { + callback(currentLocations) + } + } } + } catch { + logger.error("Error fetching locations: \(error.localizedDescription)") + throw error } - - currentFetchTask = task - try await task.value } /// Adds a new coordinate to the map and database, diff --git a/LifeSpace/Map/LifeSpaceMapView.swift b/LifeSpace/Map/LifeSpaceMapView.swift index b1ccd87..b2be966 100644 --- a/LifeSpace/Map/LifeSpaceMapView.swift +++ b/LifeSpace/Map/LifeSpaceMapView.swift @@ -14,7 +14,6 @@ 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 @@ -60,6 +59,7 @@ struct LifeSpaceMapView: View { Image(systemName: "arrow.clockwise") .accessibilityLabel("REFRESHING_MAP") } + .disabled(isRefreshing) } } } @@ -106,7 +106,9 @@ struct LifeSpaceMapView: View { } private func refreshMap() { - guard !isRefreshing else { return } + guard !isRefreshing else { + return + } Task { isRefreshing = true From 2337f92d7ea53936518b11d3b8637590f2334675 Mon Sep 17 00:00:00 2001 From: Vishnu Ravi Date: Sat, 11 Jan 2025 15:01:02 -0500 Subject: [PATCH 3/3] Update alert --- LifeSpace/Map/LifeSpaceMapView.swift | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/LifeSpace/Map/LifeSpaceMapView.swift b/LifeSpace/Map/LifeSpaceMapView.swift index b2be966..9be53b4 100644 --- a/LifeSpace/Map/LifeSpaceMapView.swift +++ b/LifeSpace/Map/LifeSpaceMapView.swift @@ -18,7 +18,7 @@ struct LifeSpaceMapView: View { @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 @@ -63,6 +63,13 @@ struct LifeSpaceMapView: View { } } } + .alert("Error", isPresented: $showingAlert) { + Button("OK", role: .cancel) { + showingAlert = false + } + } message: { + Text(alertMessage) + } .onAppear { refreshMap() } @@ -116,7 +123,7 @@ struct LifeSpaceMapView: View { try await locationModule.fetchLocations() } catch { alertMessage = error.localizedDescription - showingSurveyAlert = true + showingAlert = true } isRefreshing = false }