diff options
Diffstat (limited to 'iosApp')
-rw-r--r-- | iosApp/iosApp/Map/MapViewController.swift | 6 | ||||
-rw-r--r-- | 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) } } |