diff --git a/app/src/main/java/app/klottr/MainActivity.kt b/app/src/main/java/app/klottr/MainActivity.kt index 5c3336a..2c77fc5 100644 --- a/app/src/main/java/app/klottr/MainActivity.kt +++ b/app/src/main/java/app/klottr/MainActivity.kt @@ -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.NearbyEntryRow import app.klottr.ui.NearbyList // Delad material-fri listkomponent +import app.klottr.ui.MapPanelShell import app.klottr.util.Auth import app.klottr.util.Config import app.klottr.util.FoundStore @@ -720,169 +721,143 @@ private fun KlottrApp( 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 - Column(Modifier.padding(horizontal = 4.dp, vertical = 8.dp)) { // Tar bort ProvideTextStyle-wrappern (onödig nu) - Text( - "Senaste", - style = MaterialTheme.typography.titleLarge, - color = MaterialTheme.colorScheme.onSurface //Sätt färg direkt så rubriken är tydlig i mörkt läge - ) - Spacer(Modifier.height(6.dp)) - } + MapPanelShell( + modifier = Modifier + .fillMaxWidth() + .weight(1f), + title = "Senaste", + titleTextStyle = MaterialTheme.typography.titleLarge.copy( + color = MaterialTheme.colorScheme.onSurface + ), + mainContent = { + // Hittat (flyttat hit för att kapslas i skalet) + val foundList = + if (forceEmptyFound) emptyList() + else FoundStore.getAll(context) - // Hittat - val foundList = - if (forceEmptyFound) emptyList() - else FoundStore.getAll(context) - val labelOverrides = remember { androidx.compose.runtime.mutableStateMapOf() } // FQN så rätt top-level-funktion används trots import-trassel + val labelOverrides = remember { androidx.compose.runtime.mutableStateMapOf() } - val foundKey = foundList.joinToString("|") { "${it.layer}:${it.id}" } // Stabil nyckel som inte ändras vid tema-byte - // 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 my = LastLocationStore.lastKnownLocation - val myLat = my?.latitude - val myLon = my?.longitude - labelOverrides.clear() // Nollställ vid nytt innehåll (ny foundKey) - foundList.forEach { f -> - if (f.layer == LAYER_KLOTTER) { - val inside = if (myLat != null && myLon != null) - app.klottr.util.distanceMeters(myLat, myLon, f.lat, f.lon) <= Config.DEFAULT_KLOTTER_RADIUS_M - else true - if (inside) { - val idL = f.id.toLongOrNull() ?: return@forEach - 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) - if (detail != null) { - NoteDetailCache.put(idL, detail) - val img = detail.image - if (img != null && img.id > 0 && ImageBitmapCache.get(img.id) == null) { - withContext(Dispatchers.IO) { - fetchImageStreamPost(img.id, detail.id, f.lat, f.lon)?.let { bytes -> - val bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.size) - if (bmp != null) { - ImageBitmapCache.put(img.id, bmp) + 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 myLat = my?.latitude + val myLon = my?.longitude + labelOverrides.clear() + foundList.forEach { f -> + if (f.layer == LAYER_KLOTTER) { + val inside = if (myLat != null && myLon != null) + app.klottr.util.distanceMeters(myLat, myLon, f.lat, f.lon) <= Config.DEFAULT_KLOTTER_RADIUS_M + else true + if (inside) { + val idL = f.id.toLongOrNull() ?: return@forEach + if (NoteDetailCache.get(idL) == null) { + val detail = fetchNoteDetail(idL, f.lat, f.lon) + if (detail != null) { + NoteDetailCache.put(idL, detail) + val img = detail.image + if (img != null && img.id > 0 && ImageBitmapCache.get(img.id) == null) { + withContext(Dispatchers.IO) { + fetchImageStreamPost(img.id, detail.id, f.lat, f.lon)?.let { bytes -> + val bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.size) + if (bmp != null) { + ImageBitmapCache.put(img.id, bmp) + } + } } } } } } + val enriched = withContext(Dispatchers.IO) { + enrichKlotterLabels( + found = foundList, + radiusMeters = 60, + fetchNearbyKlotter = { la, lo, r -> + Geocache.getNearby(lat = la, lon = lo, radiusMeters = r, ttlMs = 0L) + } + ) + } + for ((k, v) in enriched) { + labelOverrides[k] = v + } } } - // 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) { - enrichKlotterLabels( // - found = foundList, - radiusMeters = 60, - fetchNearbyKlotter = { la, lo, r -> - Geocache.getNearby(lat = la, lon = lo, radiusMeters = r, ttlMs = 0L) - } - ) - } - // Uppdatera overrides-kartan i en batch - for ((k, v) in enriched) { - labelOverrides[k] = v - } } - } - } - if (foundList.isEmpty()) { - Text("Inga fynd ännu.") - } else { - Box( - modifier = Modifier - .fillMaxWidth() - .weight(1f) - ) { - val my = LastLocationStore.lastKnownLocation - val _tick = locationTick + if (foundList.isEmpty()) { + Text("Inga fynd ännu.") + } else { + val my = LastLocationStore.lastKnownLocation + val _tick = locationTick - Box(modifier = Modifier.fillMaxSize()) { // Gör ytan fullhöjd för innehållet - FoundContent( // Ingen modifier här - 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_KLOTTER) || - (f.layer == FoundStore.LAYER_DM) - }, - onEntryClick = { f -> // Flytta logiken till delad resolver - scope.launch { // kör som korutin på huvudtråden - val action = withContext(Dispatchers.IO) { // nätverk/IO utanför UI-tråd - resolveFoundOpenAction( // Gemensam logik i :shared - entry = f, // - labelOverrides = labelOverrides, // Använd förbättrade etiketter - fornsokAround = { lat, lon, radius -> // DI: Fornsök i närheten - try { Fornsok.getNearby(lat, lon, radius) } catch (_: Exception) { emptyList() } - }, - dmLookupByIdentifier = { id -> // DI: DigitaltMuseum-uppslag - try { app.klottr.net.DigitaltMuseum.lookupByIdentifier(id) } catch (_: Exception) { null } - } - ) - } - when (action) { // Mappa delad åtgärd till plattformsnavigation - is FoundOpenAction.Klotter -> { - openKlotter = OpenKlotter( // - id = action.id, // - lat = action.lat, // - lon = action.lon, // - label = action.label // + Box(modifier = Modifier.fillMaxSize()) { + FoundContent( + 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_KLOTTER) || + (f.layer == FoundStore.LAYER_DM) + }, + onEntryClick = { f -> + scope.launch { + val action = withContext(Dispatchers.IO) { + resolveFoundOpenAction( + entry = f, + labelOverrides = labelOverrides, + fornsokAround = { lat, lon, radius -> + try { Fornsok.getNearby(lat, lon, radius) } catch (_: Exception) { emptyList() } + }, + dmLookupByIdentifier = { id -> + try { app.klottr.net.DigitaltMuseum.lookupByIdentifier(id) } catch (_: Exception) { null } + } ) } - is FoundOpenAction.Wiki -> { - openWiki = OpenWiki(action.pageId, action.lat, action.lon) // + when (action) { + is FoundOpenAction.Klotter -> { + openKlotter = OpenKlotter( + id = action.id, + lat = action.lat, + lon = action.lon, + label = action.label + ) + } + is FoundOpenAction.Wiki -> { + openWiki = OpenWiki(action.pageId, action.lat, action.lon) + } + is FoundOpenAction.Fornsok -> { + openFornsok = OpenFornsok(action.item) + } + is FoundOpenAction.DigitaltMuseum -> { + openDm = action.item + } + FoundOpenAction.None -> Unit } - is FoundOpenAction.Fornsok -> { - openFornsok = OpenFornsok(action.item) // - } - is FoundOpenAction.DigitaltMuseum -> { - openDm = action.item // - } - FoundOpenAction.None -> { /* no-op */ } // } - } - }, // [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 - ) - ) - } // - - // 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) - ) + }, + isBold = { f -> !FoundStore.isAway(context, f.layer, f.id) }, + distanceFormatter = ::formatDistance, + coordsFormatter = ::formatCoords, + rowTextStyle = MaterialTheme.typography.bodyMedium.copy( + color = MaterialTheme.colorScheme.onSurface ) ) - ) - 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)) { + if (!loggedIn) Button(onClick = onLogin) { Text("Logga in") } + if (loggedIn) Button(onClick = onLogout) { Text("Logga ut") } + } } - - } - Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) { - if (!loggedIn) Button(onClick = onLogin) { Text("Logga in") } - if (loggedIn) Button(onClick = onLogout) { Text("Logga ut") } - } + ) } } // Om-appen @@ -1245,15 +1220,13 @@ private fun NearbyDialog( AlertDialog( onDismissRequest = onClose, title = { Text("Närliggande") }, - text = { // Använd delat dialoginnehåll - val hasLocation = (lat != null && lon != null) // Flagga för plats - // Kända klotteretiketter (cache) — O(1)-lookup + text = { + val hasLocation = (lat != null && lon != null) val knownKlotterLabels = FoundStore.getAll(ctx) .asSequence() .filter { it.layer == FoundStore.LAYER_KLOTTER } .associate { it.id to it.label } - // Bygg delade entries om vi har plats, annars tom lista val entries: List = if (hasLocation) { buildNearbyEntries( @@ -1271,7 +1244,6 @@ private fun NearbyDialog( ) } else emptyList() - // Markera “hittade” innan render (endast när vi inte laddar/har fel och har plats) if (hasLocation && !loading && error == null) { app.klottr.nearby.markNewFinds( ctx = ctx, @@ -1281,21 +1253,35 @@ private fun NearbyDialog( ) } - // Rendera material-fritt delat innehåll (hanterar själv loading/fel/tom-lista) - NearbyContent( - loading = loading, - error = error, - hasLocation = hasLocation, - entries = entries, - dm = dm, - forn = forn, - onWikiClick = onWikiClick, - onDmClick = onDmClick, - onFornsokClick = onFornsokClick, - onKlotterClick = onKlotterClick, - rowTextStyle = MaterialTheme.typography.bodyMedium.copy( // Säker textfärg i dag/natt + // 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( + loading = loading, + error = error, + hasLocation = hasLocation, + entries = entries, + dm = dm, + forn = forn, + onWikiClick = onWikiClick, + onDmClick = onDmClick, + onFornsokClick = onFornsokClick, + onKlotterClick = onKlotterClick, + rowTextStyle = MaterialTheme.typography.bodyMedium.copy( + color = MaterialTheme.colorScheme.onSurface + ) + ) + } ) }, confirmButton = { Button(onClick = onClose) { Text("Stäng") } } diff --git a/shared/src/commonMain/kotlin/app/klottr/ui/MapPanelShell.kt b/shared/src/commonMain/kotlin/app/klottr/ui/MapPanelShell.kt new file mode 100644 index 0000000..596dd63 --- /dev/null +++ b/shared/src/commonMain/kotlin/app/klottr/ui/MapPanelShell.kt @@ -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() + } + } + } +}