From 1883d47ac95e0149ccdc6959839e67cca90c673b Mon Sep 17 00:00:00 2001 From: Milan Stute Date: Mon, 15 Mar 2021 21:33:00 +0100 Subject: [PATCH] Add time slider --- .../HaystackApp/Views/AccessoryMapView.swift | 3 +- .../Views/OpenHaystackMainView.swift | 49 +++++++++++++++++-- .../OpenHaystack/MapViewController.swift | 12 ++++- 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/OpenHaystack/OpenHaystack/HaystackApp/Views/AccessoryMapView.swift b/OpenHaystack/OpenHaystack/HaystackApp/Views/AccessoryMapView.swift index 237b25b..6233aab 100644 --- a/OpenHaystack/OpenHaystack/HaystackApp/Views/AccessoryMapView.swift +++ b/OpenHaystack/OpenHaystack/HaystackApp/Views/AccessoryMapView.swift @@ -16,6 +16,7 @@ struct AccessoryMapView: NSViewControllerRepresentable { @Binding var mapType: MKMapType @Binding var focusedAccessory: Accessory? @Binding var showHistory: Bool + @Binding var showPastHistory: TimeInterval func makeNSViewController(context: Context) -> MapViewController { return MapViewController(nibName: NSNib.Name("MapViewController"), bundle: nil) @@ -26,7 +27,7 @@ struct AccessoryMapView: NSViewControllerRepresentable { nsViewController.focusedAccessory = focusedAccessory if showHistory { - nsViewController.addAllLocations(from: focusedAccessory!) + nsViewController.addAllLocations(from: focusedAccessory!, past: showPastHistory) nsViewController.zoomInOnAll() } else { nsViewController.addLastLocations(from: accessories) diff --git a/OpenHaystack/OpenHaystack/HaystackApp/Views/OpenHaystackMainView.swift b/OpenHaystack/OpenHaystack/HaystackApp/Views/OpenHaystackMainView.swift index d7ce017..cae1a84 100644 --- a/OpenHaystack/OpenHaystack/HaystackApp/Views/OpenHaystackMainView.swift +++ b/OpenHaystack/OpenHaystack/HaystackApp/Views/OpenHaystackMainView.swift @@ -29,6 +29,7 @@ struct OpenHaystackMainView: View { @State var isLoading = false @State var focusedAccessory: Accessory? @State var historyMapView = false + @State var historySeconds: TimeInterval = 2 * TimeInterval.Units.day.rawValue @State var accessoryToDeploy: Accessory? @State var showMailPlugInPopover = false @@ -49,8 +50,11 @@ struct OpenHaystackMainView: View { .frame(minWidth: 250, idealWidth: 280, maxWidth: .infinity, minHeight: 300, idealHeight: 400, maxHeight: .infinity, alignment: .center) ZStack { - AccessoryMapView(accessoryController: self.accessoryController, mapType: self.$mapType, focusedAccessory: self.$focusedAccessory, showHistory: self.$historyMapView) - .overlay(self.mapOverlay) + AccessoryMapView( + accessoryController: self.accessoryController, mapType: self.$mapType, focusedAccessory: self.$focusedAccessory, showHistory: self.$historyMapView, + showPastHistory: self.$historySeconds + ) + .overlay(self.mapOverlay) if self.popUpAlertType != nil { VStack { Spacer() @@ -106,9 +110,16 @@ struct OpenHaystackMainView: View { } } - /// All toolbar items shown + /// All toolbar items shown. var toolbarView: some View { Group { + if self.historyMapView { + Text("\(TimeInterval(self.historySeconds).description)") + Slider(value: $historySeconds, in: 30 * TimeInterval.Units.minute.rawValue...TimeInterval.Units.week.rawValue) { + Text("Past time to show") + } + .frame(width: 80) + } Toggle(isOn: $historyMapView) { Label("Show location history", systemImage: "clock") } @@ -425,3 +436,35 @@ extension Alert.Button { Alert.Button.default(Text("Okay")) } } + +extension TimeInterval { + var description: String { + var value = 0 + var unit = Units.second + Units.allCases.forEach { u in + if self >= u.rawValue { + value = Int(self / u.rawValue) + unit = u + } + } + return "\(value) \(unit.description)\(value > 1 ? "s" : "")" + } + + enum Units: Double, CaseIterable { + case second = 1 + case minute = 60 + case hour = 3600 + case day = 86400 + case week = 604800 + + var description: String { + switch self { + case .second: return "Second" + case .minute: return "Minute" + case .hour: return "Hour" + case .day: return "Day" + case .week: return "Week" + } + } + } +} diff --git a/OpenHaystack/OpenHaystack/MapViewController.swift b/OpenHaystack/OpenHaystack/MapViewController.swift index 515f196..af0afcf 100755 --- a/OpenHaystack/OpenHaystack/MapViewController.swift +++ b/OpenHaystack/OpenHaystack/MapViewController.swift @@ -61,9 +61,17 @@ final class MapViewController: NSViewController, MKMapViewDelegate { self.mapView.mapType = mapType } - func addAllLocations(from accessory: Accessory) { + func addAllLocations(from accessory: Accessory, past: TimeInterval) { + let now = Date() + let pastLocations = accessory.locations?.filter { location in + guard let timestamp = location.timestamp else { + return false + } + return timestamp + past >= now + } + self.mapView.removeAnnotations(self.mapView.annotations) - for location in accessory.locations ?? [] { + for location in pastLocations ?? [] { let coordinate = CLLocationCoordinate2DMake(location.latitude, location.longitude) let annotation = AccessoryHistoryAnnotation(coordinate: coordinate) self.mapView.addAnnotation(annotation)