From 2039fcdc375fdf051b344d782fa22949510892b4 Mon Sep 17 00:00:00 2001 From: ivar Date: Mon, 23 Mar 2026 16:57:21 +0100 Subject: feat: add location permission request and caching --- Solverv.xcodeproj/project.pbxproj | 2 ++ Solverv/SolvervApp.swift | 76 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) 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() + + 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() + } +} -- cgit v1.3