From a68448a25c7415acd90c457f27b6cccdedf44a01 Mon Sep 17 00:00:00 2001 From: Milan Stute Date: Wed, 10 Mar 2021 22:50:18 +0100 Subject: [PATCH] Show map controls in toolbar --- .../Views/OpenHaystackMainView.swift | 47 +++++-------------- .../OpenHaystack/MapViewController.swift | 45 +++--------------- 2 files changed, 19 insertions(+), 73 deletions(-) diff --git a/OpenHaystack/OpenHaystack/HaystackApp/Views/OpenHaystackMainView.swift b/OpenHaystack/OpenHaystack/HaystackApp/Views/OpenHaystackMainView.swift index 3c36b2e..524b650 100644 --- a/OpenHaystack/OpenHaystack/HaystackApp/Views/OpenHaystackMainView.swift +++ b/OpenHaystack/OpenHaystack/HaystackApp/Views/OpenHaystackMainView.swift @@ -44,7 +44,8 @@ struct OpenHaystackMainView: View { .navigationTitle(self.focusedAccessory?.name ?? "OpenHaystack") ZStack { - self.mapView + AccessoryMapView(accessoryController: self.accessoryController, mapType: self.$mapType, focusedAccessory: self.focusedAccessory) + .overlay(self.mapOverlay) if self.popUpAlertType != nil { VStack { Spacer() @@ -55,6 +56,17 @@ struct OpenHaystackMainView: View { } } .ignoresSafeArea(.all) + .toolbar(content: { + Picker("", selection: self.$mapType) { + Text("Satellite").tag(MKMapType.hybrid) + Text("Standard").tag(MKMapType.standard) + } + .pickerStyle(SegmentedPickerStyle()) + Button(action: self.downloadLocationReports) { + Label("Reload", systemImage: "arrow.clockwise") + } + .disabled(self.accessories.isEmpty) + }) .alert( item: self.$alertType, content: { alertType in @@ -95,39 +107,6 @@ struct OpenHaystackMainView: View { } } - /// Right side of the view showing a map with all items presented. - var mapView: some View { - ZStack { - - AccessoryMapView(accessoryController: self.accessoryController, mapType: self.$mapType, focusedAccessory: self.focusedAccessory) - .overlay(self.mapOverlay) - - VStack { - Spacer() - HStack { - - Picker("", selection: self.$mapType) { - Text("Satellite").tag(MKMapType.hybrid) - Text("Standard").tag(MKMapType.standard) - } - .pickerStyle(SegmentedPickerStyle()) - .frame(width: 150, alignment: .center) - - Button( - action: self.downloadLocationReports, - label: { - Image(systemName: "arrow.clockwise") - Text("Reload") - } - ) - .opacity(1.0) - .disabled(self.accessories.isEmpty) - } - .padding(.bottom, 25) - } - } - } - func onAppear() { /// Checks if the search party token can be fetched without the Mail Plugin. If true the plugin is not needed for this environment. (e.g. when SIP is disabled) diff --git a/OpenHaystack/OpenHaystack/MapViewController.swift b/OpenHaystack/OpenHaystack/MapViewController.swift index dfb764d..193e5a2 100755 --- a/OpenHaystack/OpenHaystack/MapViewController.swift +++ b/OpenHaystack/OpenHaystack/MapViewController.swift @@ -19,34 +19,6 @@ final class MapViewController: NSViewController, MKMapViewDelegate { self.mapView.register(AccessoryAnnotationView.self, forAnnotationViewWithReuseIdentifier: "Accessory") } - func addLocationsReports(from devices: [FindMyDevice]) { - if !self.mapView.annotations.isEmpty { - self.mapView.removeAnnotations(self.mapView.annotations) - } - - // Zoom to first location - if let location = devices.first?.decryptedReports?.first { - let coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude) - let span = MKCoordinateSpan(latitudeDelta: 5.0, longitudeDelta: 5.0) - let region = MKCoordinateRegion(center: coordinate, span: span) - - self.mapView.setRegion(region, animated: true) - } - - // Add pins - for device in devices { - - guard let reports = device.decryptedReports else { continue } - for report in reports { - let pin = MKPointAnnotation() - pin.title = device.deviceId - pin.coordinate = CLLocationCoordinate2D(latitude: report.latitude, longitude: report.longitude) - self.mapView.addAnnotation(pin) - } - } - - } - func zoom(on accessory: Accessory?) { self.focusedAccessory = accessory guard let location = accessory?.lastLocation else { return } @@ -58,8 +30,12 @@ final class MapViewController: NSViewController, MKMapViewDelegate { } func addLastLocations(from accessories: [Accessory]) { - if !self.mapView.annotations.isEmpty { - self.mapView.removeAnnotations(self.mapView.annotations) + // Add pins + self.mapView.removeAnnotations(self.mapView.annotations) + for accessory in accessories { + guard accessory.lastLocation != nil else { continue } + let annotation = AccessoryAnnotation(accessory: accessory) + self.mapView.addAnnotation(annotation) } // Zoom to first location @@ -70,15 +46,6 @@ final class MapViewController: NSViewController, MKMapViewDelegate { self.mapView.setRegion(region, animated: true) } } - - // Add pins - for accessory in accessories { - guard accessory.lastLocation != nil else { continue } - - let annotation = AccessoryAnnotation(accessory: accessory) - self.mapView.addAnnotation(annotation) - - } } func changeMapType(_ mapType: MKMapType) {