From 7ab652acd172963b2f36085eb9be6927cb21fc71 Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Mon, 17 Jan 2022 15:51:47 -0600 Subject: Fixed reports fragment map data display, and added geofences --- .../android/details/reports/UnitReportsFragment.kt | 57 ++++++++++++++++------ .../details/reports/UnitReportsViewModel.kt | 14 ++++++ .../TrackerMap/android/map/MapFragment.kt | 5 ++ 3 files changed, 62 insertions(+), 14 deletions(-) (limited to 'androidApp/src/main') 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 92a94b1..1f1bb15 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 @@ -24,6 +24,7 @@ import mx.trackermap.TrackerMap.android.details.UnitDetailsAdapter import mx.trackermap.TrackerMap.android.map.MapFragment import mx.trackermap.TrackerMap.android.shared.MarkerTransformations import mx.trackermap.TrackerMap.client.models.EventInformation +import mx.trackermap.TrackerMap.client.models.Geofence import mx.trackermap.TrackerMap.client.models.Position import mx.trackermap.TrackerMap.client.models.Stop import mx.trackermap.TrackerMap.controllers.ReportController @@ -162,6 +163,10 @@ class UnitReportsFragment : Fragment() { } ) } + + unitReportsViewModel.geofences.observe(viewLifecycleOwner) { geofences -> + display(geofences) + } } private fun removeObservers() { @@ -193,15 +198,22 @@ class UnitReportsFragment : Fragment() { } private fun display(positions: Array) { - Log.d("UnitReportsFragment", "Displaying positions: $positions") + if (reportsMapFragment.hasStarted) { + Log.d("UnitReportsFragment", "Displaying positions: $positions") - binding.eventsScroll.visibility = View.GONE - binding.reportsMapContainer.visibility = View.VISIBLE + binding.eventsScroll.visibility = View.GONE + binding.reportsMapContainer.visibility = View.VISIBLE - reportsMapFragment.display( - positions.mapNotNull(MarkerTransformations::positionToMarker).toTypedArray(), - isReport = true - ) + reportsMapFragment.display( + positions.mapNotNull(MarkerTransformations::positionToMarker).toTypedArray(), + isReport = true + ) + } else { + reportsMapFragment.setupCallbacks.clear() + reportsMapFragment.setupCallbacks.add { + display(positions) + } + } } private fun display(events: Array) { @@ -271,15 +283,32 @@ class UnitReportsFragment : Fragment() { } private fun display(stops: Array) { - Log.d("UnitReportsFragment", "Displaying stops: $stops") + if (reportsMapFragment.hasStarted) { + Log.d("UnitReportsFragment", "Displaying stops: $stops") - binding.eventsScroll.visibility = View.GONE - binding.reportsMapContainer.visibility = View.VISIBLE + binding.eventsScroll.visibility = View.GONE + binding.reportsMapContainer.visibility = View.VISIBLE - reportsMapFragment.display( - stops.mapNotNull(MarkerTransformations::stopToMarker).toTypedArray(), - isReport = true - ) + reportsMapFragment.display( + stops.mapNotNull(MarkerTransformations::stopToMarker).toTypedArray(), + isReport = true + ) + } else { + reportsMapFragment.setupCallbacks.add { + display(stops) + } + } + } + + private fun display(geofences: Array) { + if (reportsMapFragment.hasStarted) { + Log.d("UnitReportsFragment", "Displaying geofences: $geofences") + reportsMapFragment.displayGeofences(geofences) + } else { + reportsMapFragment.setupCallbacks.add { + display(geofences) + } + } } private fun loading() { 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 0c8a6ac..a957435 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 @@ -5,6 +5,8 @@ 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 @@ -15,6 +17,7 @@ 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) @@ -22,11 +25,13 @@ class UnitReportsViewModel( private val _reportPeriod: MutableLiveData = savedStateHandle.getLiveData("reportPeriod", null) private val _report: MutableLiveData = MutableLiveData() + private val _geofences: MutableLiveData> = MutableLiveData() 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 { @@ -38,6 +43,9 @@ class UnitReportsViewModel( viewModelScope.launch { setupReportObserver() } + viewModelScope.launch { + setupGeofenceObserver() + } } private suspend fun setupTypeObserver() { @@ -58,6 +66,12 @@ class UnitReportsViewModel( } } + private suspend fun setupGeofenceObserver() { + geofencesController.geofencesFlow.collect { + _geofences.value = it.values.toTypedArray() + } + } + fun setDeviceId (id: Int) { _deviceId.value = id } diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapFragment.kt index cd1778a..1ba8fd6 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapFragment.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapFragment.kt @@ -24,6 +24,7 @@ import mx.trackermap.TrackerMap.client.models.MapLayer import mx.trackermap.TrackerMap.utils.MapCalculus import mx.trackermap.TrackerMap.utils.MarkerType +typealias SetupCallback = () -> Unit typealias MarkerCallback = (Int?) -> Unit class MapFragment : GlobeMapFragment() { @@ -38,6 +39,8 @@ class MapFragment : GlobeMapFragment() { val type: MarkerType = MarkerType.DEFAULT ) + var hasStarted: Boolean = false + val setupCallbacks = mutableListOf() var markerCallback: MarkerCallback? = null private val objects = mutableListOf() @@ -65,6 +68,8 @@ class MapFragment : GlobeMapFragment() { override fun controlHasStarted() { Log.d("MapFragment", "controlHasStarted") + hasStarted = true + setupCallbacks.forEach { it() } if (tileInfo == null) { val layer = MapLayer.defaultLayer -- cgit v1.2.3