From 246ac974cbc65b7240b40d8a06f891ef3a9a26fe Mon Sep 17 00:00:00 2001 From: Danny Baumann Date: Mon, 11 Sep 2023 10:22:58 +0200 Subject: [PATCH 1/2] Fix applying first map location If the map isn't loaded yet when the first map widget is bound, the bind attempt for that widget was discarded. Fix this by making sure to apply the widget bind attempt after initially loading the map. While at it, also make sure not to load the map until after the data saver check happened to not cause data transfer before that check. Fixes #3412 Signed-off-by: Danny Baumann --- .../org/openhab/habdroid/ui/MapViewHelper.kt | 40 ++++++++++++------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/mobile/src/full/java/org/openhab/habdroid/ui/MapViewHelper.kt b/mobile/src/full/java/org/openhab/habdroid/ui/MapViewHelper.kt index 4fc7d8da76..5c2b24deb2 100644 --- a/mobile/src/full/java/org/openhab/habdroid/ui/MapViewHelper.kt +++ b/mobile/src/full/java/org/openhab/habdroid/ui/MapViewHelper.kt @@ -46,24 +46,14 @@ object MapViewHelper { init { mapView.onCreate(null) - mapView.getMapAsync { map -> - this.map = map - with(map.uiSettings) { - setAllGesturesEnabled(false) - isMapToolbarEnabled = false - } - map.setOnMarkerClickListener { - openPopup() - true - } - map.setOnMapClickListener { openPopup() } - } } override fun bindAfterDataSaverCheck(widget: Widget) { super.bindAfterDataSaverCheck(widget) - map?.clear() - map?.applyPositionAndLabel(widget, 15.0f, false) + withLoadedMap { map -> + map.clear() + map.applyPositionAndLabel(widget, 15.0f, false) + } } override fun onStart() { @@ -82,6 +72,28 @@ object MapViewHelper { val widget = boundWidget ?: return fragmentPresenter.showBottomSheet(MapBottomSheet(), widget) } + + private fun withLoadedMap(callback: (map: GoogleMap) -> Unit) { + val loadedMap = this.map + if (loadedMap != null) { + callback(loadedMap) + return + } + + mapView.getMapAsync { map -> + this.map = map + with(map.uiSettings) { + setAllGesturesEnabled(false) + isMapToolbarEnabled = false + } + map.setOnMarkerClickListener { + openPopup() + true + } + map.setOnMapClickListener { openPopup() } + callback(map) + } + } } } From 2fbf4983585912d888cbd33890d2e3c0762c2c37 Mon Sep 17 00:00:00 2001 From: Danny Baumann Date: Mon, 11 Sep 2023 11:22:48 +0200 Subject: [PATCH 2/2] Improve GoogleMap.applyPositionAndLabel() implementation Signed-off-by: Danny Baumann --- .../org/openhab/habdroid/ui/MapViewHelper.kt | 34 ++++++++----------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/mobile/src/full/java/org/openhab/habdroid/ui/MapViewHelper.kt b/mobile/src/full/java/org/openhab/habdroid/ui/MapViewHelper.kt index 5c2b24deb2..659be38675 100644 --- a/mobile/src/full/java/org/openhab/habdroid/ui/MapViewHelper.kt +++ b/mobile/src/full/java/org/openhab/habdroid/ui/MapViewHelper.kt @@ -98,23 +98,18 @@ object MapViewHelper { } fun GoogleMap.applyPositionAndLabel(widget: Widget, zoomLevel: Float, allowDrag: Boolean) { - if (widget.item == null) { - return - } - val canDragMarker = allowDrag && !widget.item.readOnly - if (widget.item.members.isNotEmpty()) { - val positions = ArrayList() - for (member in widget.item.members) { - val position = member.state?.asLocation?.toLatLng() - if (position != null) { - setMarker(position, member, member.label, canDragMarker) - positions.add(position) - } - } - if (positions.isNotEmpty()) { + val item = widget.item ?: return + val canDragMarker = allowDrag && !item.readOnly + if (item.members.isNotEmpty()) { + val positionMap = item.members + .map { m -> m.state?.asLocation?.toLatLng()?.let { m to it } } + .filterNotNull() + .toMap() + if (positionMap.isNotEmpty()) { val boundsBuilder = LatLngBounds.Builder() - for (position in positions) { - boundsBuilder.include(position) + positionMap.forEach { member, pos -> + setMarker(pos, member, member.label, canDragMarker) + boundsBuilder.include(pos) } moveCamera(CameraUpdateFactory.newLatLngBounds(boundsBuilder.build(), 0)) if (cameraPosition.zoom > zoomLevel) { @@ -122,10 +117,9 @@ fun GoogleMap.applyPositionAndLabel(widget: Widget, zoomLevel: Float, allowDrag: } } } else { - val position = widget.item.state?.asLocation?.toLatLng() - if (position != null) { - setMarker(position, widget.item, widget.label, canDragMarker) - moveCamera(CameraUpdateFactory.newLatLngZoom(position, zoomLevel)) + item.state?.asLocation?.toLatLng()?.let { pos -> + setMarker(pos, item, widget.label, canDragMarker) + moveCamera(CameraUpdateFactory.newLatLngZoom(pos, zoomLevel)) } } }