diff options
Diffstat (limited to 'androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt')
-rw-r--r-- | androidApp/src/main/java/mx/trackermap/TrackerMap/android/details/reports/UnitReportsFragment.kt | 67 |
1 files changed, 61 insertions, 6 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 9874a03..bbd1fd7 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 @@ -9,9 +9,12 @@ import android.widget.PopupMenu import android.widget.TableRow import android.widget.TextView import androidx.fragment.app.Fragment +import androidx.fragment.app.commit import mx.trackermap.TrackerMap.android.R import mx.trackermap.TrackerMap.android.databinding.UnitDetailsReportsBinding import mx.trackermap.TrackerMap.android.details.UnitDetailsAdapter +import mx.trackermap.TrackerMap.android.map.MapFragment +import mx.trackermap.TrackerMap.android.map.MarkerTransformations import mx.trackermap.TrackerMap.client.models.Event import mx.trackermap.TrackerMap.client.models.Position import mx.trackermap.TrackerMap.client.models.Stop @@ -23,6 +26,9 @@ class UnitReportsFragment : Fragment() { private val binding get() = _binding!! private val unitReportsViewModel: UnitReportsViewModel by viewModel() + private val reportsMapFragment: MapFragment by lazy { + MapFragment() + } override fun onCreateView( inflater: LayoutInflater, @@ -38,6 +44,7 @@ class UnitReportsFragment : Fragment() { unitReportsViewModel.deviceId.value = arguments?.getInt(UnitDetailsAdapter.DEVICE_ID_ARG) ?: 0 + initializeMap() setupEvents() setupObservers() } @@ -47,6 +54,12 @@ class UnitReportsFragment : Fragment() { _binding = null } + private fun initializeMap() { + childFragmentManager.commit { + replace(R.id.reportsMapContainer, reportsMapFragment) + } + } + private fun setupEvents() { binding.reportType.setOnPositionChangedListener { position -> unitReportsViewModel.reportType.value = when (position) { @@ -67,9 +80,19 @@ class UnitReportsFragment : Fragment() { Log.d("UnitReportsFragment", "Report available: $report") when (report) { - is UnitReportsViewModel.Report.PositionsReport -> display(report.positions) - is UnitReportsViewModel.Report.EventsReport -> display(report.events) - is UnitReportsViewModel.Report.StopsReport -> display(report.stops) + is UnitReportsViewModel.Report.PositionsReport -> { + display(report.positions) + showMap(true) + } + is UnitReportsViewModel.Report.EventsReport -> { + display(report.events) + showMap(false) + } + is UnitReportsViewModel.Report.StopsReport -> { + display(report.stops) + showMap(true) + } + is UnitReportsViewModel.Report.LoadingReport -> loading() } } @@ -107,14 +130,18 @@ class UnitReportsFragment : Fragment() { Log.d("UnitReportsFragment", "Displaying positions: $positions") binding.eventsScroll.visibility = View.GONE - binding.reportMap.visibility = View.VISIBLE + binding.reportsMapContainer.visibility = View.VISIBLE + + reportsMapFragment.display( + positions.mapNotNull(MarkerTransformations::positionToMarker).toTypedArray() + ) } private fun display(events: Array<Event>) { Log.d("UnitReportsFragment", "Displaying events: $events") binding.eventsScroll.visibility = View.VISIBLE - binding.reportMap.visibility = View.GONE + binding.reportsMapContainer.visibility = View.GONE binding.eventsTable.removeViews(1, Math.max(0, binding.eventsTable.childCount - 1)) val context = context!! @@ -136,6 +163,34 @@ class UnitReportsFragment : Fragment() { Log.d("UnitReportsFragment", "Displaying stops: $stops") binding.eventsScroll.visibility = View.GONE - binding.reportMap.visibility = View.VISIBLE + binding.reportsMapContainer.visibility = View.VISIBLE + + reportsMapFragment.display( + stops.mapNotNull(MarkerTransformations::stopToMarker).toTypedArray() + ) + } + + private fun loading() { + binding.reportLoading.visibility = View.VISIBLE + binding.eventsScroll.visibility = View.GONE + binding.reportsMapContainer.visibility = View.GONE + + childFragmentManager.commit { + hide(reportsMapFragment) + } + } + + private fun showMap(shouldShowMap: Boolean) { + binding.reportLoading.visibility = View.GONE + binding.eventsScroll.visibility = if (shouldShowMap) View.GONE else View.VISIBLE + binding.reportsMapContainer.visibility = if (shouldShowMap) View.VISIBLE else View.GONE + + childFragmentManager.commit { + if (shouldShowMap) { + show(reportsMapFragment) + } else { + hide(reportsMapFragment) + } + } } }
\ No newline at end of file |