diff options
3 files changed, 29 insertions, 7 deletions
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, |