diff options
author | Isidro Henoch <imhenoch@protonmail.com> | 2022-01-07 15:34:19 -0600 |
---|---|---|
committer | Isidro Henoch <imhenoch@protonmail.com> | 2022-01-07 15:34:19 -0600 |
commit | e4f78f4af356215650df9df32a13ceff8067e137 (patch) | |
tree | a188350b4d7dfa3483999ead32f8a73ad0df804a /androidApp/src | |
parent | e0e172d88af6a0dc173f150ec0dcffea08ad462a (diff) | |
download | etbsa-trackermap-mobile-e4f78f4af356215650df9df32a13ceff8067e137.tar.gz etbsa-trackermap-mobile-e4f78f4af356215650df9df32a13ceff8067e137.tar.bz2 etbsa-trackermap-mobile-e4f78f4af356215650df9df32a13ceff8067e137.zip |
Finishes the reports UI
Diffstat (limited to 'androidApp/src')
6 files changed, 154 insertions, 51 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 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 35ef343..98b8a34 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 @@ -26,6 +26,7 @@ class UnitReportsViewModel( class PositionsReport(val positions: Array<Position>) : Report() class EventsReport(val events: Array<Event>) : Report() class StopsReport(val stops: Array<Stop>) : Report() + object LoadingReport: Report() } enum class ReportType { @@ -76,6 +77,7 @@ class UnitReportsViewModel( Log.d("UnitReportsVM", "Current date:${dateFormat.format(currentDate)}") Log.d("UnitReportsVM", "Previous date:${dateFormat.format(previousDate)}") + report.postValue(Report.LoadingReport) viewModelScope.launch { when (reportType.value!!) { ReportType.POSITIONS -> fetchPositions(previousDate, currentDate) 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 7306cbf..ebd75fb 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 @@ -9,13 +9,11 @@ import android.view.View import android.view.ViewGroup import com.mousebird.maply.* import java.io.File -import kotlinx.coroutines.DelicateCoroutinesApi import mx.trackermap.TrackerMap.android.R import mx.trackermap.TrackerMap.utils.MarkerType typealias MarkerCallback = (Int?) -> Unit -@DelicateCoroutinesApi class MapFragment : GlobeMapFragment() { data class Marker( diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MarkerTransformations.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MarkerTransformations.kt new file mode 100644 index 0000000..fcde58d --- /dev/null +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MarkerTransformations.kt @@ -0,0 +1,71 @@ +package mx.trackermap.TrackerMap.android.map + +import mx.trackermap.TrackerMap.client.models.Position +import mx.trackermap.TrackerMap.client.models.Stop +import mx.trackermap.TrackerMap.client.models.UnitInformation +import mx.trackermap.TrackerMap.utils.MarkerType + +object MarkerTransformations { + fun unitToMarker(unit: UnitInformation): MapFragment.Marker? { + if (unit.position == null || unit.position!!.latitude == null || unit.position!!.longitude == null) { + return null + } + + return MapFragment.Marker( + unit.position!!.id!!, + unit.position!!.latitude!!, + unit.position!!.longitude!!, + categoryToMarkerType(unit.device.category ?: "") + ) + } + + fun positionToMarker(position: Position): MapFragment.Marker? { + if (position.latitude == null || position.longitude == null) { + return null + } + + return MapFragment.Marker( + position.id!!, + position.latitude!!, + position.longitude!! + ) + } + + fun stopToMarker(stop: Stop): MapFragment.Marker? { + if (stop.lat == null || stop.lon == null) { + return null + } + + return MapFragment.Marker( + stop.deviceId!!, + stop.lat!!, + stop.lon!! + ) + } + + private fun categoryToMarkerType(category: String): MarkerType { + return when (category.lowercase()) { + "animal" -> MarkerType.ANIMAL + "bicycle" -> MarkerType.BICYCLE + "boat" -> MarkerType.BOAT + "bus" -> MarkerType.BUS + "car" -> MarkerType.CAR + "crane" -> MarkerType.CRANE + "helicopter" -> MarkerType.HELICOPTER + "motorcycle" -> MarkerType.MOTORCYCLE + "offroad" -> MarkerType.OFFROAD + "person" -> MarkerType.PERSON + "pickup" -> MarkerType.PICKUP + "plane" -> MarkerType.PLANE + "scooter" -> MarkerType.SCOOTER + "ship" -> MarkerType.SHIP + "tractor" -> MarkerType.TRACTOR + "train" -> MarkerType.TRAIN + "tram" -> MarkerType.TRAM + "trolleybus" -> MarkerType.TROLLEYBUS + "truck" -> MarkerType.TRUCK + "van" -> MarkerType.VAN + else -> MarkerType.DEFAULT + } + } +}
\ No newline at end of file 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 20dd59b..427f020 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 @@ -16,7 +16,6 @@ import mx.trackermap.TrackerMap.android.devices.Action import mx.trackermap.TrackerMap.android.units.UnitsViewModel import mx.trackermap.TrackerMap.client.models.UnitInformation import mx.trackermap.TrackerMap.utils.Formatter -import mx.trackermap.TrackerMap.utils.MarkerType import mx.trackermap.TrackerMap.utils.SpeedUnit import org.koin.androidx.viewmodel.ext.android.viewModel @@ -56,7 +55,9 @@ class UnitMapFragment : Fragment() { unitsViewModel.units.observe(viewLifecycleOwner) { units -> Log.d("UnitMapFragment", "Available units: $units") - unitsMapFragment.display(units.mapNotNull(this::unitToMarker).toTypedArray()) + unitsMapFragment.display( + units.mapNotNull(MarkerTransformations::unitToMarker).toTypedArray() + ) } unitsViewModel.selectedUnit.observe(viewLifecycleOwner) { selectedUnit -> @@ -107,7 +108,8 @@ class UnitMapFragment : Fragment() { unitName.text = unit.device.name driverName.text = unit.device.contact - unitSpeed.text = Formatter.formatSpeed(unit.position?.speed ?: 0.0, SpeedUnit.KMH) + unitSpeed.text = + Formatter.formatSpeed(unit.position?.speed ?: 0.0, SpeedUnit.KMH) lastAddress.text = unit.position?.address unit.position?.fixTime?.let { lastDate.text = Formatter.formatDate(it) @@ -123,41 +125,6 @@ class UnitMapFragment : Fragment() { } } - private fun unitToMarker(unit: UnitInformation): MapFragment.Marker? { - if (unit.position == null || unit.position!!.latitude == null || unit.position!!.longitude == null) { - return null - } - - return MapFragment.Marker( - unit.position!!.id!!, - unit.position!!.latitude!!, - unit.position!!.longitude!!, - when (unit.device.category?.lowercase()) { - "animal" -> MarkerType.ANIMAL - "bicycle" -> MarkerType.BICYCLE - "boat" -> MarkerType.BOAT - "bus" -> MarkerType.BUS - "car" -> MarkerType.CAR - "crane" -> MarkerType.CRANE - "helicopter" -> MarkerType.HELICOPTER - "motorcycle" -> MarkerType.MOTORCYCLE - "offroad" -> MarkerType.OFFROAD - "person" -> MarkerType.PERSON - "pickup" -> MarkerType.PICKUP - "plane" -> MarkerType.PLANE - "scooter" -> MarkerType.SCOOTER - "ship" -> MarkerType.SHIP - "tractor" -> MarkerType.TRACTOR - "train" -> MarkerType.TRAIN - "tram" -> MarkerType.TRAM - "trolleybus" -> MarkerType.TROLLEYBUS - "truck" -> MarkerType.TRUCK - "van" -> MarkerType.VAN - else -> MarkerType.DEFAULT - } - ) - } - private fun itemAction(unit: UnitInformation, action: Action) { when (action) { Action.DETAILS, Action.REPORTS, Action.COMMANDS -> { diff --git a/androidApp/src/main/res/layout/unit_details_reports.xml b/androidApp/src/main/res/layout/unit_details_reports.xml index cf3c7b2..52a3926 100644 --- a/androidApp/src/main/res/layout/unit_details_reports.xml +++ b/androidApp/src/main/res/layout/unit_details_reports.xml @@ -5,17 +5,17 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - <View - android:id="@+id/reportMap" + <androidx.fragment.app.FragmentContainerView + android:id="@+id/reportsMapContainer" + android:name="mx.trackermap.TrackerMap.android.map.MapFragment" android:layout_width="0dp" android:layout_height="0dp" android:layout_margin="@dimen/margin" - android:background="@color/darkBackground" - android:visibility="gone" app:layout_constraintBottom_toTopOf="@id/periodSection" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toTopOf="parent" /> + app:layout_constraintTop_toTopOf="parent" + android:visibility="gone"/> <ScrollView android:id="@+id/eventsScroll" @@ -134,4 +134,14 @@ </LinearLayout> + <ProgressBar + android:id="@+id/reportLoading" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_margin="@dimen/margin" + app:layout_constraintBottom_toTopOf="@id/periodSection" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" /> + </androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file |