diff options
author | Iván Ávalos <avalos@disroot.org> | 2022-01-17 15:59:22 -0600 |
---|---|---|
committer | Iván Ávalos <avalos@disroot.org> | 2022-01-17 15:59:22 -0600 |
commit | 1b1480e0fad512aa0c01d0b33865b818625cf420 (patch) | |
tree | f16dabbb765f3ed87ed1eff3a69eab19f0b14692 /androidApp/src | |
parent | 2f64c3904216279a62afaa9aefbd1e49f88d3cc4 (diff) | |
parent | 07d184c408aba980f331ecaa3e4baef80b6ce82c (diff) | |
download | etbsa-trackermap-mobile-1b1480e0fad512aa0c01d0b33865b818625cf420.tar.gz etbsa-trackermap-mobile-1b1480e0fad512aa0c01d0b33865b818625cf420.tar.bz2 etbsa-trackermap-mobile-1b1480e0fad512aa0c01d0b33865b818625cf420.zip |
Merge branch 'main' of https://git.sr.ht/~avalos/trackermap-mobile
Diffstat (limited to 'androidApp/src')
6 files changed, 112 insertions, 30 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..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 @@ -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 @@ -42,9 +43,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 +60,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 +87,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,12 +121,14 @@ class UnitReportsFragment : Fragment() { } private fun setupObservers() { + Log.d("UnitReportsFragment", "Adding observers") unitReportsViewModel.report.observe(viewLifecycleOwner) { report -> Log.d("UnitReportsFragment", "Report available: $report") when (report) { is ReportController.Report.PositionsReport -> { display(report.positions) + display(unitReportsViewModel.geofences.value!!) showMap(true) } is ReportController.Report.EventsReport -> { @@ -123,6 +137,7 @@ class UnitReportsFragment : Fragment() { } is ReportController.Report.StopsReport -> { display(report.stops) + display(unitReportsViewModel.geofences.value!!) showMap(true) } is ReportController.Report.XlsxReport -> { @@ -150,11 +165,17 @@ class UnitReportsFragment : Fragment() { } ) } + + unitReportsViewModel.geofences.observe(viewLifecycleOwner) { geofences -> + display(geofences) + } } private fun removeObservers() { + Log.d("UnitReportsFragment", "Removing observers") unitReportsViewModel.report.removeObservers(viewLifecycleOwner) unitReportsViewModel.reportPeriod.removeObservers(viewLifecycleOwner) + unitReportsViewModel.clearReport() } private fun showPeriodPopUp(view: View) { @@ -179,15 +200,22 @@ class UnitReportsFragment : Fragment() { } private fun display(positions: Array<Position>) { - 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<EventInformation>) { @@ -257,15 +285,33 @@ class UnitReportsFragment : Fragment() { } private fun display(stops: Array<Stop>) { - 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<Geofence>) { + Log.d("UnitReportsFragment", "Geofences: $geofences") + 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 7749b54..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 @@ -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<ReportDates.ReportPeriod> = savedStateHandle.getLiveData("reportPeriod", null) private val _report: MutableLiveData<ReportController.Report> = MutableLiveData() + private val _geofences: MutableLiveData<Array<Geofence>> = MutableLiveData(emptyArray()) val deviceId: LiveData<Int> get() = _deviceId val reportType: LiveData<ReportController.ReportType> get() = _reportType val reportPeriod: LiveData<ReportDates.ReportPeriod> get() = _reportPeriod val report: LiveData<ReportController.Report> get() = _report + val geofences: LiveData<Array<Geofence>> 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 } @@ -74,7 +88,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/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/MapFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapFragment.kt index 17fc9d4..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<SetupCallback>() var markerCallback: MarkerCallback? = null private val objects = mutableListOf<ComponentObject>() @@ -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 @@ -127,6 +132,10 @@ class MapFragment : GlobeMapFragment() { } private fun clear(geofences: Boolean = false) { + if (mapControl == null) { + return + } + if (geofences) { mapControl.removeObjects( geofenceObjects, 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 3283778..7d42588 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?) { |