/** * 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 . */ 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 { enum class ExportAction { ACTION_SAVE, ACTION_SHARE } private val geofencesController: GeofencesController by inject() private val reportController: ReportController by inject() private var _deviceId = savedStateHandle.getLiveData("deviceId", 0) private val _reportType: MutableLiveData = savedStateHandle.getLiveData("reportType", null) private val _reportPeriod: MutableLiveData = savedStateHandle.getLiveData("reportPeriod", null) private val _report: MutableLiveData = MutableLiveData() private val _geofences: MutableLiveData> = MutableLiveData(emptyArray()) val deviceId: LiveData get() = _deviceId val reportType: LiveData get() = _reportType val reportPeriod: LiveData get() = _reportPeriod val report: LiveData get() = _report val geofences: LiveData> 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 ) ) } } } }