308 lines
12 KiB
Kotlin
308 lines
12 KiB
Kotlin
package app.klottr
|
|
|
|
import androidx.compose.foundation.layout.*
|
|
import androidx.compose.material3.AlertDialog
|
|
import androidx.compose.material3.Button
|
|
import androidx.compose.material3.Text
|
|
import androidx.compose.runtime.*
|
|
import androidx.compose.ui.Modifier
|
|
import androidx.compose.ui.platform.LocalWindowInfo
|
|
import androidx.compose.ui.unit.dp
|
|
import app.klottr.location.LocationServiceProvider
|
|
import app.klottr.maps.CameraPosition
|
|
import app.klottr.maps.LatLng as SharedLatLng
|
|
import app.klottr.maps.MapContainer
|
|
import app.klottr.maps.MapController
|
|
import app.klottr.maps.MapFeature
|
|
import app.klottr.maps.MapState
|
|
import app.klottr.maps.MapType
|
|
import app.klottr.maps.MapUiSettings
|
|
import app.klottr.net.DigitaltMuseum
|
|
import app.klottr.net.DigitaltMuseumItem
|
|
import app.klottr.net.FornsokItem
|
|
import app.klottr.net.FornsokShared
|
|
import app.klottr.net.Geocache
|
|
import app.klottr.net.NoteDTO
|
|
import app.klottr.net.Wikipedia
|
|
import app.klottr.net.WikipediaPage
|
|
import app.klottr.platform.hasFgLocationPerms
|
|
import app.klottr.ui.DmDialog
|
|
import app.klottr.ui.FornsokDialog
|
|
import app.klottr.ui.WikiArticleDialog
|
|
import app.klottr.util.Config
|
|
import app.klottr.util.SettingsStore
|
|
import app.klottr.util.distanceMeters
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.IO
|
|
import kotlinx.coroutines.withContext
|
|
import kotlin.math.roundToInt
|
|
|
|
@Composable
|
|
fun MapDialog(
|
|
onClose: () -> Unit,
|
|
onKlotterClick: (id: String, lat: Double, lon: Double, label: String) -> Unit,
|
|
onOpenUrl: (title: String, url: String) -> Unit,
|
|
isKnownNote: (Long) -> Boolean,
|
|
initialLat: Double? = null,
|
|
initialLon: Double? = null
|
|
) {
|
|
val dialogWidth = (LocalWindowInfo.current.containerSize.width.dp * 0.98f)
|
|
val dialogHeight = (LocalWindowInfo.current.containerSize.height.dp * 0.70f)
|
|
|
|
val satellite = SettingsStore.getSatelliteMap()
|
|
val showKlotter = remember { SettingsStore.getShowKlotter() }
|
|
val showWiki = remember { SettingsStore.getShowWikipedia() }
|
|
val wikiRadius = remember { SettingsStore.getWikiRadiusMeters() }
|
|
val showDm = remember { SettingsStore.getShowDigitaltMuseum() }
|
|
val dmRadius = remember { SettingsStore.getDigitaltMuseumRadiusMeters() }
|
|
val showFornsok = remember { SettingsStore.getShowFornsok() }
|
|
val fornsokRadius = remember { SettingsStore.getFornsokRadiusMeters() }
|
|
|
|
var myLat by remember { mutableStateOf<Double?>(null) }
|
|
var myLon by remember { mutableStateOf<Double?>(null) }
|
|
|
|
var klotter by remember { mutableStateOf<List<NoteDTO>>(emptyList()) }
|
|
var wiki by remember { mutableStateOf<List<WikipediaPage>>(emptyList()) }
|
|
var dm by remember { mutableStateOf<List<DigitaltMuseumItem>>(emptyList()) }
|
|
var forn by remember { mutableStateOf<List<FornsokItem>>(emptyList()) }
|
|
|
|
// TODO: Not used? do they need to?
|
|
var selLat by remember { mutableStateOf<Double?>(null) }
|
|
var selLon by remember { mutableStateOf<Double?>(null) }
|
|
var selTitle by remember { mutableStateOf<String?>(null) }
|
|
var selUri by remember { mutableStateOf<String?>(null) }
|
|
var selId by remember { mutableStateOf<String?>(null) } // klotter-id vid behov
|
|
var selLayer by remember { mutableStateOf<String?>(null) } // "Klotter", "Wikipedia", "Fornsök", ...
|
|
var selWiki by remember { mutableStateOf<WikipediaPage?>(null) }
|
|
var selForn by remember { mutableStateOf<FornsokItem?>(null) }
|
|
var selDm by remember { mutableStateOf<DigitaltMuseumItem?>(null) }
|
|
|
|
|
|
// Map state for shared MapContainer
|
|
val mapState = remember {
|
|
MapState(
|
|
initialCamera = CameraPosition(
|
|
target = SharedLatLng(
|
|
initialLat ?: 59.3293,
|
|
initialLon ?: 18.0686
|
|
),
|
|
zoom = 16f
|
|
),
|
|
initialUi = MapUiSettings(
|
|
compassEnabled = true,
|
|
zoomControlsEnabled = false,
|
|
myLocationButtonEnabled = hasFgLocationPerms(),
|
|
mapType = if (satellite) MapType.SATELLITE else MapType.NORMAL
|
|
)
|
|
)
|
|
}
|
|
var mapController by remember { mutableStateOf<MapController?>(null) }
|
|
|
|
val locService = remember { LocationServiceProvider.get() }
|
|
DisposableEffect(Unit) {
|
|
locService.start()
|
|
onDispose { locService.stop() }
|
|
}
|
|
val current = locService.location.collectAsState(initial = null)
|
|
LaunchedEffect(current.value?.lat, current.value?.lon) {
|
|
myLat = current.value?.lat
|
|
myLon = current.value?.lon
|
|
}
|
|
|
|
LaunchedEffect(myLat, myLon, showKlotter, showWiki, wikiRadius, showDm, dmRadius, showFornsok, fornsokRadius) {
|
|
val lat = myLat; val lon = myLon
|
|
if (lat == null || lon == null) return@LaunchedEffect
|
|
|
|
if (showKlotter) {
|
|
klotter = withContext(Dispatchers.IO) {
|
|
Geocache.getNearby(lat = lat, lon = lon, radiusMeters = 300, ttlMs = 0L)
|
|
}
|
|
} else klotter = emptyList()
|
|
|
|
if (showWiki) {
|
|
wiki = withContext(Dispatchers.IO) {
|
|
Wikipedia.getNearby(lat = lat, lon = lon, radiusMeters = wikiRadius)
|
|
}
|
|
} else wiki = emptyList()
|
|
|
|
if (showDm) {
|
|
dm = withContext(Dispatchers.IO) {
|
|
DigitaltMuseum.getNearby(lat = lat, lon = lon, radiusMeters = dmRadius)
|
|
}
|
|
} else dm = emptyList()
|
|
|
|
if (showFornsok) {
|
|
forn = withContext(Dispatchers.IO) {
|
|
FornsokShared.getNearby(lat = lat, lon = lon, radiusMeters = fornsokRadius)
|
|
}
|
|
} else forn = emptyList()
|
|
}
|
|
|
|
// Helper: build features for the map
|
|
fun buildFeatures(): List<MapFeature> {
|
|
val lat = myLat; val lon = myLon
|
|
val out = mutableListOf<MapFeature>()
|
|
if (showKlotter) {
|
|
klotter.forEach { n ->
|
|
val d = if (lat != null && lon != null)
|
|
distanceMeters(lat, lon, n.a, n.o).roundToInt() else Int.MAX_VALUE
|
|
val titleLine = n.t.substringBefore('\n').ifBlank { "Okänt klotter" }
|
|
val radius = Config.DEFAULT_KLOTTER_RADIUS_M
|
|
val visited = isKnownNote(n.i.toLong())
|
|
val within = (d != Int.MAX_VALUE && d <= radius)
|
|
val display = if (visited || (within && titleLine != "Okänt klotter")) titleLine else "Okänt klotter"
|
|
out += MapFeature.Marker(
|
|
id = "klotter:${n.i}",
|
|
position = SharedLatLng(n.a, n.o),
|
|
title = display
|
|
)
|
|
}
|
|
}
|
|
if (showWiki) {
|
|
wiki.forEach { w ->
|
|
out += MapFeature.Marker(
|
|
id = "wiki:${w.pageId}",
|
|
position = SharedLatLng(w.lat, w.lon),
|
|
title = w.title
|
|
)
|
|
}
|
|
}
|
|
if (showDm) {
|
|
dm.forEach { dmi ->
|
|
val dmId = dmi.webUrl ?: dmi.url ?: "${dmi.lat},${dmi.lon}"
|
|
out += MapFeature.Marker(
|
|
id = "dm:$dmId",
|
|
position = SharedLatLng(dmi.lat, dmi.lon),
|
|
title = dmi.title
|
|
)
|
|
}
|
|
}
|
|
if (showFornsok) {
|
|
forn.forEach { f ->
|
|
val fid = f.uri ?: f.title
|
|
out += MapFeature.Marker(
|
|
id = "forn:$fid",
|
|
position = SharedLatLng(f.lat, f.lon),
|
|
title = f.title
|
|
)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
|
|
AlertDialog(
|
|
onDismissRequest = onClose,
|
|
title = { Text("Karta") },
|
|
text = {
|
|
Box(
|
|
Modifier
|
|
.widthIn(max = dialogWidth)
|
|
.heightIn(min = 320.dp, max = dialogHeight)
|
|
.fillMaxWidth()
|
|
) {
|
|
// Shared Map
|
|
MapContainer(
|
|
state = mapState,
|
|
onMapClick = {
|
|
// Clear selection
|
|
selLat = null; selLon = null; selTitle = null; selUri = null; selId = null; selLayer = null
|
|
},
|
|
onMarkerClick = { id ->
|
|
fun setSel(lat: Double, lon: Double, title: String?, uri: String?, layer: String, itemId: String? = null) {
|
|
selLat = lat; selLon = lon; selTitle = title; selUri = uri; selLayer = layer; selId = itemId
|
|
}
|
|
when {
|
|
id.startsWith("klotter:") -> {
|
|
val nid = id.removePrefix("klotter:")
|
|
klotter.firstOrNull { it.i.toString() == nid }?.let { n ->
|
|
val titleLine = n.t.substringBefore('\n').ifBlank { "Okänt klotter" }
|
|
setSel(n.a, n.o, titleLine, null, "Klotter", nid)
|
|
onKlotterClick(nid, n.a, n.o, titleLine)
|
|
}
|
|
}
|
|
id.startsWith("wiki:") -> {
|
|
val pid = id.removePrefix("wiki:")
|
|
wiki.firstOrNull { it.pageId.toString() == pid }?.let { w ->
|
|
selWiki = w
|
|
}
|
|
}
|
|
id.startsWith("dm:") -> {
|
|
val mid = id.removePrefix("dm:")
|
|
dm.firstOrNull { (it.webUrl ?: it.url) == mid }?.let { dmi ->
|
|
selDm = dmi
|
|
}
|
|
}
|
|
id.startsWith("forn:") -> {
|
|
val fid = id.removePrefix("forn:")
|
|
forn.firstOrNull { (it.uri ?: it.title) == fid }?.let { f ->
|
|
selForn = f
|
|
}
|
|
}
|
|
}
|
|
},
|
|
onReady = { ctrl ->
|
|
mapController = ctrl
|
|
ctrl.setMyLocationEnabled(hasFgLocationPerms())
|
|
ctrl.setFeatures(buildFeatures())
|
|
}
|
|
)
|
|
// Keep camera + markers in sync with state changes
|
|
MapSyncEffects(
|
|
controller = mapController,
|
|
myLat = if (initialLat == null && initialLon == null) myLat else null, // don't override explicit initial cam
|
|
myLon = if (initialLat == null && initialLon == null) myLon else null,
|
|
featuresBuilder = { buildFeatures() },
|
|
// everything that affects markers must be referenced here:
|
|
showKlotter, showWiki, wikiRadius, showDm, dmRadius, showFornsok, fornsokRadius,
|
|
klotter, wiki, dm, forn
|
|
)
|
|
}
|
|
},
|
|
confirmButton = {
|
|
Button(onClick = onClose) { Text("Stäng") }
|
|
}
|
|
)
|
|
if (selWiki != null) {
|
|
WikiArticleDialog(
|
|
pageId = selWiki!!.pageId,
|
|
lat = selWiki!!.lat,
|
|
lon = selWiki!!.lon,
|
|
onClose = { selWiki = null }
|
|
)
|
|
}
|
|
if (selForn != null) {
|
|
FornsokDialog(
|
|
item = selForn!!,
|
|
onClose = { selForn = null }
|
|
)
|
|
}
|
|
if (selDm != null) {
|
|
DmDialog(
|
|
item = selDm!!,
|
|
onClose = { selDm = null }
|
|
)
|
|
}
|
|
}
|
|
|
|
// Keep map in sync with state changes, camera position, enabled features.
|
|
@Composable
|
|
private fun MapSyncEffects(
|
|
controller: MapController?,
|
|
myLat: Double?, myLon: Double?,
|
|
featuresBuilder: () -> List<MapFeature>,
|
|
vararg featuresDeps: Any?
|
|
) {
|
|
LaunchedEffect(controller, myLat, myLon) {
|
|
if (controller != null && myLat != null && myLon != null) {
|
|
controller.animateCamera(CameraPosition(SharedLatLng(myLat, myLon), 16f))
|
|
}
|
|
}
|
|
// Rebuild + apply features whenever any dependency changes
|
|
LaunchedEffect(controller, *featuresDeps) {
|
|
if (controller != null) {
|
|
controller.setFeatures(featuresBuilder())
|
|
}
|
|
}
|
|
}
|