This commit is contained in:
fmodf 2024-07-05 10:43:40 +02:00
parent c9021b964a
commit ac850bfe4a

View file

@ -3,34 +3,63 @@ import SwiftUI
struct AttachmentLocationPickerView: View { struct AttachmentLocationPickerView: View {
@StateObject var locationManager = LocationManager() @StateObject var locationManager = LocationManager()
@State var region = MKCoordinateRegion() @State private var region = MKCoordinateRegion()
@State private var userInteraction = false
var body: some View { var body: some View {
ZStack { ZStack {
MapView(region: $region) // MapView
MapView(
region: $region,
userInteraction: $userInteraction
)
// Track button
let img = userInteraction ? "location.circle" : "location.circle.fill"
VStack {
Spacer()
HStack {
Spacer()
Image(systemName: img)
.resizable()
.frame(width: 30, height: 30)
.foregroundColor(.Material.Elements.active)
.background(Color.Material.Shape.white)
.clipShape(Circle())
.padding(16)
.shadow(color: .white, radius: 2)
.tappablePadding(.symmetric(10)) {
userInteraction = false
}
}
}
} }
.onAppear { .onAppear {
locationManager.start() locationManager.start()
} }
.onChange(of: locationManager.region) { region in .onChange(of: locationManager.region) { region in
self.region = region if !userInteraction {
self.region = region
}
} }
} }
} }
struct MapView: UIViewRepresentable { struct MapView: UIViewRepresentable {
@Binding var region: MKCoordinateRegion @Binding var region: MKCoordinateRegion
@Binding var userInteraction: Bool
func makeUIView(context: Context) -> MKMapView { func makeUIView(context: Context) -> MKMapView {
let mapView = MKMapView() let mapView = MKMapView()
mapView.delegate = context.coordinator mapView.delegate = context.coordinator
mapView.showsUserLocation = true mapView.showsUserLocation = false
mapView.userTrackingMode = .none mapView.userTrackingMode = .none
return mapView return mapView
} }
func updateUIView(_ uiView: MKMapView, context _: Context) { func updateUIView(_ uiView: MKMapView, context _: Context) {
if uiView.region != region { if uiView.region != region, !userInteraction {
uiView.setRegion(region, animated: true) uiView.setRegion(region, animated: true)
} }
} }
@ -48,7 +77,7 @@ struct MapView: UIViewRepresentable {
} }
} }
class LocationManager: NSObject, ObservableObject { class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
private let locationManager = CLLocationManager() private let locationManager = CLLocationManager()
@Published var region: MKCoordinateRegion @Published var region: MKCoordinateRegion
@ -67,9 +96,7 @@ class LocationManager: NSObject, ObservableObject {
func stop() { func stop() {
locationManager.stopUpdatingLocation() locationManager.stopUpdatingLocation()
} }
}
extension LocationManager: CLLocationManagerDelegate {
func locationManager(_: CLLocationManager, didUpdateLocations locations: [CLLocation]) { func locationManager(_: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let loc = locations.first { if let loc = locations.first {
region = MKCoordinateRegion( region = MKCoordinateRegion(