diff options
author | Iván Ávalos <avalos@disroot.org> | 2022-01-29 04:13:25 -0600 |
---|---|---|
committer | Iván Ávalos <avalos@disroot.org> | 2022-01-29 04:13:25 -0600 |
commit | e1d8854d7d5e789ed84e8948f17fb0dac0d44da6 (patch) | |
tree | ab84d7edaead85e03950102e091da51952635159 /iosApp/iosApp/Units/UnitsViewModel.swift | |
parent | 64c4a58e644229b9d94233b3dd18b7805ffbb8d7 (diff) | |
download | etbsa-trackermap-mobile-e1d8854d7d5e789ed84e8948f17fb0dac0d44da6.tar.gz etbsa-trackermap-mobile-e1d8854d7d5e789ed84e8948f17fb0dac0d44da6.tar.bz2 etbsa-trackermap-mobile-e1d8854d7d5e789ed84e8948f17fb0dac0d44da6.zip |
Initial implementation of device list, and added getStatus() and getEngineStop() methods to UnitInformation
Diffstat (limited to 'iosApp/iosApp/Units/UnitsViewModel.swift')
-rw-r--r-- | iosApp/iosApp/Units/UnitsViewModel.swift | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/iosApp/iosApp/Units/UnitsViewModel.swift b/iosApp/iosApp/Units/UnitsViewModel.swift new file mode 100644 index 0000000..648e429 --- /dev/null +++ b/iosApp/iosApp/Units/UnitsViewModel.swift @@ -0,0 +1,65 @@ +// +// UnitsViewModel.swift +// iosApp +// +// Created by Iván on 28/01/22. +// Copyright © 2022 orgName. All rights reserved. +// + +import Foundation +import shared + +class UnitsViewModel: ObservableObject { + @Inject var unitsController: UnitsController + @Inject var geofenceController: GeofencesController + + enum UnitsDisplayMode { + case map + case list + } + + @Published var searchQuery = "" { + didSet { + unitsDisplayMode = .list + search(query: searchQuery) + } + } + @Published var isEditing = false + @Published var unitsDisplayMode: UnitsDisplayMode = .list + @Published var units: [UnitInformation] = [] + @Published var selectedUnit: UnitInformation? = nil + @Published var geofences: [Int: Geofence] = [:] + + init() { + setupObservers() + } + + private func setupObservers() { + let unitsCollector = Collector<[UnitInformation]>(callback: setUnits) + unitsController.displayedUnitsFlow.collect(collector: unitsCollector) { unit, error in } + + let geofencesCollector = Collector<[Int: Geofence]>(callback: setGeofences) + geofenceController.geofencesFlow.collect(collector: geofencesCollector) { unit, error in } + } + + private func setUnits(units: [UnitInformation]) { + self.units = units + } + + private func setGeofences(geofences: [Int: Geofence]) { + self.geofences = geofences + } + + func toggleDisplayMode() { + switch unitsDisplayMode { + case .map: + unitsDisplayMode = .list + case .list: + unitsDisplayMode = .map + } + } + + func search(query: String) { + unitsController.search(query: query) + } +} |