This commit is contained in:
fmodf 2024-07-05 11:58:31 +02:00
parent ac850bfe4a
commit ce3bc42fbb

View file

@ -4,32 +4,39 @@ import SwiftUI
struct AttachmentLocationPickerView: View { struct AttachmentLocationPickerView: View {
@StateObject var locationManager = LocationManager() @StateObject var locationManager = LocationManager()
@State private var region = MKCoordinateRegion() @State private var region = MKCoordinateRegion()
@State private var userInteraction = false
var body: some View { var body: some View {
ZStack { ZStack {
// MapView // MapView
MapView( MapView(region: $region)
region: $region, .onAppear {
userInteraction: $userInteraction DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
) self.region = locationManager.region
}
}
.overlay {
Image(systemName: "mappin")
.foregroundColor(.Material.Elements.active)
.font(.system(size: 30))
.shadow(color: .white, radius: 2)
}
// Track button // Track button
let img = userInteraction ? "location.circle" : "location.circle.fill"
VStack { VStack {
Spacer() Spacer()
HStack { HStack {
Spacer() Spacer()
Image(systemName: img) Image(systemName: "location.circle")
.resizable() .resizable()
.frame(width: 30, height: 30) .frame(width: 40, height: 40)
.foregroundColor(.Material.Elements.active) .foregroundColor(.Material.Elements.active)
.background(Color.Material.Shape.white) .background(Color.Material.Shape.white)
.clipShape(Circle()) .clipShape(Circle())
.padding(16)
.shadow(color: .white, radius: 2) .shadow(color: .white, radius: 2)
.padding(.trailing)
.padding(.bottom, 50)
.tappablePadding(.symmetric(10)) { .tappablePadding(.symmetric(10)) {
userInteraction = false self.region = locationManager.region
} }
} }
} }
@ -37,17 +44,11 @@ struct AttachmentLocationPickerView: View {
.onAppear { .onAppear {
locationManager.start() locationManager.start()
} }
.onChange(of: locationManager.region) { region in
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()
@ -59,7 +60,7 @@ struct MapView: UIViewRepresentable {
} }
func updateUIView(_ uiView: MKMapView, context _: Context) { func updateUIView(_ uiView: MKMapView, context _: Context) {
if uiView.region != region, !userInteraction { if uiView.region != region {
uiView.setRegion(region, animated: true) uiView.setRegion(region, animated: true)
} }
} }