From 25f56bca22ad45499781a6910d62684ff84f7279 Mon Sep 17 00:00:00 2001 From: Krystof Date: Thu, 16 May 2024 00:38:55 +0200 Subject: [PATCH] feat(app): real time location --- app/metro-now/metro-now/Core/TabBar/MainTabView.swift | 4 ++++ app/metro-now/metro-now/Models/LocationModel.swift | 11 ++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/metro-now/metro-now/Core/TabBar/MainTabView.swift b/app/metro-now/metro-now/Core/TabBar/MainTabView.swift index d571bdc8..12238b4e 100644 --- a/app/metro-now/metro-now/Core/TabBar/MainTabView.swift +++ b/app/metro-now/metro-now/Core/TabBar/MainTabView.swift @@ -10,6 +10,7 @@ import SwiftUI struct MainTabView: View { @StateObject private var locationModel = LocationModel() + let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect() var body: some View { TabView { @@ -23,6 +24,9 @@ struct MainTabView: View { Label("Map", systemImage: "map") } } + .onReceive(locationModel.$location) { location in + print("User's location: \(location)") + } .onAppear { locationModel.checkLocationServicesEnabled() } diff --git a/app/metro-now/metro-now/Models/LocationModel.swift b/app/metro-now/metro-now/Models/LocationModel.swift index 3a0933bb..5647ba84 100644 --- a/app/metro-now/metro-now/Models/LocationModel.swift +++ b/app/metro-now/metro-now/Models/LocationModel.swift @@ -9,6 +9,7 @@ import Foundation import MapKit final class LocationModel: NSObject, ObservableObject, CLLocationManagerDelegate { + @Published var location: CLLocation? var locationManager: CLLocationManager? func checkLocationServicesEnabled() { @@ -16,12 +17,20 @@ final class LocationModel: NSObject, ObservableObject, CLLocationManagerDelegate if isEnabled { locationManager = CLLocationManager() - locationManager!.delegate = self + if let locationManager { + locationManager.delegate = self + locationManager.distanceFilter = 10.0 + locationManager.startUpdatingLocation() + } } else { print("Location services not enabled") } } + func locationManager(_: CLLocationManager, didUpdateLocations locations: [CLLocation]) { + location = locations.last + } + private func checkLocationAuthorization() { guard let locationManager else { return }