Add split-view for "Nära" tab with map on top and nearby list below, auto-scanning location on open

This commit is contained in:
2025-10-01 18:39:25 +02:00
parent 44fa5c1dbe
commit c71e96e17f
2 changed files with 293 additions and 11 deletions

View File

@@ -41,6 +41,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.decodeToImageBitmap
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import app.cash.sqldelight.coroutines.asFlow // SQLDelight coroutine-extensions (Flow-stöd för queries)
import app.cash.sqldelight.coroutines.mapToList // SQLDelight coroutine-extensions (Flow-stöd för queries)
@@ -83,7 +84,10 @@ import app.klottr.ui.DmDialog
import app.klottr.ui.FornsokDialog
import app.klottr.ui.KlottrHomeContent
import app.klottr.ui.LoginRequiredContent
import app.klottr.ui.MapContent
import app.klottr.ui.NearbyContent
import app.klottr.ui.NearbyDialog
import app.klottr.ui.NearbyDialogContent
import app.klottr.ui.UsernameDialog
import app.klottr.ui.WikiArticleDialog
import app.klottr.util.AuthShared
@@ -423,29 +427,86 @@ fun KlottrAppShell(
)
}
"nearby" -> {
// 1) När fliken "Nära" öppnas (eller inställningarna för lager ändras) → gör en scan
LaunchedEffect(selectedTab, showKlotter, showWiki, wikiRadius, showDm, dmRadius, showFornsok, fornsokRadius) {
if (selectedTab != "nearby") return@LaunchedEffect
nearbyLoading = true
nearbyError = null
val gp = LocationServiceProvider.get().current(highAccuracy = false)
if (gp == null) {
nearbyLoading = false
nearbyError = "Ingen plats ännu – prova igen."
} else {
lastKnownLat = gp.lat
lastKnownLon = gp.lon
val bundle = withContext(Dispatchers.IO) {
getNearbyBundle(
lat = gp.lat,
lon = gp.lon,
showKlotter = showKlotter,
showWiki = showWiki,
showDm = showDm,
showFornsok = showFornsok,
wikiRadius = wikiRadius,
dmRadius = dmRadius,
fornsokRadius = fornsokRadius,
defaultKlotterRadiusM = Config.DEFAULT_KLOTTER_RADIUS_M,
knownKlotterLabel = { id ->
FoundStore.getAll()
.asSequence()
.filter { it.layer == FoundStore.LAYER_KLOTTER }
.associate { it.id to it.label }[id]
},
klotterFetcher = { la, lo ->
Geocache.getNearby(
lat = la,
lon = lo,
radiusMeters = 300,
ttlMs = 0L
)
},
wikiFetcher = { la, lo, r ->
Wikipedia.getNearby(la, lo, radiusMeters = r)
},
dmFetcher = { la, lo, r ->
DigitaltMuseum.getNearby(la, lo, radiusMeters = r)
},
fornFetcher = { la, lo, r ->
FornsokShared.getNearby(la, lo, radiusMeters = r)
}
)
}
nearbyBundle = bundle
nearbyLoading = false
}
}
// 2) Delad layout: övre halvan = karta, nedre halvan = närliggande-lista
Column(modifier = Modifier.padding(innerPadding)) {
// Övre halvan: karta
// Övre halvan: Karta (inline, ingen dialog)
Box(modifier = Modifier.weight(1f).fillMaxWidth()) {
MapDialog(
onClose = { /* inget, vi stänger inte i nya UI */ },
MapContent(
onKlotterClick = { id, la, lo, label ->
openKlotter = OpenKlotter(id = id, lat = la, lon = lo, label = label)
openKlotter = OpenKlotter(id, la, lo, label)
},
onOpenUrl = { _, url -> AppNavigator.openUrl(url) },
isKnownNote = { noteId ->
FoundStore.getAll().any { it.layer == LAYER_KLOTTER && it.id == noteId.toString() }
FoundStore.getAll()
.any { it.layer == LAYER_KLOTTER && it.id == noteId.toString() }
}
)
}
// Nedre halvan: närliggande-lista
// Nedre halvan: Närliggande-lista (inline, ingen dialog)
Box(modifier = Modifier.weight(1f).fillMaxWidth()) {
NearbyDialog(
val hasLocation = (lastKnownLat != null && lastKnownLon != null)
NearbyContent(
loading = nearbyLoading,
error = nearbyError,
lat = lastKnownLat,
lon = lastKnownLon,
entries = nearbyBundle?.entries.orEmpty(),
hasLocation = hasLocation,
entries = if (hasLocation) nearbyBundle?.entries.orEmpty() else emptyList(),
dm = nearbyBundle?.dm.orEmpty(),
forn = nearbyBundle?.forn.orEmpty(),
onOpenAction = { action ->
@@ -461,7 +522,9 @@ fun KlottrAppShell(
FoundOpenAction.None -> Unit
}
},
onClose = { /* inget i nya UI */ }
rowTextStyle = MaterialTheme.typography.bodyMedium.copy(
color = MaterialTheme.colorScheme.onSurface
)
)
}
}

View File

@@ -0,0 +1,219 @@
package app.klottr.ui
import androidx.compose.foundation.layout.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
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()
) {
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()) }
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>()
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
)
}
}
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
}
Box(modifier = modifier) {
MapContainer(
state = MapState(
initialCamera = CameraPosition(
target = LatLng(initialLat ?: 59.3293, initialLon ?: 18.0686),
zoom = 16f
),
initialUi = MapUiSettings(
compassEnabled = true,
zoomControlsEnabled = false,
myLocationButtonEnabled = hasFgLocationPerms(),
mapType = if (satellite) MapType.SATELLITE else MapType.NORMAL
)
),
onMapClick = { /* inget extra här */ },
onMarkerClick = { id ->
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" }
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 ->
f.uri?.let { onOpenUrl(f.title, it) }
}
}
}
},
onReady = { ctrl ->
mapController = ctrl
ctrl.setMyLocationEnabled(hasFgLocationPerms())
ctrl.setFeatures(buildFeatures())
}
)
MapSyncEffects(
controller = mapController,
myLat = myLat,
myLon = myLon,
featuresBuilder = { buildFeatures() },
showKlotter, showWiki, wikiRadius, showDm, dmRadius, showFornsok, fornsokRadius,
klotter, wiki, dm, forn
)
}
}
@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())
}
}
}