Skip to content

Commit

Permalink
Use CLLocationUpdate in more places with safeguards
Browse files Browse the repository at this point in the history
  • Loading branch information
daneden committed Jun 20, 2024
1 parent 5e6db2f commit 4e0fefb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
34 changes: 19 additions & 15 deletions Solstice/Helpers/CurrentLocation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ class CurrentLocation: NSObject, ObservableObject, ObservableLocation, Identifia
}

func requestAccess() {
#if os(macOS)
#if os(macOS)
self.locationManager.requestAlwaysAuthorization()
#else
#else
self.locationManager.requestWhenInUseAuthorization()
#endif
#endif
}
}

Expand All @@ -78,12 +78,16 @@ extension CurrentLocation {
}

func requestLocation() {

if #available(iOS 17, watchOS 10, macOS 14, *) {
Task {
do {
for try await update in CLLocationUpdate.liveUpdates() {
self.location = update.location
guard let location = update.location else { return }
guard let storedLocation = self.location else { return self.location = location }

if storedLocation.distance(from: location) > 200 {
self.location = location
}
}
} catch {
print(error.localizedDescription)
Expand All @@ -93,9 +97,9 @@ extension CurrentLocation {
locationManager.requestLocation()
locationManager.startUpdatingLocation()

#if !os(watchOS) && !os(visionOS)
#if !os(watchOS) && !os(visionOS)
locationManager.startMonitoringSignificantLocationChanges()
#endif
#endif
}
}

Expand All @@ -111,19 +115,19 @@ extension CurrentLocation {
}

var isAuthorizedForWidgetUpdates: Bool {
#if !os(watchOS)
#if !os(watchOS)
locationManager.isAuthorizedForWidgetUpdates
#else
#else
isAuthorized
#endif
#endif
}
}

extension CurrentLocation: CLLocationManagerDelegate {
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
#if canImport(WidgetKit)
#if canImport(WidgetKit)
WidgetCenter.shared.reloadAllTimelines()
#endif
#endif

if isAuthorized { requestLocation() }
}
Expand All @@ -136,7 +140,7 @@ extension CurrentLocation: CLLocationManagerDelegate {
if location == nil {
updateLocation(newLocation)
} else if let newLocation, let location,
newLocation.distance(from: location) > CLLocationDistance(10_000) {
newLocation.distance(from: location) > CLLocationDistance(10_000) {
updateLocation(newLocation)
} else {
print("Location is within 10km of last update")
Expand All @@ -149,9 +153,9 @@ extension CurrentLocation: CLLocationManagerDelegate {
if newLocation != nil {
Task { await NotificationManager.scheduleNotifications(locationManager: self) }

#if canImport(WidgetKit)
#if canImport(WidgetKit)
WidgetCenter.shared.reloadAllTimelines()
#endif
#endif
}
}

Expand Down
14 changes: 13 additions & 1 deletion Widget/Helpers/SolsticeWidgetTimelineProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,19 @@ fileprivate class LocationManager: NSObject, CLLocationManagerDelegate {
}

func requestLocation() {
locationManager.requestLocation()
if #available(iOSApplicationExtension 17, watchOSApplicationExtension 10, macOSApplicationExtension 14, *) {
Task {
for try await update in CLLocationUpdate.liveUpdates() {
guard let location = update.location else { return }
for callback in updateLocationsCallbacks {
callback([location])
}
break
}
}
} else {
locationManager.requestLocation()
}
}

var location: CLLocation? { locationManager.location }
Expand Down

0 comments on commit 4e0fefb

Please sign in to comment.