From a0e16d47a97c27cb84d6e4d5e626b99c0e08081a Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Thu, 30 Dec 2021 16:46:21 -0600 Subject: Center map on device click --- .../TrackerMap/android/devices/DevicesAdapter.kt | 3 ++- .../TrackerMap/android/devices/DevicesFragment.kt | 19 +++++++++++++------ .../trackermap/TrackerMap/android/map/MapFragment.kt | 15 +++++++++++++++ .../TrackerMap/android/units/UnitsActivity.kt | 7 +++++-- .../TrackerMap/android/units/UnitsViewModel.kt | 12 ++++++++++++ 5 files changed, 47 insertions(+), 9 deletions(-) diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesAdapter.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesAdapter.kt index 4852757..ffe6ca7 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesAdapter.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/devices/DevicesAdapter.kt @@ -15,7 +15,7 @@ import mx.trackermap.TrackerMap.android.databinding.UnitItemBinding import mx.trackermap.TrackerMap.client.models.UnitInformation enum class Action { - DETAILS, REPORTS, COMMANDS + CLICK, DETAILS, REPORTS, COMMANDS } typealias ActionCallback = (unit: UnitInformation, action: Action) -> Unit @@ -75,6 +75,7 @@ class DevicesAdapter( lastAddress.text = unit.position?.address lastDate.text = "yyyy/mm/dd, hh:mm" actionCallback?.let { callback -> + unitCard.setOnClickListener { callback(unit, Action.CLICK) } detailsButton.setOnClickListener { callback(unit, Action.DETAILS) } reportsButton.setOnClickListener { callback(unit, Action.REPORTS) } commandsButton.setOnClickListener { callback(unit, Action.COMMANDS) } 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 a8782cc..933316a 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 @@ -64,11 +64,18 @@ class DevicesFragment : Fragment() { } private fun itemAction(unit: UnitInformation, action: Action) { - Log.d("DevicesFragment", "Action: $action - Unit: $unit") - val activity = requireActivity() - val intent = Intent(activity.applicationContext, DetailsActivity::class.java) - intent.putExtra(DetailsActivity.DEVICE_ID_EXTRA, unit.device.id) - intent.putExtra(DetailsActivity.ACTION_EXTRA, action) - startActivity(intent) + when (action) { + Action.DETAILS, Action.REPORTS, Action.COMMANDS -> { + Log.d("DevicesFragment", "Action: $action - Unit: $unit") + val activity = requireActivity() + val intent = Intent(activity.applicationContext, DetailsActivity::class.java) + intent.putExtra(DetailsActivity.DEVICE_ID_EXTRA, unit.device.id) + intent.putExtra(DetailsActivity.ACTION_EXTRA, action) + startActivity(intent) + } + Action.CLICK -> { + unitsViewModel.selectUnit(unit) + } + } } } \ No newline at end of file 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 f3d7cac..f0ec769 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 @@ -13,6 +13,7 @@ import kotlinx.coroutines.DelicateCoroutinesApi import mx.trackermap.TrackerMap.android.R import mx.trackermap.TrackerMap.android.databinding.MapFragmentBinding import mx.trackermap.TrackerMap.android.units.UnitsViewModel +import mx.trackermap.TrackerMap.client.models.UnitInformation import org.koin.androidx.viewmodel.ext.android.viewModel import java.io.File @@ -23,6 +24,8 @@ class MapFragment: GlobeMapFragment() { private val unitsViewModel: UnitsViewModel by viewModel() + private val markers = mutableListOf() + override fun chooseDisplayType(): MapDisplayType { return MapDisplayType.Map } @@ -116,11 +119,23 @@ class MapFragment: GlobeMapFragment() { marker.image = icon marker.size = markerSize marker.userObject = unit + markers.add(marker) mapControl.addScreenMarker(marker, markerInfo, ThreadMode.ThreadAny) } } } } + unitsViewModel.selectedUnit.observe(this) { + it?.let { unit -> + Log.d("MapFragment", "Centering map on ${it.position?.latitude}, ${it.position?.longitude}") + unit.position?.let { position -> + val latitude = position.latitude!! * Math.PI / 180 + val longitude = position.longitude!! * Math.PI / 180 + val zoom = 0.000008 + mapControl.animatePositionGeo(longitude, latitude, zoom, 1.0) + } + } + } } } \ No newline at end of file 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 ea40d91..452ece7 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 @@ -20,6 +20,9 @@ class UnitsActivity : AppCompatActivity() { private val unitsViewModel: UnitsViewModel by viewModel() + private val mapFragment = MapFragment() + private val devicesFragment = DevicesFragment() + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -66,8 +69,8 @@ class UnitsActivity : AppCompatActivity() { val newFragment = when (displayMode) { - UnitsViewModel.UnitsDisplayMode.LIST -> DevicesFragment() - UnitsViewModel.UnitsDisplayMode.MAP -> MapFragment() + UnitsViewModel.UnitsDisplayMode.LIST -> devicesFragment + UnitsViewModel.UnitsDisplayMode.MAP -> mapFragment else -> DevicesFragment() } supportFragmentManager.commit { diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsViewModel.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsViewModel.kt index bcb0818..bb4efde 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsViewModel.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsViewModel.kt @@ -27,6 +27,7 @@ class UnitsViewModel( var searchQuery = savedStateHandle.getLiveData("searchQuery", "") var unitsDisplayMode = MutableLiveData(UnitsDisplayMode.LIST) var units = MutableLiveData>() + var selectedUnit = MutableLiveData() init { Log.d("UnitsViewModel", "Initializing Units View Model") @@ -42,6 +43,17 @@ class UnitsViewModel( } } + fun selectUnit(unit: UnitInformation) { + Log.d("UnitsViewModel", "Selecting unit ${unit.device.name}") + selectedUnit.postValue(unit) + setDisplayMode(UnitsDisplayMode.MAP) + } + + fun setDisplayMode(displayMode: UnitsDisplayMode) { + Log.d("UnitsViewModel", "Setting Display mode to $displayMode") + unitsDisplayMode.postValue(displayMode) + } + fun toggleDisplayMode() { Log.d("UnitsViewModel", "Toggling Display mode") val newDisplayMode = -- cgit v1.2.3