From 24bbcbb4a244f2e41c7ca3773ad1378ee422f55c Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Thu, 13 Jan 2022 20:22:36 -0600 Subject: Implemented base for exporting XLSX reports --- .../android/details/reports/UnitReportsFragment.kt | 3 + .../details/reports/UnitReportsViewModel.kt | 115 +++++++++++++-------- 2 files changed, 76 insertions(+), 42 deletions(-) (limited to 'androidApp/src/main/java/mx/trackermap') diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt index 43f879d..2cb2d9a 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt @@ -78,6 +78,9 @@ class UnitReportsFragment : Fragment() { binding.periodButton.setOnClickListener { showPeriodPopUp(it) } + binding.exportButton.setOnClickListener { + unitReportsViewModel.fetchReportXlsx() + } unitReportsViewModel.setReportPeriod(UnitReportsViewModel.ReportPeriod.DAY) unitReportsViewModel.setReportType(UnitReportsViewModel.ReportType.POSITIONS) } diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsViewModel.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsViewModel.kt index 48486e1..851516e 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsViewModel.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsViewModel.kt @@ -112,7 +112,11 @@ class UnitReportsViewModel( return Pair(currentDate, previousDate) } - private fun fetchReport() { + fun fetchReportXlsx() { + fetchReport(true) + } + + private fun fetchReport(xlsx: Boolean = false) { if (_reportType.value == null || _reportPeriod.value == null) { return } @@ -126,64 +130,91 @@ class UnitReportsViewModel( _report.postValue(Report.LoadingReport) viewModelScope.launch { when (_reportType.value!!) { - ReportType.POSITIONS -> fetchPositions(previousDate, currentDate) - ReportType.EVENTS -> fetchEvents(previousDate, currentDate) - ReportType.STOPS -> fetchStops(previousDate, currentDate) + ReportType.POSITIONS -> fetchPositions(previousDate, currentDate, xlsx) + ReportType.EVENTS -> fetchEvents(previousDate, currentDate, xlsx) + ReportType.STOPS -> fetchStops(previousDate, currentDate, xlsx) } } } - private suspend fun fetchPositions(from: Date, to: Date) { + private suspend fun fetchPositions(from: Date, to: Date, xlsx: Boolean) { Log.d("UnitReportsVM", "Fetching positions") - val result = reportsApi.reportsRouteGet( - dateFormat.format(from), - dateFormat.format(to), - _deviceId.value!! - ) + if (!xlsx) { + val result = reportsApi.reportsRouteGet( + dateFormat.format(from), + dateFormat.format(to), + _deviceId.value!! + ) + Log.d("UnitReportsVM", "Positions report: $result") + _report.postValue(Report.PositionsReport(result)) + } else { + val result = reportsApi.reportsRouteGetXlsx( + dateFormat.format(from), + dateFormat.format(to), + _deviceId.value!! + ) + Log.d("UnitReportsVM", "Positions report: $result") + } - Log.d("UnitReportsVM", "Positions report: $result") - _report.postValue(Report.PositionsReport(result)) } - private suspend fun fetchEvents(from: Date, to: Date) { + private suspend fun fetchEvents(from: Date, to: Date, xlsx: Boolean) { Log.d("UnitReportsVM", "Fetching events") - val positionsResult = reportsApi.reportsRouteGet( - dateFormat.format(from), - dateFormat.format(to), - _deviceId.value!! - ) - - val eventsResult = reportsApi.reportsEventsGet( - dateFormat.format(from), - dateFormat.format(to), - _deviceId.value!! - ) - - val result = mutableListOf() + if (!xlsx) { + val positionsResult = reportsApi.reportsRouteGet( + dateFormat.format(from), + dateFormat.format(to), + _deviceId.value!! + ) + + val eventsResult = reportsApi.reportsEventsGet( + dateFormat.format(from), + dateFormat.format(to), + _deviceId.value!! + ) + + val result = mutableListOf() + + eventsResult.forEach { event -> + result.add(EventInformation( + event = event, + position = positionsResult.find { it.id == event.positionId } + )) + } - eventsResult.forEach { event -> - result.add(EventInformation( - event = event, - position = positionsResult.find { it.id == event.positionId } - )) + Log.d("UnitReportsVM", "Events report: $result") + _report.postValue(Report.EventsReport(result.toTypedArray())) + } else { + val result = reportsApi.reportsEventsGetXlsx( + dateFormat.format(from), + dateFormat.format(to), + _deviceId.value!! + ) + Log.d("UnitReportsVM", "Events report: $result") } - - Log.d("UnitReportsVM", "Events report: $result") - _report.postValue(Report.EventsReport(result.toTypedArray())) } - private suspend fun fetchStops(from: Date, to: Date) { + private suspend fun fetchStops(from: Date, to: Date, xlsx: Boolean) { Log.d("UnitReportsVM", "Fetching stops") - val result = reportsApi.reportsStopsGet( - dateFormat.format(from), - dateFormat.format(to), - _deviceId.value!! - ) + if (!xlsx) { + val result = reportsApi.reportsStopsGet( + dateFormat.format(from), + dateFormat.format(to), + _deviceId.value!! + ) + Log.d("UnitReportsVM", "Stops report: $result") + _report.postValue(Report.StopsReport(result)) + } else { + val result = reportsApi.reportsStopsGetXlsx( + dateFormat.format(from), + dateFormat.format(to), + _deviceId.value!! + ) + Log.d("UnitReportsVM", "Stops report: $result") + } - Log.d("UnitReportsVM", "Stops report: $result") - _report.postValue(Report.StopsReport(result)) } } \ No newline at end of file -- cgit v1.2.3