aboutsummaryrefslogtreecommitdiff
path: root/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsViewModel.kt
blob: a95743515f2cede5fbbffc7c6cd03c7d61a9f0ef (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package mx.trackermap.TrackerMap.android.details.reports

import androidx.lifecycle.*
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import mx.trackermap.TrackerMap.client.models.EventInformation
import mx.trackermap.TrackerMap.client.models.Geofence
import mx.trackermap.TrackerMap.controllers.GeofencesController
import mx.trackermap.TrackerMap.controllers.ReportController
import mx.trackermap.TrackerMap.utils.ReportDates
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

@DelicateCoroutinesApi
class UnitReportsViewModel(
    savedStateHandle: SavedStateHandle
) : ViewModel(), KoinComponent {

    private val geofencesController: GeofencesController by inject()
    private val reportController: ReportController by inject()

    private var _deviceId = savedStateHandle.getLiveData("deviceId", 0)
    private val _reportType: MutableLiveData<ReportController.ReportType> = savedStateHandle.getLiveData("reportType", null)
    private val _reportPeriod: MutableLiveData<ReportDates.ReportPeriod> =
        savedStateHandle.getLiveData("reportPeriod", null)
    private val _report: MutableLiveData<ReportController.Report> = MutableLiveData()
    private val _geofences: MutableLiveData<Array<Geofence>> = MutableLiveData()

    val deviceId: LiveData<Int> get() = _deviceId
    val reportType: LiveData<ReportController.ReportType> get() = _reportType
    val reportPeriod: LiveData<ReportDates.ReportPeriod> get() = _reportPeriod
    val report: LiveData<ReportController.Report> get() = _report
    val geofences: LiveData<Array<Geofence>> get() = _geofences

    init {
        viewModelScope.launch {
            setupTypeObserver()
        }
        viewModelScope.launch {
            setupPeriodObserver()
        }
        viewModelScope.launch {
            setupReportObserver()
        }
        viewModelScope.launch {
            setupGeofenceObserver()
        }
    }

    private suspend fun setupTypeObserver() {
        _reportType.asFlow().collect {
            fetchReport()
        }
    }

    private suspend fun setupPeriodObserver() {
        _reportPeriod.asFlow().collect {
            fetchReport()
        }
    }

    private suspend fun setupReportObserver() {
        reportController.reportFlow.collect {
            _report.value = it
        }
    }

    private suspend fun setupGeofenceObserver() {
        geofencesController.geofencesFlow.collect {
            _geofences.value = it.values.toTypedArray()
        }
    }

    fun setDeviceId (id: Int) {
        _deviceId.value = id
    }

    fun setReportType(reportType: ReportController.ReportType) {
        _reportType.value = reportType
    }

    fun setReportPeriod(reportPeriod: ReportDates.ReportPeriod) {
        _reportPeriod.value = reportPeriod
    }

    fun fetchReportXlsx() {
        fetchReport(true)
    }

    fun clearReport() {
        _report.value = null
    }

    fun fetchReport(xlsx: Boolean = false) {
        if (_reportType.value == null || _reportPeriod.value == null) {
            return
        }

        viewModelScope.launch {
            _deviceId.value?.let { id ->
                reportController.fetchReport(
                    deviceId = id,
                    reportType = _reportType.value,
                    reportPeriod = _reportPeriod.value,
                    xlsx = xlsx,
                    eventTypes = listOf(
                        EventInformation.Type.DEVICE_INACTIVE,
                        EventInformation.Type.DEVICE_MOVING,
                        EventInformation.Type.DEVICE_STOPPED,
                        EventInformation.Type.DEVICE_OVERSPEED,
                        EventInformation.Type.DEVICE_FUEL_DROP,
                        EventInformation.Type.COMMAND_RESULT,
                        EventInformation.Type.GEOFENCE_ENTER,
                        EventInformation.Type.GEOFENCE_EXIT,
                        EventInformation.Type.ALARM,
                        EventInformation.Type.IGNITION_ON,
                        EventInformation.Type.IGNITION_OFF,
                        EventInformation.Type.MAINTENANCE,
                        EventInformation.Type.TEXT_MESSAGE,
                        EventInformation.Type.DRIVER_CHANGED,
                        EventInformation.Type.UNKNOWN
                    )
                )
            }
        }
    }
}