refactor: migrate MapFragment to Composable (#647)

This commit is contained in:
Andre K 2023-06-24 07:58:01 -03:00 committed by GitHub
parent e15cdc42f1
commit d4879ceea9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 1207 additions and 823 deletions

View file

@ -1,18 +1,14 @@
package com.geeksville.mesh.model.map
import android.content.Context
import android.view.MotionEvent
import org.osmdroid.tileprovider.MapTileProviderBase
import org.osmdroid.views.MapView
import org.osmdroid.views.overlay.DefaultOverlayManager
import org.osmdroid.views.overlay.TilesOverlay
class CustomOverlayManager
/**
* Default constructor
* CustomOverlayManager with disabled double taps events
*/
(tilesOverlay: TilesOverlay?) : DefaultOverlayManager(tilesOverlay) {
class CustomOverlayManager(tilesOverlay: TilesOverlay?) : DefaultOverlayManager(tilesOverlay) {
/**
* Override event & do nothing
*/
@ -26,19 +22,4 @@ class CustomOverlayManager
override fun onDoubleTapEvent(e: MotionEvent?, pMapView: MapView?): Boolean {
return true
}
companion object {
/**
* Create MyOverlayManager
*/
fun create(mapView: MapView, context: Context?): CustomOverlayManager {
val mTileProvider: MapTileProviderBase = mapView.tileProvider
val tilesOverlay = TilesOverlay(mTileProvider, context)
mapView.tileProvider
mapView.overlayManager = CustomOverlayManager(tilesOverlay)
//mapView.overlayManager.overlays().add(overlay)
mapView.invalidate()
return CustomOverlayManager(tilesOverlay)
}
}
}

View file

@ -167,23 +167,25 @@ class CustomTileSource {
val DEFAULT_TILE_SOURCE: OnlineTileSourceBase = TileSourceFactory.DEFAULT_TILE_SOURCE
/**
* The order in this list must match that in the arrays.xml under map_styles
* Source for each available [ITileSource] and their display names.
*/
val mTileSources: List<ITileSource> =
listOf(
MAPNIK,
USGS_TOPO,
OPEN_TOPO,
ESRI_WORLD_TOPO,
USGS_SAT,
ESRI_IMAGERY,
)
val mTileSources: Map<ITileSource, String> = mapOf(
MAPNIK to "OpenStreetMap",
USGS_TOPO to "USGS TOPO",
OPEN_TOPO to "Open TOPO",
ESRI_WORLD_TOPO to "ESRI World TOPO",
USGS_SAT to "USGS Satellite",
ESRI_IMAGERY to "ESRI World Overview",
)
fun getTileSource(index: Int): ITileSource {
return mTileSources.keys.elementAtOrNull(index) ?: DEFAULT_TILE_SOURCE
}
fun getTileSource(aName: String): ITileSource {
for (tileSource: ITileSource in mTileSources) {
for (tileSource: ITileSource in mTileSources.keys) {
if (tileSource.name().equals(aName)) {
return tileSource;
return tileSource
}
}
throw IllegalArgumentException("No such tile source: $aName")

View file

@ -0,0 +1,63 @@
package com.geeksville.mesh.model.map
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.view.MotionEvent
import org.osmdroid.views.MapView
import org.osmdroid.views.overlay.Marker
class MarkerWithLabel(mapView: MapView?, label: String, emoji: String? = null) : Marker(mapView) {
private var onLongClickListener: (() -> Boolean)? = null
fun setOnLongClickListener(listener: () -> Boolean) {
onLongClickListener = listener
}
private val mLabel = label
private val mEmoji = emoji
private val textPaint = Paint().apply {
textSize = 40f
color = Color.DKGRAY
isAntiAlias = true
isFakeBoldText = true
textAlign = Paint.Align.CENTER
}
private val emojiPaint = Paint().apply {
textSize = 80f
isAntiAlias = true
textAlign = Paint.Align.CENTER
}
private val bgPaint = Paint().apply { color = Color.WHITE }
private fun getTextBackgroundSize(text: String, x: Float, y: Float): Rect {
val fontMetrics = textPaint.fontMetrics
val halfTextLength = textPaint.measureText(text) / 2 + 3
return Rect(
(x - halfTextLength).toInt(),
(y + fontMetrics.top).toInt(),
(x + halfTextLength).toInt(),
(y + fontMetrics.bottom).toInt()
)
}
override fun onLongPress(event: MotionEvent?, mapView: MapView?): Boolean {
val touched = hitTest(event, mapView)
if (touched && this.id != null) {
return onLongClickListener?.invoke() ?: super.onLongPress(event, mapView)
}
return super.onLongPress(event, mapView)
}
override fun draw(c: Canvas, osmv: MapView?, shadow: Boolean) {
super.draw(c, osmv, false)
val p = mPositionPixels
val bgRect = getTextBackgroundSize(mLabel, (p.x - 0f), (p.y - 110f))
c.drawRect(bgRect, bgPaint)
c.drawText(mLabel, (p.x - 0f), (p.y - 110f), textPaint)
mEmoji?.let { c.drawText(it, (p.x - 0f), (p.y - 30f), emojiPaint) }
}
}