payfrit-food-ios/PayfritFood/Services/LocationService.swift
John Pinkyfloyd 71e7ec34f6 Initial commit: PayfritFood iOS app
- SwiftUI + async/await architecture
- Barcode scanning with AVFoundation
- Product display with score ring, NOVA badge, nutrition
- Alternatives with sort/filter
- Auth (login/register)
- Favorites & history
- Account management
- Dark theme
- Connected to food.payfrit.com API (Open Food Facts proxy)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-03-16 16:58:21 -07:00

51 lines
1.6 KiB
Swift

import CoreLocation
import SwiftUI
class LocationService: NSObject, ObservableObject {
@Published var currentLocation: CLLocation?
@Published var authorizationStatus: CLAuthorizationStatus = .notDetermined
@Published var error: String?
private let locationManager = CLLocationManager()
override init() {
super.init()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
authorizationStatus = locationManager.authorizationStatus
}
func requestPermission() {
locationManager.requestWhenInUseAuthorization()
}
func requestLocation() {
guard authorizationStatus == .authorizedWhenInUse || authorizationStatus == .authorizedAlways else {
requestPermission()
return
}
locationManager.requestLocation()
}
func hasPermission() -> Bool {
return authorizationStatus == .authorizedWhenInUse || authorizationStatus == .authorizedAlways
}
}
// MARK: - CLLocationManagerDelegate
extension LocationService: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
currentLocation = locations.last
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
self.error = error.localizedDescription
}
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
authorizationStatus = manager.authorizationStatus
if hasPermission() {
requestLocation()
}
}
}