Move ImageViewerDialog to CommonDialogsShell; use existing UI shells from app.klottr.ui

Remove unused local FornsokDialog/DmDialog/fetchBitmapFromUrl duplicates from MainActivity
This commit is contained in:
2025-09-20 01:34:08 +02:00
parent 21c05cfad4
commit e0240f44b5
2 changed files with 64 additions and 237 deletions

View File

@@ -124,6 +124,7 @@ import app.klottr.ui.DmDialog
import app.klottr.ui.FornsokDetailContent // Ny: delad Fornsök-layout
import app.klottr.ui.FornsokDialog
import app.klottr.ui.FoundDetailContent // Delad detalj-UI (CMP)
import app.klottr.ui.ImageViewerDialog
import app.klottr.ui.KlottrHomeContent // Ny: delat mittinnehåll
import app.klottr.ui.LoginRequiredContent
import app.klottr.ui.NearbyDialog
@@ -1798,234 +1799,7 @@ private fun KlotterDialog(
}
@OptIn(ExperimentalFoundationApi::class)
@Composable
private fun ImageViewerDialog(bitmap: Bitmap, onClose: () -> Unit) {
Dialog(
onDismissRequest = onClose,
properties = DialogProperties(usePlatformDefaultWidth = false)
) {
Box(
modifier = Modifier
.fillMaxSize()
.background(Color.Black)
) {
var scale by remember { mutableStateOf(1f) }
var offset by remember { mutableStateOf(Offset.Zero) }
val state = rememberTransformableState { zoomChange, panChange, _ ->
val newScale = (scale * zoomChange).coerceIn(1f, 6f)
val scaledPan = panChange * (newScale / scale)
scale = newScale
offset += scaledPan
}
Image(
bitmap = bitmap.asImageBitmap(),
contentDescription = null,
modifier = Modifier
.fillMaxSize()
.graphicsLayer(
scaleX = scale,
scaleY = scale,
translationX = offset.x,
translationY = offset.y
)
.transformable(state),
contentScale = ContentScale.Fit
)
Box(
modifier = Modifier
.align(Alignment.TopEnd)
.padding(12.dp)
) {
Button(onClick = onClose) { Text("Stäng") }
}
}
}
}
@Composable
private fun FornsokDialog(
item: FornsokItem,
onClose: () -> Unit
) {
val context = LocalContext.current
val configuration = LocalConfiguration.current
val dialogWidth = (configuration.screenWidthDp.dp * 0.98f)
// Vi använder fälten från FornsokItem; om desc/uri saknas visar vi "saknas"-text.
val title = remember(item.id) { item.title.ifBlank { "Fornlämning" } }
val desc = remember(item.id) { item.desc ?: "" }
val link = remember(item.id) { item.uri ?: (if (item.id.startsWith("http")) item.id else null) }
val platLat = item.lat
val platLon = item.lon
// Avståndstext som uppdateras på plats-tick
var fornDistanceText by remember { mutableStateOf("-") }
val showCoords = remember { app.klottr.util.SettingsStore.getShowCoordinates() }
fun recalcFornDistance() {
val my = app.klottr.util.LastLocationStore.lastKnownLocation
val d = if (my != null)
app.klottr.util.distanceMeters(my.latitude, my.longitude, platLat, platLon).roundToInt()
else null
fornDistanceText = d?.let { "$it\u00A0m" } ?: "-"
}
// Beräkna direkt
LaunchedEffect(platLat, platLon) { recalcFornDistance() }
// Lyssna på plats-tick
DisposableEffect(Unit) {
val filter = IntentFilter(LocationService.ACTION_LOCATION_TICK)
val receiver = object : BroadcastReceiver() {
override fun onReceive(ctx: Context?, intent: Intent?) { recalcFornDistance() }
}
if (Build.VERSION.SDK_INT >= 33) {
context.registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED)
} else {
@Suppress("DEPRECATION")
context.registerReceiver(receiver, filter)
}
onDispose { try { context.unregisterReceiver(receiver) } catch (_: Exception) {} }
}
AlertDialog(
onDismissRequest = onClose,
properties = DialogProperties(usePlatformDefaultWidth = false),
title = {
Box(Modifier.fillMaxWidth().widthIn(max = dialogWidth)) {
Text(title)
}
},
text = {
Box(
Modifier
.fillMaxWidth()
.widthIn(max = dialogWidth)
.heightIn(max = 520.dp)
) {
FornsokDetailContent(
titleText = title,
descText = desc,
distanceText = fornDistanceText,
coordText = if (showCoords) formatCoords(platLat, platLon) else null,
titleStyle = MaterialTheme.typography.titleLarge.copy(
color = MaterialTheme.colorScheme.onSurface
),
bodyStyle = MaterialTheme.typography.bodyMedium.copy(
color = MaterialTheme.colorScheme.onSurface
),
captionStyle = MaterialTheme.typography.bodySmall.copy(
color = MaterialTheme.colorScheme.onSurfaceVariant
),
actions = {
Button(
onClick = {
val geoTitle = Uri.encode(title)
val geo = Uri.parse("geo:$platLat,$platLon?q=$platLat,$platLon($geoTitle)")
val intent = Intent(Intent.ACTION_VIEW, geo).apply { setPackage("com.google.android.apps.maps") }
try { context.startActivity(intent) } catch (_: Exception) {
context.startActivity(Intent(Intent.ACTION_VIEW, geo))
}
}
) { Text("Maps") }
Button(
onClick = {
val u = link ?: return@Button
context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(u)))
},
enabled = link != null
) { Text("Läs mer") }
}
)
}
},
confirmButton = { Button(onClick = onClose) { Text("Stäng") } }
)
}
@Composable
private fun DmDialog(
item: DigitaltMuseumItem,
onClose: () -> Unit
) {
val context = LocalContext.current
val configuration = LocalConfiguration.current
val dialogWidth = (configuration.screenWidthDp.dp * 0.98f)
var detailText by remember { mutableStateOf<String?>(null) }
var loading by remember { mutableStateOf(false) }
var thumb: Bitmap? by remember { mutableStateOf(null) }
// Avstånd som tickar
var dmDistanceText by remember { mutableStateOf("-") }
val showCoords = remember { SettingsStore.getShowCoordinates() }
fun recalcDmDistance() {
val my = LastLocationStore.lastKnownLocation
val d = if (my != null) app.klottr.util.distanceMeters(
my.latitude,
my.longitude,
item.lat,
item.lon
).roundToInt() else null
dmDistanceText = d?.let { formatDistance(it) } ?: "-"
}
LaunchedEffect(item.lat, item.lon) { recalcDmDistance() }
DisposableEffect(Unit) {
val filter = IntentFilter(LocationService.ACTION_LOCATION_TICK)
val receiver = object : BroadcastReceiver() {
override fun onReceive(ctx: Context?, intent: Intent?) {
recalcDmDistance()
}
}
if (Build.VERSION.SDK_INT >= 33) {
context.registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED)
} else {
@Suppress("DEPRECATION")
context.registerReceiver(receiver, filter)
}
onDispose {
try {
context.unregisterReceiver(receiver)
} catch (_: Exception) {
}
}
}
// Hämta miniatyr om vi har bild-URL
LaunchedEffect(item.id) {
// Bild (om finns)
if (!item.url.isNullOrBlank()) {
loading = true
thumb = withContext(Dispatchers.IO) { fetchBitmapFromUrl(item.url!!) }
loading = false
} else {
thumb = null
}
// Text (om uniqueId finns)
detailText = withContext(Dispatchers.IO) {
app.klottr.net.DigitaltMuseum.getDetailText(
uniqueId = item.uniqueId,
owner = item.owner,
identifierId = item.id
)
}
}
}
/* ---------- Hjälp ---------- */
fun hasFgLocationPerms(ctx: Context): Boolean {
val fine = ContextCompat.checkSelfPermission(ctx, Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED
@@ -2040,13 +1814,3 @@ fun hasBgLocationPerm(ctx: Context): Boolean {
ctx, Manifest.permission.ACCESS_BACKGROUND_LOCATION
) == PackageManager.PERMISSION_GRANTED
}
private fun fetchBitmapFromUrl(url: String): Bitmap? {
return try {
val conn = java.net.URL(url).openConnection() as java.net.HttpURLConnection
conn.connectTimeout = 6000
conn.readTimeout = 8000
conn.instanceFollowRedirects = true
conn.inputStream.use { stream -> BitmapFactory.decodeStream(stream) }
} catch (_: Exception) { null }
}

View File

@@ -3,11 +3,14 @@ package app.klottr.ui
import android.content.pm.PackageManager
import android.os.Build
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectTransformGestures
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
@@ -28,8 +31,14 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.clipToBounds
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
@@ -318,3 +327,57 @@ fun AboutDialog(onDismiss: () -> Unit) {
}
)
}
// Ny: enkel helskärms bildvisare med pinch-zoom/pan
@Composable
fun ImageViewerDialog(
bitmap: android.graphics.Bitmap,
onClose: () -> Unit
) {
androidx.compose.ui.window.Dialog(
onDismissRequest = onClose,
properties = androidx.compose.ui.window.DialogProperties(usePlatformDefaultWidth = false)
) {
androidx.compose.foundation.layout.Box(
modifier = androidx.compose.ui.Modifier
.fillMaxSize()
.background(androidx.compose.ui.graphics.Color.Black)
) {
var scale by remember { mutableStateOf(1f) }
var offset by remember { mutableStateOf(Offset.Zero) }
androidx.compose.foundation.Image(
bitmap = bitmap.asImageBitmap(),
contentDescription = null,
modifier = androidx.compose.ui.Modifier
.fillMaxSize()
.clipToBounds()
.pointerInput(Unit) {
detectTransformGestures { _, pan, zoom, _ ->
scale = (scale * zoom).coerceIn(0.5f, 6f)
offset += pan
val clamp = 2000f
offset = Offset(
x = offset.x.coerceIn(-clamp, clamp),
y = offset.y.coerceIn(-clamp, clamp)
)
}
}
.graphicsLayer(
scaleX = scale,
scaleY = scale,
translationX = offset.x,
translationY = offset.y
)
)
androidx.compose.foundation.layout.Box(
modifier = androidx.compose.ui.Modifier
.align(Alignment.TopEnd)
.padding(12.dp)
) {
Button(onClick = onClose) { Text("Stäng") }
}
}
}
}