wip
This commit is contained in:
parent
170c0daa5b
commit
c9021b964a
|
@ -1,48 +1,90 @@
|
|||
import Combine
|
||||
import CoreLocation
|
||||
import MapKit
|
||||
import SwiftUI
|
||||
|
||||
struct AttachmentLocationPickerView: View {
|
||||
@StateObject var locationManager = LocationManager()
|
||||
@State var region = MKCoordinateRegion()
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 0) {
|
||||
// Map
|
||||
Map(
|
||||
coordinateRegion: $region,
|
||||
interactionModes: .all,
|
||||
showsUserLocation: false,
|
||||
userTrackingMode: .constant(.follow)
|
||||
)
|
||||
.overlay {
|
||||
Image(systemName: "mappin")
|
||||
.font(.system(size: 30))
|
||||
.foregroundColor(.Material.Elements.active)
|
||||
ZStack {
|
||||
MapView(region: $region)
|
||||
}
|
||||
.onAppear {
|
||||
locationManager.start()
|
||||
}
|
||||
.onChange(of: locationManager.region) { region in
|
||||
self.region = region
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Send panel
|
||||
Rectangle()
|
||||
.foregroundColor(.Material.Shape.black)
|
||||
.frame(maxWidth: .infinity)
|
||||
.frame(height: 50)
|
||||
.overlay {
|
||||
HStack {
|
||||
Text(L10n.Attachment.Send.location)
|
||||
.foregroundColor(.Material.Text.white)
|
||||
.font(.body1)
|
||||
Image(systemName: "arrow.up.circle")
|
||||
.foregroundColor(.Material.Text.white)
|
||||
.font(.body1)
|
||||
.padding(.leading, 8)
|
||||
struct MapView: UIViewRepresentable {
|
||||
@Binding var region: MKCoordinateRegion
|
||||
|
||||
func makeUIView(context: Context) -> MKMapView {
|
||||
let mapView = MKMapView()
|
||||
mapView.delegate = context.coordinator
|
||||
mapView.showsUserLocation = true
|
||||
mapView.userTrackingMode = .none
|
||||
return mapView
|
||||
}
|
||||
.padding()
|
||||
|
||||
func updateUIView(_ uiView: MKMapView, context _: Context) {
|
||||
if uiView.region != region {
|
||||
uiView.setRegion(region, animated: true)
|
||||
}
|
||||
.clipped()
|
||||
.onTapGesture {
|
||||
// TODO: Send location
|
||||
print("Send location")
|
||||
}
|
||||
|
||||
func makeCoordinator() -> Coordinator {
|
||||
Coordinator(self)
|
||||
}
|
||||
|
||||
class Coordinator: NSObject, MKMapViewDelegate {
|
||||
var parent: MapView
|
||||
|
||||
init(_ parent: MapView) {
|
||||
self.parent = parent
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class LocationManager: NSObject, ObservableObject {
|
||||
private let locationManager = CLLocationManager()
|
||||
@Published var region: MKCoordinateRegion
|
||||
|
||||
override init() {
|
||||
region = MKCoordinateRegion()
|
||||
super.init()
|
||||
locationManager.delegate = self
|
||||
locationManager.desiredAccuracy = kCLLocationAccuracyBest
|
||||
}
|
||||
|
||||
func start() {
|
||||
locationManager.requestWhenInUseAuthorization()
|
||||
locationManager.startUpdatingLocation()
|
||||
}
|
||||
|
||||
func stop() {
|
||||
locationManager.stopUpdatingLocation()
|
||||
}
|
||||
}
|
||||
|
||||
extension LocationManager: CLLocationManagerDelegate {
|
||||
func locationManager(_: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
|
||||
if let loc = locations.first {
|
||||
region = MKCoordinateRegion(
|
||||
center: loc.coordinate,
|
||||
span: MKCoordinateSpan(latitudeDelta: 0.002, longitudeDelta: 0.002)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue