KMP/UI: shared scrollbars, common “Nearby” open resolver, and map panel shell — wired into app

shared(ui): FoundList fills its space and adds a lightweight, material-free scrollbar thumb; expose optional LazyListState.
shared(ui): Harden FoundList scrollbar math (non-negative weights, safe totals) and use Color.takeOrElse for color fallback to avoid startup crash.
shared(ui): NearbyList fills its space and adds the same lightweight scrollbar; optional LazyListState support.

shared(nearby): Add resolveNearbyOpenAction for Nearby entries; map to FoundOpenAction (Klotter/Wiki/Fornsok/DM); fix import placement and remove unused import.
shared(ui): NearbyContent now fills its space (remove 420dp height cap) and delegates onEntryClick to resolveNearbyOpenAction.

shared(ui): Add material-free MapPanelShell to host title/main/footers above map and reuse across platforms.

app(ui): Use MapPanelShell for the main “Senaste” panel in KlottrApp — move header, FoundContent area, and login/logout row into the shared shell; main area is full-height.
app(ui): Use MapPanelShell inside NearbyDialog and set a sensible max height (~80% of screen) so content is clearly scrollable; keep AlertDialog title/buttons.
This commit is contained in:
2025-09-14 17:55:43 +02:00
parent dae2513b7c
commit 4e2d062329
2 changed files with 220 additions and 163 deletions

View File

@@ -136,6 +136,7 @@ import app.klottr.ui.FoundList // Delad, material-fri listkomponent för "Hittat
import app.klottr.ui.FoundContent // Delat innehåll för “Hittat” import app.klottr.ui.FoundContent // Delat innehåll för “Hittat”
import app.klottr.ui.NearbyEntryRow import app.klottr.ui.NearbyEntryRow
import app.klottr.ui.NearbyList // Delad material-fri listkomponent import app.klottr.ui.NearbyList // Delad material-fri listkomponent
import app.klottr.ui.MapPanelShell
import app.klottr.util.Auth import app.klottr.util.Auth
import app.klottr.util.Config import app.klottr.util.Config
import app.klottr.util.FoundStore import app.klottr.util.FoundStore
@@ -720,29 +721,30 @@ private fun KlottrApp(
HorizontalDivider() // Avdelare under knappraden HorizontalDivider() // Avdelare under knappraden
//Sätt textstil för allt inom blocket så även BasicText (i SharedInfoCard) blir läsbar i mörkt läge MapPanelShell(
Column(Modifier.padding(horizontal = 4.dp, vertical = 8.dp)) { // Tar bort ProvideTextStyle-wrappern (onödig nu) modifier = Modifier
Text( .fillMaxWidth()
"Senaste", .weight(1f),
style = MaterialTheme.typography.titleLarge, title = "Senaste",
color = MaterialTheme.colorScheme.onSurface //Sätt färg direkt så rubriken är tydlig i mörkt läge titleTextStyle = MaterialTheme.typography.titleLarge.copy(
) color = MaterialTheme.colorScheme.onSurface
Spacer(Modifier.height(6.dp)) ),
} mainContent = {
// Hittat (flyttat hit för att kapslas i skalet)
// Hittat
val foundList = val foundList =
if (forceEmptyFound) emptyList() if (forceEmptyFound) emptyList()
else FoundStore.getAll(context) else FoundStore.getAll(context)
val labelOverrides = remember { androidx.compose.runtime.mutableStateMapOf<String, String>() } // FQN så rätt top-level-funktion används trots import-trassel
val foundKey = foundList.joinToString("|") { "${it.layer}:${it.id}" } // Stabil nyckel som inte ändras vid tema-byte val labelOverrides = remember { androidx.compose.runtime.mutableStateMapOf<String, String>() }
// Buffra detalj + bild för kända klotter inom radien → snabbare öppning
LaunchedEffect(foundKey) { // Kör bara när innehållet faktiskt ändras, inte vid tema-byte val foundKey = foundList.joinToString("|") { "${it.layer}:${it.id}" }
// Förbättra etiketter och förladda detaljer/bilder när innehållet ändras
LaunchedEffect(foundKey) {
val my = LastLocationStore.lastKnownLocation val my = LastLocationStore.lastKnownLocation
val myLat = my?.latitude val myLat = my?.latitude
val myLon = my?.longitude val myLon = my?.longitude
labelOverrides.clear() // Nollställ vid nytt innehåll (ny foundKey) labelOverrides.clear()
foundList.forEach { f -> foundList.forEach { f ->
if (f.layer == LAYER_KLOTTER) { if (f.layer == LAYER_KLOTTER) {
val inside = if (myLat != null && myLon != null) val inside = if (myLat != null && myLon != null)
@@ -751,7 +753,6 @@ private fun KlottrApp(
if (inside) { if (inside) {
val idL = f.id.toLongOrNull() ?: return@forEach val idL = f.id.toLongOrNull() ?: return@forEach
if (NoteDetailCache.get(idL) == null) { if (NoteDetailCache.get(idL) == null) {
// Hämta detalj med NOTISENS koordinater (ger rätt “bevis” på servern)
val detail = fetchNoteDetail(idL, f.lat, f.lon) val detail = fetchNoteDetail(idL, f.lat, f.lon)
if (detail != null) { if (detail != null) {
NoteDetailCache.put(idL, detail) NoteDetailCache.put(idL, detail)
@@ -769,10 +770,8 @@ private fun KlottrApp(
} }
} }
} }
// Förbättra etikett för klotter om vi bara har tom/”Okänt klotter”
// Berika etiketter för klotter i ett svep via delad hjälpare (IO-tråd)
val enriched = withContext(Dispatchers.IO) { val enriched = withContext(Dispatchers.IO) {
enrichKlotterLabels( // enrichKlotterLabels(
found = foundList, found = foundList,
radiusMeters = 60, radiusMeters = 60,
fetchNearbyKlotter = { la, lo, r -> fetchNearbyKlotter = { la, lo, r ->
@@ -780,7 +779,6 @@ private fun KlottrApp(
} }
) )
} }
// Uppdatera overrides-kartan i en batch
for ((k, v) in enriched) { for ((k, v) in enriched) {
labelOverrides[k] = v labelOverrides[k] = v
} }
@@ -791,99 +789,76 @@ private fun KlottrApp(
if (foundList.isEmpty()) { if (foundList.isEmpty()) {
Text("Inga fynd ännu.") Text("Inga fynd ännu.")
} else { } else {
Box(
modifier = Modifier
.fillMaxWidth()
.weight(1f)
) {
val my = LastLocationStore.lastKnownLocation val my = LastLocationStore.lastKnownLocation
val _tick = locationTick val _tick = locationTick
Box(modifier = Modifier.fillMaxSize()) { // Gör ytan fullhöjd för innehållet Box(modifier = Modifier.fillMaxSize()) {
FoundContent( // Ingen modifier här FoundContent(
entries = foundList, currentLat = my?.latitude, currentLon = my?.longitude, showCoords = showCoords, labelOverrides = labelOverrides, isClickable = { f -> (f.layer == FoundStore.LAYER_WIKI) || entries = foundList,
currentLat = my?.latitude,
currentLon = my?.longitude,
showCoords = showCoords,
labelOverrides = labelOverrides,
isClickable = { f ->
(f.layer == FoundStore.LAYER_WIKI) ||
(f.layer == FoundStore.LAYER_FORNSOK) || (f.layer == FoundStore.LAYER_FORNSOK) ||
(f.layer == FoundStore.LAYER_KLOTTER) || (f.layer == FoundStore.LAYER_KLOTTER) ||
(f.layer == FoundStore.LAYER_DM) (f.layer == FoundStore.LAYER_DM)
}, },
onEntryClick = { f -> // Flytta logiken till delad resolver onEntryClick = { f ->
scope.launch { // kör som korutin på huvudtråden scope.launch {
val action = withContext(Dispatchers.IO) { // nätverk/IO utanför UI-tråd val action = withContext(Dispatchers.IO) {
resolveFoundOpenAction( // Gemensam logik i :shared resolveFoundOpenAction(
entry = f, // entry = f,
labelOverrides = labelOverrides, // Använd förbättrade etiketter labelOverrides = labelOverrides,
fornsokAround = { lat, lon, radius -> // DI: Fornsök i närheten fornsokAround = { lat, lon, radius ->
try { Fornsok.getNearby(lat, lon, radius) } catch (_: Exception) { emptyList() } try { Fornsok.getNearby(lat, lon, radius) } catch (_: Exception) { emptyList() }
}, },
dmLookupByIdentifier = { id -> // DI: DigitaltMuseum-uppslag dmLookupByIdentifier = { id ->
try { app.klottr.net.DigitaltMuseum.lookupByIdentifier(id) } catch (_: Exception) { null } try { app.klottr.net.DigitaltMuseum.lookupByIdentifier(id) } catch (_: Exception) { null }
} }
) )
} }
when (action) { // Mappa delad åtgärd till plattformsnavigation when (action) {
is FoundOpenAction.Klotter -> { is FoundOpenAction.Klotter -> {
openKlotter = OpenKlotter( // openKlotter = OpenKlotter(
id = action.id, // id = action.id,
lat = action.lat, // lat = action.lat,
lon = action.lon, // lon = action.lon,
label = action.label // label = action.label
) )
} }
is FoundOpenAction.Wiki -> { is FoundOpenAction.Wiki -> {
openWiki = OpenWiki(action.pageId, action.lat, action.lon) // openWiki = OpenWiki(action.pageId, action.lat, action.lon)
} }
is FoundOpenAction.Fornsok -> { is FoundOpenAction.Fornsok -> {
openFornsok = OpenFornsok(action.item) // openFornsok = OpenFornsok(action.item)
} }
is FoundOpenAction.DigitaltMuseum -> { is FoundOpenAction.DigitaltMuseum -> {
openDm = action.item // openDm = action.item
} }
FoundOpenAction.None -> { /* no-op */ } // FoundOpenAction.None -> Unit
} }
} }
}, // [Avslutar onEntryClick-parametern },
isBold = { f -> !FoundStore.isAway(context, f.layer, f.id) }, distanceFormatter = ::formatDistance, coordsFormatter = ::formatCoords, rowTextStyle = MaterialTheme.typography.bodyMedium.copy( color = MaterialTheme.colorScheme.onSurface isBold = { f -> !FoundStore.isAway(context, f.layer, f.id) },
) distanceFormatter = ::formatDistance,
) coordsFormatter = ::formatCoords,
} // rowTextStyle = MaterialTheme.typography.bodyMedium.copy(
color = MaterialTheme.colorScheme.onSurface
// Scroll-hints (behåll som i föregående patch)
Box(
modifier = Modifier
.align(Alignment.TopCenter) //
.fillMaxWidth() //
.height(12.dp) //
.background(
brush = Brush.verticalGradient(
colors = listOf(
MaterialTheme.colorScheme.surface,
MaterialTheme.colorScheme.surface.copy(alpha = 0f)
)
)
)
)
Box(
modifier = Modifier
.align(Alignment.BottomCenter) //
.fillMaxWidth() //
.height(12.dp) //
.background(
brush = Brush.verticalGradient(
colors = listOf(
MaterialTheme.colorScheme.surface.copy(alpha = 0f),
MaterialTheme.colorScheme.surface
)
)
) )
) )
} }
} }
},
footerContent = {
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
if (!loggedIn) Button(onClick = onLogin) { Text("Logga in") } if (!loggedIn) Button(onClick = onLogin) { Text("Logga in") }
if (loggedIn) Button(onClick = onLogout) { Text("Logga ut") } if (loggedIn) Button(onClick = onLogout) { Text("Logga ut") }
} }
} }
)
}
} }
// Om-appen // Om-appen
if (showAbout) { if (showAbout) {
@@ -1245,15 +1220,13 @@ private fun NearbyDialog(
AlertDialog( AlertDialog(
onDismissRequest = onClose, onDismissRequest = onClose,
title = { Text("Närliggande") }, title = { Text("Närliggande") },
text = { // Använd delat dialoginnehåll text = {
val hasLocation = (lat != null && lon != null) // Flagga för plats val hasLocation = (lat != null && lon != null)
// Kända klotteretiketter (cache) — O(1)-lookup
val knownKlotterLabels = FoundStore.getAll(ctx) val knownKlotterLabels = FoundStore.getAll(ctx)
.asSequence() .asSequence()
.filter { it.layer == FoundStore.LAYER_KLOTTER } .filter { it.layer == FoundStore.LAYER_KLOTTER }
.associate { it.id to it.label } .associate { it.id to it.label }
// Bygg delade entries om vi har plats, annars tom lista
val entries: List<NearbyEntryShared> = val entries: List<NearbyEntryShared> =
if (hasLocation) { if (hasLocation) {
buildNearbyEntries( buildNearbyEntries(
@@ -1271,7 +1244,6 @@ private fun NearbyDialog(
) )
} else emptyList() } else emptyList()
// Markera “hittade” innan render (endast när vi inte laddar/har fel och har plats)
if (hasLocation && !loading && error == null) { if (hasLocation && !loading && error == null) {
app.klottr.nearby.markNewFinds( app.klottr.nearby.markNewFinds(
ctx = ctx, ctx = ctx,
@@ -1281,7 +1253,19 @@ private fun NearbyDialog(
) )
} }
// Rendera material-fritt delat innehåll (hanterar själv loading/fel/tom-lista) // Dialogens innehåll i delat skal med rimlig höjd (max ~80% av skärmen)
val cfg = LocalConfiguration.current
val maxDialogHeight = (cfg.screenHeightDp * 0.8f).dp
MapPanelShell(
modifier = Modifier
.fillMaxWidth()
.heightIn(min = 360.dp, max = maxDialogHeight), // Gör dialogytan stor nog och skrollbar
title = null, // AlertDialog visar redan "Närliggande" som titel
titleTextStyle = MaterialTheme.typography.titleLarge.copy(
color = MaterialTheme.colorScheme.onSurface
),
mainContent = {
NearbyContent( NearbyContent(
loading = loading, loading = loading,
error = error, error = error,
@@ -1293,10 +1277,12 @@ private fun NearbyDialog(
onDmClick = onDmClick, onDmClick = onDmClick,
onFornsokClick = onFornsokClick, onFornsokClick = onFornsokClick,
onKlotterClick = onKlotterClick, onKlotterClick = onKlotterClick,
rowTextStyle = MaterialTheme.typography.bodyMedium.copy( // Säker textfärg i dag/natt rowTextStyle = MaterialTheme.typography.bodyMedium.copy(
color = MaterialTheme.colorScheme.onSurface color = MaterialTheme.colorScheme.onSurface
) )
) )
}
)
}, },
confirmButton = { Button(onClick = onClose) { Text("Stäng") } } confirmButton = { Button(onClick = onClose) { Text("Stäng") } }
) )

View File

@@ -0,0 +1,71 @@
package app.klottr.ui
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import androidx.compose.foundation.text.BasicText
/**
* Material-fritt skal för en panel ovanpå en karta.
* Innehåller valfri titel/överdel, en huvudyta som fyller resten av höjden,
* samt en valfri fotrad (t.ex. knappar).
*/
@Composable
fun MapPanelShell(
modifier: Modifier = Modifier,
title: String? = null,
titleTextStyle: TextStyle,
topRightContent: (@Composable () -> Unit)? = null,
mainContent: @Composable () -> Unit,
footerContent: (@Composable () -> Unit)? = null
) {
Column(modifier = modifier.fillMaxSize()) {
if (title != null || topRightContent != null) {
Box(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 12.dp)
) {
if (title != null) {
BasicText(
text = title,
style = titleTextStyle
)
}
if (topRightContent != null) {
Box(
modifier = Modifier.fillMaxWidth(),
contentAlignment = Alignment.CenterEnd
) {
topRightContent()
}
}
}
}
Box(
modifier = Modifier
.weight(1f)
.fillMaxWidth()
) {
mainContent()
}
if (footerContent != null) {
Box(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 12.dp, vertical = 8.dp)
) {
footerContent()
}
}
}
}