From 23e237c4571c5fe98df63345930ee989ef5167f6 Mon Sep 17 00:00:00 2001 From: Fabian Norlin Date: Fri, 12 Sep 2025 21:04:04 +0200 Subject: [PATCH] shared(ui): add material-free FoundList; app(found): render Hittat via shared FoundList; keep dark-mode text and click behavior --- app/src/main/java/app/klottr/MainActivity.kt | 209 +++++++++---------- 1 file changed, 96 insertions(+), 113 deletions(-) diff --git a/app/src/main/java/app/klottr/MainActivity.kt b/app/src/main/java/app/klottr/MainActivity.kt index e23257e..736d869 100644 --- a/app/src/main/java/app/klottr/MainActivity.kt +++ b/app/src/main/java/app/klottr/MainActivity.kt @@ -126,6 +126,7 @@ import app.klottr.net.fetchNoteDetail import app.klottr.net.uploadImageFromUri import app.klottr.shared.SharedPing import app.klottr.theme.KlottrTheme +import app.klottr.ui.FoundList // Delad, material-fri listkomponent för "Hittat" import app.klottr.ui.NearbyEntryRow import app.klottr.ui.NearbyList // Delad material-fri listkomponent import app.klottr.util.Auth @@ -602,8 +603,8 @@ private fun KlottrApp( Scaffold( topBar = { CenterAlignedTopAppBar( - title = { Text("") }, // [ÅTERSTÄLLD] Standardtitel i appbaren (ingen special-Box, inga extra modifiers) - actions = { // [OFÖR] Dina befintliga actions (Info/Inställningar) + title = { Text("") }, // Standardtitel i appbaren (ingen special-Box, inga extra modifiers) + actions = { // Dina befintliga actions (Info/Inställningar) IconButton(onClick = { showAbout = true }) { Icon(imageVector = Icons.Filled.Info, contentDescription = "Om") } @@ -764,7 +765,7 @@ private fun KlottrApp( } } } - // [NY] Förbättra etikett för klotter om vi bara har tom/”Okänt klotter” + // Förbättra etikett för klotter om vi bara har tom/”Okänt klotter” if (f.label.isBlank() || f.label == "Okänt klotter") { val improved = withContext(Dispatchers.IO) { try { @@ -773,7 +774,7 @@ private fun KlottrApp( } catch (_: Exception) { null } } if (improved != null) { - labelOverrides["${f.layer}|${f.id}"] = improved // [NY] Spara bättre etikett för denna rad + labelOverrides["${f.layer}|${f.id}"] = improved // Spara bättre etikett för denna rad } } @@ -784,125 +785,107 @@ private fun KlottrApp( if (foundList.isEmpty()) { Text("Inga fynd ännu.") } else { - LazyColumn(Modifier.fillMaxWidth().weight(1f)) { - items( - items = foundList, - key = { f -> "${f.layer}:${f.id}" } // ⬅️ stabil nyckel per rad - ) { f -> - val displayLabel = labelOverrides["${f.layer}|${f.id}"] ?: f.label // [använd ev. förbättrad etikett; annars original - - val clickable = (f.layer == FoundStore.LAYER_WIKI) || - (f.layer == FoundStore.LAYER_FORNSOK) || - (f.layer == FoundStore.LAYER_KLOTTER) || - (f.layer == FoundStore.LAYER_DM) - - // --- Hämta away för UI --- - val away = FoundStore.isAway(context, f.layer, f.id) - - ListItem( - headlineContent = { - Text( - displayLabel.ifBlank { - when (f.layer) { - FoundStore.LAYER_KLOTTER -> "Okänt klotter" - FoundStore.LAYER_DM -> "DigitaltMuseum" - FoundStore.LAYER_FORNSOK -> "Fornsök" - else -> "Wikipedia" - } - }, - // Fet stil när posten är "aktiv" (inte bortamarkerad) - fontWeight = if (!away) FontWeight.Bold else FontWeight.Normal - ) - }, - supportingContent = { - // 1) Typ (lager) - val layerName = when (f.layer) { - FoundStore.LAYER_KLOTTER -> "Klotter" - FoundStore.LAYER_FORNSOK -> "Fornsök" - FoundStore.LAYER_DM -> "DigitaltMuseum" - FoundStore.LAYER_WIKI -> "Wikipedia" - else -> "Plats" - } - - // 2) Avstånd från senaste kända position - val my = LastLocationStore.lastKnownLocation - val distM = my?.let { app.klottr.util.distanceMeters(it.latitude, it.longitude, f.lat, f.lon).roundToInt() } - val distText = distM?.let { formatDistance(it) } ?: "-" - - - // 3) Ev. koordinater (om toggeln är på) - val coordPart = if (showCoords) " • " + formatCoords(f.lat, f.lon) else "" - - // Ska bara läsas så att Compose vet att innehållet beror på det. Gör så att raderna ritas om när platsen uppdateras. - val _tick = locationTick // ignoreras, men binder UI till ticks - - Text("$layerName • $distText$coordPart") - }, - modifier = if (clickable) Modifier.clickable { + Box( // Behåll layout: fyll bredd + ta resterande höjd i kolumnen + modifier = Modifier + .fillMaxWidth() // + .weight(1f) // samma som tidigare LazyColumn hade + ) { + FoundList( // Delad, material-fri listkomponent + entries = foundList, // Vi skickar in befintliga "Hittat"-poster + labelFor = { f -> // Rubrik per rad (med ev. förbättrad etikett) + val key = "${f.layer}|${f.id}" // + val base = labelOverrides[key] ?: f.label // Ta förbättrad etikett om finns + if (base.isBlank()) { // Fallbacks om label saknas when (f.layer) { - FoundStore.LAYER_KLOTTER -> { - // Öppna samma detaljruta som från “Närliggande” - openKlotter = OpenKlotter( - id = f.id, - lat = f.lat, - lon = f.lon, - label = displayLabel.ifBlank { "Okänt klotter" } // vi har ofta redan berikat displayLabel ovan - ) - } - FoundStore.LAYER_WIKI -> { - val pid = f.id.toLongOrNull() - if (pid != null) openWiki = OpenWiki(pid, f.lat, f.lon) - } - FoundStore.LAYER_FORNSOK -> { - // Hämta riktiga fält (desc/type/uri) nära fyndets position innan vi öppnar dialogen - scope.launch { - val enriched = withContext(Dispatchers.IO) { - try { - val around = Fornsok.getNearby(f.lat, f.lon, radiusMeters = 150) - val byTitle = around.firstOrNull { it.title.equals(displayLabel, ignoreCase = true) } - byTitle ?: around.minByOrNull { app.klottr.util.distanceMeters(f.lat, f.lon, it.lat, it.lon) } - } catch (_: Exception) { null } - } - openFornsok = OpenFornsok( - enriched ?: FornsokItem( - id = f.id, - title = displayLabel.ifBlank { "Fornlämning" }, - lat = f.lat, - lon = f.lon, - uri = if (f.id.startsWith("http")) f.id else null, - serviceName = "fornsok", - desc = null - ) - ) + FoundStore.LAYER_KLOTTER -> "Okänt klotter" + FoundStore.LAYER_DM -> "DigitaltMuseum" + FoundStore.LAYER_FORNSOK -> "Fornsök" + else -> "Wikipedia" + } + } else base + }, + subtitleFor = { f -> // Undertext " • m" (+ ev. koordinater) + val layerName = when (f.layer) { + FoundStore.LAYER_KLOTTER -> "Klotter" + FoundStore.LAYER_FORNSOK -> "Fornsök" + FoundStore.LAYER_DM -> "DigitaltMuseum" + FoundStore.LAYER_WIKI -> "Wikipedia" + else -> "Plats" + } + val my = LastLocationStore.lastKnownLocation // + val distM = my?.let { app.klottr.util.distanceMeters(it.latitude, it.longitude, f.lat, f.lon).roundToInt() } // + val distText = distM?.let { formatDistance(it) } ?: "-" // + val coordPart = if (showCoords) " • " + formatCoords(f.lat, f.lon) else "" // + val _tick = locationTick // Binda till plats-ticks så raden uppdateras när positionen uppdateras + "$layerName • $distText$coordPart" // + }, + isClickable = { f -> // Samma klick-regel som tidigare + (f.layer == FoundStore.LAYER_WIKI) || + (f.layer == FoundStore.LAYER_FORNSOK) || + (f.layer == FoundStore.LAYER_KLOTTER) || + (f.layer == FoundStore.LAYER_DM) + }, + onEntryClick = { f -> // Samma klickbeteende som tidigare rader + when (f.layer) { + FoundStore.LAYER_KLOTTER -> { + val display = (labelOverrides["${f.layer}|${f.id}"] ?: f.label).ifBlank { "Okänt klotter" } // + openKlotter = OpenKlotter( // + id = f.id, + lat = f.lat, + lon = f.lon, + label = display + ) + } + FoundStore.LAYER_WIKI -> { + val pid = f.id.toLongOrNull() // + if (pid != null) openWiki = OpenWiki(pid, f.lat, f.lon) // + } + FoundStore.LAYER_FORNSOK -> { + scope.launch { // Samma enrich-flöde som tidigare + val enriched = withContext(Dispatchers.IO) { + try { + val around = Fornsok.getNearby(f.lat, f.lon, radiusMeters = 150) + val want = (labelOverrides["${f.layer}|${f.id}"] ?: f.label) + val byTitle = around.firstOrNull { it.title.equals(want, ignoreCase = true) } + byTitle ?: around.minByOrNull { app.klottr.util.distanceMeters(f.lat, f.lon, it.lat, it.lon) } + } catch (_: Exception) { null } } - } - FoundStore.LAYER_DM -> { - // Öppna EXAKT det klickade objektet (inte "något i närheten") - scope.launch { - val exact = withContext(Dispatchers.IO) { - try { - app.klottr.net.DigitaltMuseum.lookupByIdentifier(f.id) - } catch (_: Exception) { null } - } - openDm = exact ?: app.klottr.net.DigitaltMuseumItem( + openFornsok = OpenFornsok( + enriched ?: FornsokItem( id = f.id, - title = displayLabel.ifBlank { "DigitaltMuseum" }, + title = (labelOverrides["${f.layer}|${f.id}"] ?: f.label).ifBlank { "Fornlämning" }, lat = f.lat, lon = f.lon, - url = null, - uniqueId = null, - webUrl = null, - owner = null + uri = if (f.id.startsWith("http")) f.id else null, + serviceName = "fornsok", + desc = null ) - } + ) } } - } else Modifier + FoundStore.LAYER_DM -> { + scope.launch { // Samma DM-lookup som tidigare + val exact = withContext(Dispatchers.IO) { + try { app.klottr.net.DigitaltMuseum.lookupByIdentifier(f.id) } + catch (_: Exception) { null } + } + openDm = exact ?: app.klottr.net.DigitaltMuseumItem( + id = f.id, + title = (labelOverrides["${f.layer}|${f.id}"] ?: f.label).ifBlank { "DigitaltMuseum" }, + lat = f.lat, + lon = f.lon, + url = null, uniqueId = null, webUrl = null, owner = null + ) + } + } + } + }, + isBold = { f -> !FoundStore.isAway(context, f.layer, f.id) }, // Fet stil när posten är “aktiv” + rowTextStyle = MaterialTheme.typography.bodyMedium.copy( // Tydlig text i dag/natt + color = MaterialTheme.colorScheme.onSurface ) - HorizontalDivider() - } + ) } - } Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { if (!loggedIn) Button(onClick = onLogin) { Text("Logga in") }