From 83340f9df0210fa725bf3875d4f7298ab2e7a76b Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Thu, 7 Apr 2022 21:52:25 -0500 Subject: [iOS] Card on map now updates info when there is a change. --- iosApp/iosApp/Map/MapViewController.swift | 6 +-- iosApp/iosApp/Units/UnitsViewModel.swift | 62 ++++++++++++++++++++++++------- 2 files changed, 52 insertions(+), 16 deletions(-) diff --git a/iosApp/iosApp/Map/MapViewController.swift b/iosApp/iosApp/Map/MapViewController.swift index 3c38b89..07b72a6 100644 --- a/iosApp/iosApp/Map/MapViewController.swift +++ b/iosApp/iosApp/Map/MapViewController.swift @@ -20,7 +20,7 @@ import WhirlyGlobe import shared import GEOSwift -typealias MarkerCallback = (Int32?) -> () +typealias MarkerCallback = (Int32?, Bool) -> () extension Geometry { func getCoordinates() -> [Point] { @@ -138,7 +138,7 @@ extension MapViewController: MaplyViewControllerDelegate { func maplyViewController(_ viewC: MaplyViewController, didTapAt coord: MaplyCoordinate) { - markerCallback?(nil) + markerCallback?(nil, true) } func maplyViewController(_ viewC: MaplyViewController, @@ -147,7 +147,7 @@ extension MapViewController: MaplyViewControllerDelegate { onScreen screenPt: CGPoint) { if let marker = selectedObj as? MaplyScreenMarker { if let id = marker.userObject as? Int32 { - markerCallback?(id) + markerCallback?(id, true) } } } diff --git a/iosApp/iosApp/Units/UnitsViewModel.swift b/iosApp/iosApp/Units/UnitsViewModel.swift index 8ba6a4b..6a3ce19 100644 --- a/iosApp/iosApp/Units/UnitsViewModel.swift +++ b/iosApp/iosApp/Units/UnitsViewModel.swift @@ -49,12 +49,15 @@ class UnitsViewModel: ObservableObject { } } @Published var markers: [Marker] = [] + @Published var oldSelectedUnit: UnitInformation? = nil @Published var selectedUnit: UnitInformation? = nil { didSet { - if let unit = selectedUnit { - selectedMarker = Marker.companion.fromUnit(unit: unit) - } else { - selectedMarker = nil + if selectedUnit?.device.id != oldSelectedUnit?.device.id { + if let unit = selectedUnit { + selectedMarker = Marker.companion.fromUnit(unit: unit) + } else { + selectedMarker = nil + } } } } @@ -87,32 +90,65 @@ class UnitsViewModel: ObservableObject { private func setUnits(units: [UnitInformation]) { print("Positions") self.units = units + updateSelectedUnit() } private func setGeofences(geofences: [Int: Geofence]) { self.geofences = geofences } - func selectUnitWith(position id: Int32?) { + func selectUnit(unit: UnitInformation?, switchToMap: Bool = true) { + print ("Selecting unit \(unit?.device.name ?? "")") + oldSelectedUnit = selectedUnit + selectedUnit = unit + if unit != nil && switchToMap { + setDisplayMode(UnitsDisplayMode.map) + } + } + + func selectUnitWith(position id: Int32?, switchToMap: Bool = true) { if id == nil { print("Deselecting unit") - selectedUnit = nil + selectUnit(unit: nil, switchToMap: switchToMap) return } - print("Selecting unit with position id: \(id ?? 0)") - if let unit = units.first(where: { - Int32(truncating: $0.position?.id ?? 0) == id - }) { - selectedUnit = unit + print("Selecting unit with position id: \(id!)") + let unit = units.first { + Int32(truncating: $0.position?.id ?? 0) == id! } + selectUnit(unit: unit, switchToMap: switchToMap) + } + + private func selectUnitWith(id: Int32?, switchToMap: Bool = true) { + if id == nil { + print("Deselecting unit") + selectUnit(unit: nil, switchToMap: switchToMap) + return + } + print("Selecting unit with device id: \(id!)") + let unit = units.first { + $0.device.id == id! + } + selectUnit(unit: unit, switchToMap: switchToMap) + } + + private func updateSelectedUnit() { + if let selected = selectedUnit { + print("Updating selected unit with id: \(selected.device.id)") + selectUnitWith(id: selected.device.id, switchToMap: false) + } + } + + func setDisplayMode(_ displayMode: UnitsDisplayMode) { + unitsDisplayMode = displayMode } func toggleDisplayMode() { switch unitsDisplayMode { case .map: - unitsDisplayMode = .list + setDisplayMode(.list) case .list: - unitsDisplayMode = .map + setDisplayMode(.map) } } -- cgit v1.2.3