From c3679c9a2ac0f7619a18f7f54b380c4e458eaeb5 Mon Sep 17 00:00:00 2001 From: fmodf Date: Thu, 4 Jul 2024 16:25:25 +0200 Subject: [PATCH] wip --- .../AttachmentLocationPickerView.swift | 93 ++++++------------- 1 file changed, 28 insertions(+), 65 deletions(-) diff --git a/ConversationsClassic/View/Screens/Attachments/AttachmentLocationPickerView.swift b/ConversationsClassic/View/Screens/Attachments/AttachmentLocationPickerView.swift index 1eac91b..49c3f7d 100644 --- a/ConversationsClassic/View/Screens/Attachments/AttachmentLocationPickerView.swift +++ b/ConversationsClassic/View/Screens/Attachments/AttachmentLocationPickerView.swift @@ -1,79 +1,42 @@ +import Combine import CoreLocation import MapKit import SwiftUI struct AttachmentLocationPickerView: View { - @StateObject var locationManager = LocationManager() - @State private var region = MKCoordinateRegion() - @State var userInteracted: Bool = false + @State var region = MKCoordinateRegion() + @State var userTrackingMode: MapUserTrackingMode = .follow var body: some View { - Map( - coordinateRegion: $region, - interactionModes: .all, - showsUserLocation: false, - userTrackingMode: .none - ) - .onAppear { - locationManager.start() - } - .onDisappear { - locationManager.stop() - } - .onChange(of: locationManager.region) { region in - if !userInteracted { - let currentLoc = CLLocation(latitude: self.region.center.latitude, longitude: self.region.center.longitude) - let newLoc = CLLocation(latitude: region.center.latitude, longitude: region.center.longitude) - if newLoc.distance(from: currentLoc) > 10 { - DispatchQueue.main.async { - withAnimation { - self.region = region + ZStack { + Map( + coordinateRegion: $region, + interactionModes: .all, + showsUserLocation: false, + userTrackingMode: $userTrackingMode + ) + .overlay { + Image(systemName: "mappin") + .resizable() + .frame(width: 30, height: 30) + .foregroundColor(.Material.Elements.active) + if userTrackingMode != .follow { + VStack { + Spacer() + HStack { + Spacer() + Image(systemName: "location.north.circle.fill") + .resizable() + .frame(width: 30, height: 30) + .foregroundColor(.Material.Elements.active) + .padding() + .tappablePadding(.symmetric(10)) { + userTrackingMode = .follow + } } } } } } - .overlay { - Image(systemName: "mappin") - .font(.title2) - .foregroundColor(.Material.Elements.active) - } - .onChange(of: region) { region in - print("Region changed: \(region.center)") - } - } -} - -class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate { - @Published var region = MKCoordinateRegion() - private let locationManager = CLLocationManager() - - override init() { - super.init() - locationManager.delegate = self - locationManager.desiredAccuracy = kCLLocationAccuracyBest - } - - func start() { - locationManager.requestWhenInUseAuthorization() - locationManager.startUpdatingLocation() - } - - func stop() { - locationManager.stopUpdatingLocation() - } - - func locationManager(_: CLLocationManager, didUpdateLocations locations: [CLLocation]) { - guard let location = locations.last else { return } - region = MKCoordinateRegion(center: location.coordinate, span: MKCoordinateSpan(latitudeDelta: 0.001, longitudeDelta: 0.001)) - } -} - -extension MKCoordinateRegion: Equatable { - public static func == (lhs: MKCoordinateRegion, rhs: MKCoordinateRegion) -> Bool { - lhs.center.latitude == rhs.center.latitude && - lhs.center.longitude == rhs.center.longitude && - lhs.span.latitudeDelta == rhs.span.latitudeDelta && - lhs.span.longitudeDelta == rhs.span.longitudeDelta } }