From 76747c473656ecc4f63f28212326f90dc1e53643 Mon Sep 17 00:00:00 2001 From: Isidro Henoch Date: Mon, 17 Jan 2022 13:53:21 -0600 Subject: Little update to some fragments constructor --- .../trackermap/TrackerMap/android/devices/DevicesFragment.kt | 11 ++++++++--- .../mx/trackermap/TrackerMap/android/map/UnitMapFragment.kt | 12 ++++++++---- .../mx/trackermap/TrackerMap/android/units/UnitsActivity.kt | 4 ++-- 3 files changed, 18 insertions(+), 9 deletions(-) (limited to 'androidApp/src/main/java/mx/trackermap/TrackerMap') diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesFragment.kt index dce8ceb..b905beb 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesFragment.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesFragment.kt @@ -6,19 +6,24 @@ import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.fragment.app.Fragment +import androidx.fragment.app.viewModels import androidx.recyclerview.widget.LinearLayoutManager +import kotlin.time.ExperimentalTime import kotlinx.coroutines.DelicateCoroutinesApi import mx.trackermap.TrackerMap.android.databinding.DevicesFragmentBinding import mx.trackermap.TrackerMap.android.details.DetailsActivity import mx.trackermap.TrackerMap.android.shared.UnitRenderData -import mx.trackermap.TrackerMap.android.units.UnitFragment import mx.trackermap.TrackerMap.android.units.UnitsViewModel import mx.trackermap.TrackerMap.client.models.UnitInformation -import kotlin.time.ExperimentalTime @DelicateCoroutinesApi @ExperimentalTime -class DevicesFragment(private val unitsViewModel: UnitsViewModel) : UnitFragment(unitsViewModel) { +class DevicesFragment : Fragment() { + + private val unitsViewModel: UnitsViewModel by viewModels( + ownerProducer = { requireActivity() } + ) private var _binding: DevicesFragmentBinding? = null private val binding get() = _binding!! diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/UnitMapFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/UnitMapFragment.kt index 3ab25e4..b6396c8 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/UnitMapFragment.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/UnitMapFragment.kt @@ -6,23 +6,27 @@ import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.Toast import androidx.core.text.HtmlCompat +import androidx.fragment.app.Fragment +import androidx.fragment.app.viewModels +import kotlin.time.ExperimentalTime import kotlinx.coroutines.DelicateCoroutinesApi import mx.trackermap.TrackerMap.android.R import mx.trackermap.TrackerMap.android.databinding.UnitMapFragmentBinding import mx.trackermap.TrackerMap.android.details.DetailsActivity import mx.trackermap.TrackerMap.android.shared.MarkerTransformations import mx.trackermap.TrackerMap.android.shared.UnitRenderData -import mx.trackermap.TrackerMap.android.units.UnitFragment import mx.trackermap.TrackerMap.android.units.UnitsViewModel import mx.trackermap.TrackerMap.client.models.MapLayer import mx.trackermap.TrackerMap.client.models.UnitInformation -import kotlin.time.ExperimentalTime @DelicateCoroutinesApi @ExperimentalTime -class UnitMapFragment(private val unitsViewModel: UnitsViewModel) : UnitFragment(unitsViewModel) { +class UnitMapFragment : Fragment() { + + private val unitsViewModel: UnitsViewModel by viewModels( + ownerProducer = { requireActivity() } + ) private var _binding: UnitMapFragmentBinding? = null private val binding get() = _binding!! diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsActivity.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsActivity.kt index 30e34b3..adc0cb9 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsActivity.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsActivity.kt @@ -32,10 +32,10 @@ class UnitsActivity : AppCompatActivity() { private val unitsViewModel: UnitsViewModel by viewModel() private val mapFragment: UnitMapFragment by lazy { - UnitMapFragment(unitsViewModel) + UnitMapFragment() } private val devicesFragment: DevicesFragment by lazy { - DevicesFragment(unitsViewModel) + DevicesFragment() } override fun onCreate(savedInstanceState: Bundle?) { -- cgit v1.2.3 From d91688765774ccb9153a5236edf359a8fa3a0f62 Mon Sep 17 00:00:00 2001 From: Isidro Henoch Date: Mon, 17 Jan 2022 15:21:45 -0600 Subject: Tries to fix the report map issues --- .../android/details/reports/UnitReportsFragment.kt | 26 +++++++++++++++++----- .../details/reports/UnitReportsViewModel.kt | 6 ++++- .../TrackerMap/android/map/MapFragment.kt | 4 ++++ 3 files changed, 29 insertions(+), 7 deletions(-) (limited to 'androidApp/src/main/java/mx/trackermap/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 600cfe1..92a94b1 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 @@ -42,9 +42,7 @@ class UnitReportsFragment : Fragment() { private val binding get() = _binding!! private val unitReportsViewModel: UnitReportsViewModel by viewModel() - private val reportsMapFragment: MapFragment by lazy { - MapFragment() - } + private lateinit var reportsMapFragment: MapFragment override fun onCreateView( inflater: LayoutInflater, @@ -61,19 +59,24 @@ class UnitReportsFragment : Fragment() { unitReportsViewModel.setDeviceId( arguments?.getInt(UnitDetailsAdapter.DEVICE_ID_ARG) ?: 0) setupEvents() - initializeMap() } override fun onResume() { super.onResume() + initializeMap() setupObservers() } - override fun onStop() { - super.onStop() + override fun onPause() { + super.onPause() removeObservers() + removeMap() + + if (unitReportsViewModel.report.value == null) { + unitReportsViewModel.fetchReport() + } } override fun onDestroyView() { @@ -83,11 +86,19 @@ class UnitReportsFragment : Fragment() { private fun initializeMap() { Log.d("UnitReportsFragment", "initializeMap()") + reportsMapFragment = MapFragment() childFragmentManager.commit { replace(R.id.reportsMapContainer, reportsMapFragment) } } + private fun removeMap() { + Log.d("UnitReportsFragment", "removeMap()") + childFragmentManager.commit { + remove(reportsMapFragment) + } + } + private fun setupEvents() { binding.reportType.setOnPositionChangedListener { position -> unitReportsViewModel.setReportType( @@ -109,6 +120,7 @@ class UnitReportsFragment : Fragment() { } private fun setupObservers() { + Log.d("UnitReportsFragment", "Adding observers") unitReportsViewModel.report.observe(viewLifecycleOwner) { report -> Log.d("UnitReportsFragment", "Report available: $report") @@ -153,8 +165,10 @@ class UnitReportsFragment : Fragment() { } private fun removeObservers() { + Log.d("UnitReportsFragment", "Removing observers") unitReportsViewModel.report.removeObservers(viewLifecycleOwner) unitReportsViewModel.reportPeriod.removeObservers(viewLifecycleOwner) + unitReportsViewModel.clearReport() } private fun showPeriodPopUp(view: View) { 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 7749b54..0c8a6ac 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 @@ -74,7 +74,11 @@ class UnitReportsViewModel( fetchReport(true) } - private fun fetchReport(xlsx: Boolean = false) { + fun clearReport() { + _report.value = null + } + + fun fetchReport(xlsx: Boolean = false) { if (_reportType.value == null || _reportPeriod.value == null) { return } 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 17fc9d4..cd1778a 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 @@ -127,6 +127,10 @@ class MapFragment : GlobeMapFragment() { } private fun clear(geofences: Boolean = false) { + if (mapControl == null) { + return + } + if (geofences) { mapControl.removeObjects( geofenceObjects, -- cgit v1.2.3 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/java/mx/trackermap/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 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 From 07d184c408aba980f331ecaa3e4baef80b6ce82c Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Mon, 17 Jan 2022 15:57:21 -0600 Subject: Fixed geofences retrieval on reports fragment --- .../TrackerMap/android/details/reports/UnitReportsFragment.kt | 3 +++ .../TrackerMap/android/details/reports/UnitReportsViewModel.kt | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) (limited to 'androidApp/src/main/java/mx/trackermap/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 1f1bb15..e1a24ec 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 @@ -128,6 +128,7 @@ class UnitReportsFragment : Fragment() { when (report) { is ReportController.Report.PositionsReport -> { display(report.positions) + display(unitReportsViewModel.geofences.value!!) showMap(true) } is ReportController.Report.EventsReport -> { @@ -136,6 +137,7 @@ class UnitReportsFragment : Fragment() { } is ReportController.Report.StopsReport -> { display(report.stops) + display(unitReportsViewModel.geofences.value!!) showMap(true) } is ReportController.Report.XlsxReport -> { @@ -301,6 +303,7 @@ class UnitReportsFragment : Fragment() { } private fun display(geofences: Array) { + Log.d("UnitReportsFragment", "Geofences: $geofences") if (reportsMapFragment.hasStarted) { Log.d("UnitReportsFragment", "Displaying geofences: $geofences") reportsMapFragment.displayGeofences(geofences) 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 a957435..7d1e028 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 @@ -25,7 +25,7 @@ class UnitReportsViewModel( private val _reportPeriod: MutableLiveData = savedStateHandle.getLiveData("reportPeriod", null) private val _report: MutableLiveData = MutableLiveData() - private val _geofences: MutableLiveData> = MutableLiveData() + private val _geofences: MutableLiveData> = MutableLiveData(emptyArray()) val deviceId: LiveData get() = _deviceId val reportType: LiveData get() = _reportType -- cgit v1.2.3