320 lines
12 KiB
Kotlin
320 lines
12 KiB
Kotlin
package app.klottr.ui
|
|
|
|
import androidx.compose.foundation.layout.*
|
|
import androidx.compose.runtime.*
|
|
import androidx.compose.runtime.collectAsState
|
|
import androidx.compose.ui.Modifier
|
|
import app.klottr.location.LastLocationStore
|
|
import app.klottr.location.LocationServiceProvider
|
|
import app.klottr.maps.*
|
|
import app.klottr.net.*
|
|
import app.klottr.platform.hasFgLocationPerms
|
|
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 MapContent(
|
|
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,
|
|
modifier: Modifier = Modifier.fillMaxSize(),
|
|
onFornsokClick: (FornsokItem) -> Unit,
|
|
// Extra-data och styrning för titel/radie
|
|
extraKlotter: List<NoteDTO> = emptyList(),
|
|
revealKlotterTitles: Boolean = false,
|
|
// Radie-override för klotter; null = använd default, 0 = obegränsat
|
|
klotterRadiusM: Int? = null,
|
|
onlyExtraKlotter: Boolean = false,
|
|
onFeaturesApplied: (() -> Unit)? = null,
|
|
satellite: Boolean,
|
|
night: Boolean
|
|
) {
|
|
println("🛰 Satellite flag = $satellite")
|
|
|
|
// --- Läs aktuella inställningar från SettingsStore (utan Flow) ---
|
|
// satellite kommer redan som parameter från KlottrAppShell
|
|
val showKlotter = SettingsStore.getShowKlotter()
|
|
val showWiki = SettingsStore.getShowWikipedia()
|
|
val wikiRadius = SettingsStore.getWikiRadiusMeters()
|
|
val showDm = SettingsStore.getShowDigitaltMuseum()
|
|
val dmRadius = SettingsStore.getDigitaltMuseumRadiusMeters()
|
|
val showFornsok = SettingsStore.getShowFornsok()
|
|
val fornsokRadius = 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()) }
|
|
|
|
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()
|
|
}
|
|
|
|
fun buildFeatures(): List<MapFeature> {
|
|
val lat = myLat; val lon = myLon
|
|
val out = mutableListOf<MapFeature>()
|
|
|
|
// ----- "Mitt klotter"-läge: visa ENDAST extraKlotter oavsett SettingsStore -----
|
|
if (onlyExtraKlotter) {
|
|
val radius = (klotterRadiusM ?: Config.DEFAULT_KLOTTER_RADIUS_M)
|
|
val unlimited = (radius == 0)
|
|
extraKlotter
|
|
.distinctBy { it.i }
|
|
.forEach { n ->
|
|
val d = if (lat != null && lon != null)
|
|
distanceMeters(lat, lon, n.a, n.o).roundToInt() else Int.MAX_VALUE
|
|
val within = if (unlimited) true else (d != Int.MAX_VALUE && d <= radius)
|
|
val titleLine = n.t.substringBefore('\n').ifBlank { "Okänt klotter" }
|
|
// I "Mitt klotter" ska rubriker ALLTID synas (det är ju dina egna), oavsett avstånd:
|
|
val display = titleLine
|
|
// Vi ritar ALLA markörer (within påverkar inte synlighet här):
|
|
out += MapFeature.Marker(
|
|
id = "klotter:${n.i}",
|
|
position = LatLng(n.a, n.o),
|
|
title = display
|
|
)
|
|
}
|
|
return out
|
|
}
|
|
// === Klotter-markörer (bakåtkompatibelt läge) ===
|
|
run {
|
|
// Om inga nya parametrar används → exakt gamla koden
|
|
val usingExtras = (extraKlotter.isNotEmpty() || revealKlotterTitles || klotterRadiusM != null)
|
|
|
|
if (!usingExtras) {
|
|
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 = LatLng(n.a, n.o),
|
|
title = display
|
|
)
|
|
}
|
|
}
|
|
return@run
|
|
}
|
|
|
|
// Nytt beteende (endast när extras faktiskt används)
|
|
val combined: List<app.klottr.net.NoteDTO> = when {
|
|
extraKlotter.isNotEmpty() && showKlotter -> (klotter + extraKlotter).distinctBy { it.i }
|
|
extraKlotter.isNotEmpty() && !showKlotter -> extraKlotter.distinctBy { it.i }
|
|
else -> if (showKlotter) klotter else emptyList()
|
|
}
|
|
|
|
val radius = (klotterRadiusM ?: Config.DEFAULT_KLOTTER_RADIUS_M)
|
|
val unlimited = (radius == 0)
|
|
|
|
combined.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 within = if (unlimited) true else (d != Int.MAX_VALUE && d <= radius)
|
|
val visited = isKnownNote(n.i.toLong())
|
|
|
|
val display = if (revealKlotterTitles || visited || (within && titleLine != "Okänt klotter"))
|
|
titleLine else "Okänt klotter"
|
|
|
|
out += MapFeature.Marker(
|
|
id = "klotter:${n.i}",
|
|
position = LatLng(n.a, n.o),
|
|
title = display
|
|
)
|
|
}
|
|
}
|
|
|
|
if (showWiki) {
|
|
wiki.forEach { w ->
|
|
out += MapFeature.Marker(
|
|
id = "wiki:${w.pageId}",
|
|
position = LatLng(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 = LatLng(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 = LatLng(f.lat, f.lon),
|
|
title = f.title
|
|
)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
val lastKnown = LastLocationStore.lastKnownLocation.value
|
|
val startLat = initialLat ?: lastKnown?.lat ?: 59.2974 // Årsta Torg
|
|
val startLon = initialLon ?: lastKnown?.lon ?: 18.0520
|
|
|
|
val mapState = remember(satellite) {
|
|
MapState(
|
|
initialCamera = CameraPosition(
|
|
target = LatLng(startLat, startLon),
|
|
zoom = 16f
|
|
),
|
|
initialUi = MapUiSettings(
|
|
compassEnabled = true,
|
|
zoomControlsEnabled = false,
|
|
myLocationButtonEnabled = hasFgLocationPerms(),
|
|
mapType = if (satellite) MapType.SATELLITE else MapType.NORMAL
|
|
)
|
|
)
|
|
}
|
|
|
|
Box(modifier = modifier) {
|
|
MapContainer(
|
|
state = mapState,
|
|
onMapClick = { /* inget extra här */ },
|
|
|
|
onMarkerClick = { id ->
|
|
when {
|
|
id.startsWith("klotter:") -> {
|
|
val nid = id.removePrefix("klotter:")
|
|
val n = klotter.firstOrNull { it.i.toString() == nid }
|
|
?: extraKlotter.firstOrNull { it.i.toString() == nid }
|
|
if (n != null) {
|
|
val titleLine =
|
|
n.t.substringBefore('\n').ifBlank { "Okänt klotter" }
|
|
onKlotterClick(nid, n.a, n.o, titleLine)
|
|
}
|
|
}
|
|
id.startsWith("wiki:") -> {
|
|
val pid = id.removePrefix("wiki:")
|
|
wiki.firstOrNull { it.pageId.toString() == pid }?.let { w ->
|
|
val url = "https://sv.m.wikipedia.org/?curid=${w.pageId}"
|
|
onOpenUrl(w.title, url)
|
|
}
|
|
}
|
|
id.startsWith("dm:") -> {
|
|
val mid = id.removePrefix("dm:")
|
|
dm.firstOrNull { (it.webUrl ?: it.url) == mid }?.let { dmi ->
|
|
val url = dmi.webUrl ?: dmi.url
|
|
url?.let { onOpenUrl(dmi.title, it) }
|
|
}
|
|
}
|
|
id.startsWith("forn:") -> {
|
|
val fid = id.removePrefix("forn:")
|
|
forn.firstOrNull { (it.uri ?: it.title) == fid }?.let { f ->
|
|
// Trigga samma "öppna fornsök" som i listan
|
|
onFornsokClick(f)
|
|
}
|
|
}
|
|
|
|
}
|
|
},
|
|
onReady = { ctrl ->
|
|
mapController = ctrl
|
|
ctrl.setMyLocationEnabled(hasFgLocationPerms())
|
|
ctrl.setFeatures(buildFeatures())
|
|
onFeaturesApplied?.invoke()
|
|
}
|
|
)
|
|
MapSyncEffects(
|
|
controller = mapController,
|
|
myLat = if (onlyExtraKlotter) null else myLat,
|
|
myLon = if (onlyExtraKlotter) null else myLon,
|
|
featuresBuilder = { buildFeatures() },
|
|
showKlotter, showWiki, wikiRadius, showDm, dmRadius, showFornsok, fornsokRadius,
|
|
klotter, wiki, dm, forn
|
|
)
|
|
// När vi visar endast egna klotter: applicera features igen när datan ändras,
|
|
// och signalera att features är på plats (så "Laddar..." kan stängas).
|
|
LaunchedEffect(onlyExtraKlotter, extraKlotter) {
|
|
if (onlyExtraKlotter) {
|
|
mapController?.setFeatures(buildFeatures())
|
|
onFeaturesApplied?.invoke()
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
@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(LatLng(myLat, myLon), 16f))
|
|
}
|
|
}
|
|
LaunchedEffect(controller, *featuresDeps) {
|
|
if (controller != null) {
|
|
controller.setFeatures(featuresBuilder())
|
|
}
|
|
}
|
|
}
|
|
|