/** * TrackerMap * Copyright (C) 2021-2022 Iván Ávalos , Henoch Ojeda * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ import Foundation import Combine import shared class UnitReportsViewModel: ObservableObject { @Inject var reportController: ReportController @Inject var geofencesController: GeofencesController enum XlsxAction { case save case share } @Published var deviceId: Int32? = nil // MARK: - Report settings @Published var reportType: ReportController.ReportType = .positions { didSet { fetchReport() } } @Published var periodType: ReportDates.PeriodTypes = .today { didSet { switch periodType { case .today: reportPeriod = ReportDates.ReportPeriodToday() case .last24: reportPeriod = ReportDates.ReportPeriodLast24() case .yesterday: reportPeriod = ReportDates.ReportPeriodYesterday() case .thisWeek: reportPeriod = ReportDates.ReportPeriodThisWeek() case .last7: reportPeriod = ReportDates.ReportPeriodLast7() case .thisMonth: reportPeriod = ReportDates.ReportPeriodThisMonth() case .last30: reportPeriod = ReportDates.ReportPeriodLast30() case .custom: reportPeriod = ReportDates.ReportPeriodCustom(from: nil, to: nil) default: reportPeriod = ReportDates.ReportPeriodToday() } } } @Published var reportPeriod: ReportDates.ReportPeriod = ReportDates.ReportPeriodToday() { 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) } case let report as ReportController.ReportXlsxReport: print ("Got XLSX report!") saveDocument = XlsxFile(bytes: report.data) if xlsxAction == .save { showExportDialog = true } else { let url = saveDocument.data.dataToFile(fileName: "report.xlsx") activityItems = [url!] showShareDialog = true } default: break } } } @Published var layer = MapLayer.companion.defaultLayer @Published var markers = [Marker]() @Published var selectedMarker: Marker? = nil @Published var fromDate: Date = Date() { didSet { if let custom = reportPeriod as? ReportDates.ReportPeriodCustom { reportPeriod = custom.withFrom( from: DateUtils.companion.iosDateToKotlin(date: fromDate)) } } } @Published var toDate: Date = Calendar.current.startOfDay(for: Date()) { didSet { if let custom = reportPeriod as? ReportDates.ReportPeriodCustom { reportPeriod = custom.withTo( to: DateUtils.companion.iosDateToKotlin(date: toDate)) } } } // MARK: - Geofences @Published var geofences: [Int: Geofence] = [:] { didSet { flatGeofences = Array(geofences.values) } } @Published var flatGeofences: [Geofence] = [] // MARK: - Save and share var xlsxAction: XlsxAction = .save var saveDocument = XlsxFile() @Published var showExportDialog: Bool = false @Published var showShareDialog: Bool = false @Published var activityItems: [Any] = [] init(deviceId id: Int32?) { deviceId = id let collector = Collector(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 } } } func saveXlsxReport () { xlsxAction = .save fetchReport(xlsx: true) } func shareXlsxReport () { xlsxAction = .share fetchReport(xlsx: true) } }