aboutsummaryrefslogtreecommitdiff
path: root/iosApp/iosApp/Details/Reports/UnitReportsViewModel.swift
diff options
context:
space:
mode:
Diffstat (limited to 'iosApp/iosApp/Details/Reports/UnitReportsViewModel.swift')
-rw-r--r--iosApp/iosApp/Details/Reports/UnitReportsViewModel.swift94
1 files changed, 94 insertions, 0 deletions
diff --git a/iosApp/iosApp/Details/Reports/UnitReportsViewModel.swift b/iosApp/iosApp/Details/Reports/UnitReportsViewModel.swift
new file mode 100644
index 0000000..4731a57
--- /dev/null
+++ b/iosApp/iosApp/Details/Reports/UnitReportsViewModel.swift
@@ -0,0 +1,94 @@
+//
+// UnitReportsViewModel.swift
+// iosApp
+//
+// Created by Iván on 13/02/22.
+// Copyright © 2022 orgName. All rights reserved.
+//
+
+import Foundation
+import Combine
+import shared
+
+class UnitReportsViewModel: ObservableObject {
+ @Inject var reportController: ReportController
+ @Inject var geofencesController: GeofencesController
+
+ @Published var deviceId: Int32? = nil
+ @Published var reportType: ReportController.ReportType = .positions {
+ didSet {
+ fetchReport()
+ }
+ }
+ @Published var reportPeriod: ReportDates.ReportPeriod = .today {
+ didSet {
+ fetchReport()
+ }
+ }
+ @Published var report: ReportController.Report? = nil {
+ didSet {
+ switch report {
+ case let report as ReportController.ReportPositionsReport:
+ markers = report.positions.compactMap { p in Marker.companion.fromPosition(position: p)}
+ case is ReportController.ReportEventsReport:
+ markers = []
+ case let report as ReportController.ReportStopsReport:
+ markers = report.stops.compactMap { s in Marker.companion.fromStop(stop: s) }
+ default:
+ break
+ }
+ }
+ }
+ @Published var layer = MapLayer.companion.defaultLayer
+ @Published var markers = [Marker]()
+ @Published var selectedMarker: Marker? = nil
+
+ @Published var geofences: [Int: Geofence] = [:] {
+ didSet {
+ flatGeofences = Array(geofences.values)
+ }
+ }
+ @Published var flatGeofences: [Geofence] = []
+
+ init(deviceId id: Int32?) {
+ deviceId = id
+ let collector = Collector<ReportController.Report>(callback: setReport)
+ reportController.reportFlow.collect(collector: collector) { _, _ in }
+
+ let geofencesCollector = Collector<[Int: Geofence]>(callback: setGeofences)
+ geofencesController.geofencesFlow.collect(collector: geofencesCollector) { unit, error in }
+ }
+
+ func setReport (report: ReportController.Report) {
+ self.report = report
+ }
+
+ private func setGeofences(geofences: [Int: Geofence]) {
+ self.geofences = geofences
+ }
+
+ func fetchReport(xlsx: Bool = false) {
+ if let id = deviceId {
+ reportController.fetchReport(deviceId: id,
+ reportType: reportType,
+ reportPeriod: reportPeriod,
+ xlsx: xlsx, eventTypes: [
+ .deviceInactive,
+ .deviceMoving,
+ .deviceStopped,
+ .deviceOverspeed,
+ .deviceFuelDrop,
+ .commandResult,
+ .geofenceEnter,
+ .geofenceExit,
+ .alarm,
+ .ignitionOn,
+ .ignitionOff,
+ .maintenance,
+ .textMessage,
+ .driverChanged,
+ .unknown
+ ]) { _, _ in }
+ }
+ }
+}