Move detail body to shared CMP: add FoundDetailContent and integrate in MainActivity (preserve image click, keep Android actions)

This commit is contained in:
2025-09-18 16:08:20 +02:00
parent fbae61eb16
commit 061beaaa84
2 changed files with 196 additions and 123 deletions

View File

@@ -138,6 +138,7 @@ 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.FoundContent // Delat innehåll för “Hittat”
import app.klottr.ui.FoundDetailContent // Delad detalj-UI (CMP)
import app.klottr.ui.NearbyContent // Delat material-fritt innehåll för Närliggande
import app.klottr.ui.MapPanelShell
import app.klottr.ui.SettingsContent // Delad material-fri UI för inställningar
@@ -2060,136 +2061,67 @@ private fun KlotterDialog(
shadowElevation = 6.dp,
color = MaterialTheme.colorScheme.surface
) {
// --- Lägg här exakt samma innehåll du redan hade i AlertDialog ---
Column(
modifier = Modifier.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(12.dp)
) {
FoundDetailContent(
titleText = titleText,
bodyText = text,
distanceText = distanceText,
wasFoundBefore = wasFoundBefore,
createdText = createdText,
updatedText = updatedText,
authorName = authorName,
showCoords = showCoords,
coordText = if (showCoords) formatCoords(lat, lon) else "",
image = imgBitmap?.asImageBitmap(), // Konvertera till ImageBitmap för delad UI
onImageClick = { showImageViewer = true }, // Behåll bildvisare på klick
loading = loading,
firstVisitText = firstVisitText,
lastVisitText = lastVisitText,
titleStyle = MaterialTheme.typography.titleLarge, // Android ger stil
bodyStyle = MaterialTheme.typography.bodyMedium, // Android ger stil
captionStyle = MaterialTheme.typography.bodySmall.copy( // Android ger stil
fontWeight = FontWeight.Normal
),
// Actions-slot: vi ritar Maps / Radera / Stäng med Android-komponenter
actions = {
val canDelete = currentUserId != null && ownerUserId != null && currentUserId == ownerUserId
// "Klotter • 37 m (ev. koordinater)"
val coordPart = if (showCoords) formatCoords(lat, lon) else ""
val header = if (!wasFoundBefore) {
// Okänt klotter → kort header
"Klotter • $distanceText bort."
} else {
// Besökt tidigare → visa skapad/av men bara när värden finns
buildString {
append("Klotter • $distanceText bort")
if (!createdText.isNullOrBlank()) append(", skapad ").append(createdText)
// Om du redan har variabeln authorName (String?), lämna raden nedan;
// om inte, hoppa över den.
if (!(authorName.isNullOrBlank())) append(" av ").append(authorName)
append(".")
}
}
Text(
header,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
if (updatedText != null && updatedText != createdText) {
Text(
"Senast ändrad: $updatedText",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
if(coordPart != "") {
Text(
"Koordinater: $coordPart",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
HorizontalDivider()
Text(text = titleText, style = MaterialTheme.typography.titleLarge)
// Visa ev. bild oavsett loading (cache kan ha satt den)
if (imgBitmap != null) {
Image(
bitmap = imgBitmap!!.asImageBitmap(),
contentDescription = null,
modifier = Modifier
.fillMaxWidth()
.heightIn(min = 120.dp, max = 320.dp)
.clip(RoundedCornerShape(8.dp))
.clickable { showImageViewer = true },
contentScale = ContentScale.Fit
)
}
// Laddningsindikator som INTE döljer texten
if (loading && text.isBlank() && imgBitmap == null) {
Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp)) {
CircularProgressIndicator(modifier = Modifier.size(16.dp), strokeWidth = 2.dp)
Text("Laddar …")
}
}
// Brödtext alltid synlig (även under loading)
Text(text)
HorizontalDivider()
if (wasFoundBefore) {
Column(verticalArrangement = Arrangement.spacedBy(2.dp)) {
Text(
"Först besökt: $firstVisitText",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
Text(
"Senast besökt: $lastVisitText",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}
// Knappar på EN rad: Maps till vänster, ev. Radera (om ägare), Stäng till höger
val canDelete = currentUserId != null && ownerUserId != null && currentUserId == ownerUserId
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(12.dp)
) {
Button(
onClick = {
val labelEnc = Uri.encode(titleText.ifBlank { "Plats" })
val uri = Uri.parse("geo:$lat,$lon?q=$lat,$lon($labelEnc)")
val intent = Intent(Intent.ACTION_VIEW, uri).apply {
setPackage("com.google.android.apps.maps")
}
try { context.startActivity(intent) } catch (_: Exception) {
context.startActivity(Intent(Intent.ACTION_VIEW, uri))
}
}
) { Text("Maps") }
Spacer(Modifier.weight(1f))
if (canDelete) {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(12.dp)
) {
Button(
enabled = !deleting,
onClick = { askDelete = true }
) {
if (deleting) {
CircularProgressIndicator(modifier = Modifier.size(18.dp), strokeWidth = 2.dp)
} else {
Text("Radera")
onClick = {
val labelEnc = Uri.encode(titleText.ifBlank { "Plats" })
val uri = Uri.parse("geo:$lat,$lon?q=$lat,$lon($labelEnc)")
val intent = Intent(Intent.ACTION_VIEW, uri).apply {
setPackage("com.google.android.apps.maps")
}
try { context.startActivity(intent) } catch (_: Exception) {
context.startActivity(Intent(Intent.ACTION_VIEW, uri))
}
}
) { Text("Maps") }
Spacer(Modifier.weight(1f))
if (canDelete) {
Button(
enabled = !deleting,
onClick = { askDelete = true }
) {
if (deleting) {
CircularProgressIndicator(modifier = Modifier.size(18.dp), strokeWidth = 2.dp)
} else {
Text("Radera")
}
}
}
Button(onClick = onClose) { Text("Stäng") }
}
Button(onClick = onClose) { Text("Stäng") }
}
)
}
// --- Slut på innehåll ---
}
}
if (showImageViewer && imgBitmap != null) {