aboutsummaryrefslogtreecommitdiff
path: root/iosApp
diff options
context:
space:
mode:
authorIván Ávalos <avalos@disroot.org>2022-04-07 22:21:03 -0500
committerIván Ávalos <avalos@disroot.org>2022-04-07 22:21:03 -0500
commit7b80fd8cad3b60a37317c32bb0c971070b56c06f (patch)
tree94fd237c9f85e06701e52ad06bc6d2d9667c2a8f /iosApp
parent355d846e1f024a6877de94e38cd14d12fc80478b (diff)
parent83340f9df0210fa725bf3875d4f7298ab2e7a76b (diff)
downloadetbsa-trackermap-mobile-7b80fd8cad3b60a37317c32bb0c971070b56c06f.tar.gz
etbsa-trackermap-mobile-7b80fd8cad3b60a37317c32bb0c971070b56c06f.tar.bz2
etbsa-trackermap-mobile-7b80fd8cad3b60a37317c32bb0c971070b56c06f.zip
Merge branch 'main' of https://git.sr.ht/~avalos/trackermap-mobile
Diffstat (limited to 'iosApp')
-rw-r--r--iosApp/iosApp/Map/MapViewController.swift6
-rw-r--r--iosApp/iosApp/Units/UnitsViewModel.swift62
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)
}
}