From dfe5dd6a425ac2418e33e0b64911b6f914eb514c Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Mon, 17 Jan 2022 23:09:46 -0600 Subject: Pass overlay top margin and whether should show layer toggle as arguments for MapWrapperFragment, and refactored stuff --- .../android/details/reports/UnitReportsFragment.kt | 7 +-- .../TrackerMap/android/map/MapWrapperFragment.kt | 51 +++++++++++++++++++--- .../TrackerMap/android/map/UnitMapFragment.kt | 15 ++++--- .../trackermap/TrackerMap/android/shared/Utils.kt | 26 +++++++++++ .../TrackerMap/android/units/UnitsActivity.kt | 15 ++----- 5 files changed, 86 insertions(+), 28 deletions(-) create mode 100644 androidApp/src/main/java/mx/trackermap/TrackerMap/android/shared/Utils.kt (limited to 'androidApp/src/main/java/mx/trackermap') 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 2fb94f3..51d2f6d 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 @@ -39,9 +39,7 @@ class UnitReportsFragment : Fragment() { private val binding get() = _binding!! private val unitReportsViewModel: UnitReportsViewModel by viewModel() - private val mapFragment: MapWrapperFragment by lazy { - MapWrapperFragment() - } + private lateinit var mapFragment: MapWrapperFragment override fun onCreateView( inflater: LayoutInflater, @@ -84,6 +82,9 @@ class UnitReportsFragment : Fragment() { private fun initializeMap() { Log.d("UnitReportsFragment", "initializeMap()") + mapFragment = MapWrapperFragment.newInstance( + showLayerToggle = true + ) childFragmentManager.commit { replace(R.id.reportsMapContainer, mapFragment) } diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapWrapperFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapWrapperFragment.kt index 2b28a06..cecb567 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapWrapperFragment.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapWrapperFragment.kt @@ -11,6 +11,7 @@ import androidx.fragment.app.commit import mx.trackermap.TrackerMap.android.R import mx.trackermap.TrackerMap.android.databinding.FragmentMapWrapperBinding import mx.trackermap.TrackerMap.android.shared.MarkerTransformations +import mx.trackermap.TrackerMap.android.shared.Utils import mx.trackermap.TrackerMap.client.models.* class MapWrapperFragment: Fragment() { @@ -30,6 +31,7 @@ class MapWrapperFragment: Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) setupEvents() + setupViews() } override fun onResume() { @@ -79,7 +81,7 @@ class MapWrapperFragment: Fragment() { mapFragment.zoomIn() } else { mapFragment.setupCallbacks.add { - mapFragment.zoomIn() + binding.zoomInButton.performClick() } } } @@ -88,19 +90,37 @@ class MapWrapperFragment: Fragment() { mapFragment.zoomOut() } else { mapFragment.setupCallbacks.add { - mapFragment.zoomOut() + binding.zoomOutButton.performClick() + } + } + } + binding.mapLayerToggle.setOnClickListener { + context?.let { context -> + if (mapFragment.hasStarted) { + Utils.showLayersPopUp(context, it) { type -> + mapFragment.updateLayer(type) + } + } else { + binding.mapLayerToggle.performClick() } } } } - fun setMarkerCallback(callback: MarkerCallback) { - mapFragment.markerCallback = callback + private fun setupViews() { + val overlayMarginTop = arguments?.getInt(OVERLAY_MARGIN_TOP_ARG) + val showLayerToggle = arguments?.getBoolean(SHOW_LAYER_TOGGLE_ARG) + + overlayMarginTop?.let { top -> + binding.overlay.setPadding(0, top, 0, 0) + } + showLayerToggle?.let { show -> + binding.mapLayerToggle.visibility = if (show) View.VISIBLE else View.GONE + } } - fun setOverlayPaddingTop(top: Int) { - val overlay by lazy { binding.overlay } - overlay.setPadding(0, top, 0, 0) + fun setMarkerCallback(callback: MarkerCallback) { + mapFragment.markerCallback = callback } fun focusOn( @@ -187,4 +207,21 @@ class MapWrapperFragment: Fragment() { } } } + + companion object { + const val SHOW_LAYER_TOGGLE_ARG = "show_layer_toggle" + const val OVERLAY_MARGIN_TOP_ARG = "overlay_margin_top" + + fun newInstance( + showLayerToggle: Boolean = false, + overlayMarginTop: Int = 0 + ): MapWrapperFragment { + val args = Bundle() + args.putBoolean(SHOW_LAYER_TOGGLE_ARG, showLayerToggle) + args.putInt(OVERLAY_MARGIN_TOP_ARG, overlayMarginTop) + val fragment = MapWrapperFragment() + fragment.arguments = args + return fragment + } + } } \ 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 1a39ab1..768c0f0 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,8 +6,8 @@ import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import androidx.core.text.HtmlCompat import androidx.fragment.app.Fragment +import androidx.fragment.app.commit import androidx.fragment.app.viewModels import kotlin.time.ExperimentalTime import kotlinx.coroutines.DelicateCoroutinesApi @@ -16,7 +16,6 @@ import mx.trackermap.TrackerMap.android.databinding.UnitMapFragmentBinding import mx.trackermap.TrackerMap.android.details.DetailsActivity import mx.trackermap.TrackerMap.android.shared.UnitRenderData import mx.trackermap.TrackerMap.android.units.UnitsViewModel -import mx.trackermap.TrackerMap.client.models.MapLayer import mx.trackermap.TrackerMap.client.models.UnitInformation @DelicateCoroutinesApi @@ -61,10 +60,14 @@ class UnitMapFragment : Fragment() { } private fun initializeMap() { - shouldCenter = shouldCenter && unitsViewModel.selectedUnit.value == null - mapFragment = childFragmentManager.findFragmentById(R.id.unitsMap) as MapWrapperFragment + shouldCenter = true + mapFragment = MapWrapperFragment.newInstance( + overlayMarginTop = resources.getDimensionPixelSize(R.dimen.nav_height) + ) + childFragmentManager.commit { + replace(R.id.unitsMap, mapFragment) + } mapFragment.setMarkerCallback(unitsViewModel::selectUnitWith) - mapFragment.setOverlayPaddingTop(resources.getDimensionPixelSize(R.dimen.nav_height)) } private fun setupObservers() { @@ -75,7 +78,7 @@ class UnitMapFragment : Fragment() { mapFragment.display( units.toTypedArray(), isReport = false, - center = shouldCenter + center = shouldCenter && unitsViewModel.selectedUnit.value == null ) if (units.isNotEmpty()) { shouldCenter = false diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/shared/Utils.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/shared/Utils.kt new file mode 100644 index 0000000..56a9167 --- /dev/null +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/shared/Utils.kt @@ -0,0 +1,26 @@ +package mx.trackermap.TrackerMap.android.shared + +import android.content.Context +import android.view.View +import androidx.appcompat.widget.PopupMenu +import mx.trackermap.TrackerMap.android.R +import mx.trackermap.TrackerMap.client.models.MapLayer + +class Utils { + companion object { + fun showLayersPopUp(context: Context, view: View, callback: (layer: MapLayer.Type) -> Unit) { + val popOver = PopupMenu(context, view) + popOver.menuInflater.inflate(R.menu.map_layers, popOver.menu) + popOver.setOnMenuItemClickListener { item -> + val layer = when (item.itemId) { + R.id.layerStreets -> MapLayer.Type.STREETS + R.id.layerSatellite -> MapLayer.Type.SATELLITE + else -> MapLayer.Type.STREETS + } + callback(layer) + true + } + popOver.show() + } + } +} \ 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 adc0cb9..9fcef7f 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 @@ -19,7 +19,7 @@ import mx.trackermap.TrackerMap.android.databinding.UnitsActivityBinding import mx.trackermap.TrackerMap.android.devices.DevicesFragment import mx.trackermap.TrackerMap.android.map.UnitMapFragment import mx.trackermap.TrackerMap.android.session.UserInformationActivity -import mx.trackermap.TrackerMap.client.models.MapLayer +import mx.trackermap.TrackerMap.android.shared.Utils import org.koin.androidx.viewmodel.ext.android.viewModel import kotlin.time.ExperimentalTime @@ -94,18 +94,9 @@ class UnitsActivity : AppCompatActivity() { } private fun showLayersPopUp(view: View) { - val popOver = PopupMenu(this, view) - popOver.menuInflater.inflate(R.menu.map_layers, popOver.menu) - popOver.setOnMenuItemClickListener { item -> - val layer = when (item.itemId) { - R.id.layerStreets -> MapLayer.Type.STREETS - R.id.layerSatellite -> MapLayer.Type.SATELLITE - else -> MapLayer.Type.STREETS - } - unitsViewModel.setMapLayerType(layer) - true + Utils.showLayersPopUp(this, view) { + unitsViewModel.setMapLayerType(it) } - popOver.show() } private fun unfocusSearch(clearText: Boolean = false) { -- cgit v1.2.3