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..659be38675 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,27 +72,44 @@ 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) + } + } } } 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) { @@ -110,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)) } } }