aboutsummaryrefslogtreecommitdiff
path: root/iosApp/iosApp/Details/Reports/UnitReportsViewModel.swift
blob: 5e85e89c02d7b2a1eca8561d40cb4f4eff71b4d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
//  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
    
    @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
    
    init(deviceId id: Int32?) {
        deviceId = id
        let collector = Collector<ReportController.Report>(callback: setReport)
        reportController.reportFlow.collect(collector: collector) { _, _ in }
    }
    
    func setReport (report: ReportController.Report) {
        self.report = report
    }
    
    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 }
        }
    }
}