diff options
| author | ivar <i@oiee.no> | 2026-03-23 16:57:21 +0100 |
|---|---|---|
| committer | ivar <i@oiee.no> | 2026-03-23 16:57:21 +0100 |
| commit | 2039fcdc375fdf051b344d782fa22949510892b4 (patch) | |
| tree | 189b402e5e6604bab37ffa31083775bfb759f052 | |
| parent | ed86502d27d6f03cbdee0d4c2d34cd4b0a6a3d3e (diff) | |
| download | solverv-2039fcdc375fdf051b344d782fa22949510892b4.tar.xz solverv-2039fcdc375fdf051b344d782fa22949510892b4.zip | |
feat: add location permission request and caching
| -rw-r--r-- | Solverv.xcodeproj/project.pbxproj | 2 | ||||
| -rw-r--r-- | Solverv/SolvervApp.swift | 76 |
2 files changed, 78 insertions, 0 deletions
diff --git a/Solverv.xcodeproj/project.pbxproj b/Solverv.xcodeproj/project.pbxproj index 64b581c..8819bbc 100644 --- a/Solverv.xcodeproj/project.pbxproj +++ b/Solverv.xcodeproj/project.pbxproj @@ -378,6 +378,7 @@ DEVELOPMENT_TEAM = ZH95BL9NUK; ENABLE_PREVIEWS = YES; GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_KEY_NSLocationWhenInUseUsageDescription = "We need your location to calculate sunrise and sunset times for your area."; INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; INFOPLIST_KEY_UILaunchScreen_Generation = YES; @@ -410,6 +411,7 @@ DEVELOPMENT_TEAM = ZH95BL9NUK; ENABLE_PREVIEWS = YES; GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_KEY_NSLocationWhenInUseUsageDescription = "We need your location to calculate sunrise and sunset times for your area."; INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; INFOPLIST_KEY_UILaunchScreen_Generation = YES; diff --git a/Solverv/SolvervApp.swift b/Solverv/SolvervApp.swift index 0833363..a2beb1d 100644 --- a/Solverv/SolvervApp.swift +++ b/Solverv/SolvervApp.swift @@ -7,9 +7,13 @@ import SwiftUI import SwiftData +import CoreLocation +import Combine @main struct SolvervApp: App { + @StateObject private var locationManager = LocationManager() + var sharedModelContainer: ModelContainer = { let schema = Schema([ Item.self, @@ -26,7 +30,79 @@ struct SolvervApp: App { var body: some Scene { WindowGroup { ContentView() + .onAppear { + locationManager.requestLocationPermission() + } } .modelContainer(sharedModelContainer) } } + +class LocationManager: NSObject, CLLocationManagerDelegate, ObservableObject { + let manager = CLLocationManager() + @Published var objectWillChange = PassthroughSubject<Void, Never>() + + override init() { + super.init() + manager.delegate = self + } + + func requestLocationPermission() { + if manager.authorizationStatus == .notDetermined { + manager.requestWhenInUseAuthorization() + } else if manager.authorizationStatus == .authorizedWhenInUse || manager.authorizationStatus == .authorizedAlways { + fetchLocation() + } else { + // Use default location (Greenwich) + let defaultLocation = AppGroupManager.UserLocation( + latitude: 0.0, + longitude: 0.0, + timestamp: ISO8601DateFormatter().string(from: Date()), + isDefaultLocation: true + ) + AppGroupManager.shared.saveLocation(defaultLocation) + } + } + + func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { + if status == .authorizedWhenInUse || status == .authorizedAlways { + fetchLocation() + } + } + + private func fetchLocation() { + manager.startUpdatingLocation() + } + + func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { + guard let location = locations.last else { return } + + let userLocation = AppGroupManager.UserLocation( + latitude: location.coordinate.latitude, + longitude: location.coordinate.longitude, + timestamp: ISO8601DateFormatter().string(from: Date()), + isDefaultLocation: false + ) + AppGroupManager.shared.saveLocation(userLocation) + + // Calculate and cache sunrise/sunset + let sunTimes = SunTimes( + latitude: location.coordinate.latitude, + longitude: location.coordinate.longitude, + date: Date() + ) + + if let sunrise = sunTimes.sunrise(), let sunset = sunTimes.sunset() { + let formatter = ISO8601DateFormatter() + let cachedTimes = AppGroupManager.SunTimes( + date: formatter.string(from: Date()).prefix(10).description, + sunrise: formatter.string(from: sunrise), + sunset: formatter.string(from: sunset), + timestamp: formatter.string(from: Date()) + ) + AppGroupManager.shared.saveSunTimes(cachedTimes) + } + + manager.stopUpdatingLocation() + } +} |
