Move CommonDialogsShell.kt to :shared
This commit is contained in:
@@ -1,369 +0,0 @@
|
||||
package app.klottr.ui
|
||||
|
||||
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
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.widthIn
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.AlertDialog
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TextButton
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
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.Color
|
||||
import androidx.compose.ui.graphics.graphicsLayer
|
||||
import androidx.compose.ui.input.pointer.pointerInput
|
||||
import androidx.compose.ui.unit.Dp
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.window.Dialog
|
||||
import androidx.compose.ui.window.DialogProperties
|
||||
import app.klottr.nearby.FoundOpenAction
|
||||
import app.klottr.nearby.NearbyEntryShared
|
||||
import app.klottr.net.DigitaltMuseumItem
|
||||
import app.klottr.net.FornsokItem
|
||||
import coil3.PlatformContext
|
||||
import coil3.compose.AsyncImage
|
||||
import coil3.compose.LocalPlatformContext
|
||||
import coil3.request.ImageRequest
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.IO
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
/* ---------- Nearby (shared) ---------- */
|
||||
|
||||
@Composable
|
||||
fun NearbyDialog(
|
||||
loading: Boolean,
|
||||
error: String?,
|
||||
lat: Double?,
|
||||
lon: Double?,
|
||||
entries: List<NearbyEntryShared>,
|
||||
dm: List<DigitaltMuseumItem>,
|
||||
forn: List<FornsokItem>,
|
||||
onOpenAction: (FoundOpenAction) -> Unit,
|
||||
onClose: () -> Unit,
|
||||
/** Optional max content height; if null, content becomes scrollable. */
|
||||
maxHeight: Dp? = null
|
||||
) {
|
||||
AlertDialog(
|
||||
onDismissRequest = onClose,
|
||||
title = { Text("Närliggande") },
|
||||
text = {
|
||||
val hasLocation = (lat != null && lon != null)
|
||||
val scroll = rememberScrollState()
|
||||
val container = @Composable { inner: @Composable () -> Unit ->
|
||||
if (maxHeight != null) {
|
||||
Box(Modifier.widthIn(min = 280.dp).height(maxHeight)) {
|
||||
Column(Modifier.fillMaxSize().verticalScroll(scroll)) { inner() }
|
||||
}
|
||||
} else {
|
||||
Column(Modifier.widthIn(min = 280.dp).verticalScroll(scroll)) { inner() }
|
||||
}
|
||||
}
|
||||
//TODO: MaxHeight should be calculated as it was in the original
|
||||
NearbyDialogContent(maxHeight = 580.dp) {
|
||||
container {
|
||||
NearbyContent(
|
||||
loading = loading,
|
||||
error = error,
|
||||
hasLocation = hasLocation,
|
||||
entries = if (hasLocation) entries else emptyList(),
|
||||
dm = dm,
|
||||
forn = forn,
|
||||
onOpenAction = onOpenAction,
|
||||
rowTextStyle = MaterialTheme.typography.bodyMedium.copy(
|
||||
color = MaterialTheme.colorScheme.onSurface
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
confirmButton = { Button(onClick = onClose) { Text("Stäng") } }
|
||||
)
|
||||
}
|
||||
|
||||
/* ---------- Username (shared) ---------- */
|
||||
|
||||
@Composable
|
||||
fun UsernameDialog(
|
||||
onSaved: (String) -> Unit,
|
||||
onLogout: () -> Unit
|
||||
) {
|
||||
val scope = rememberCoroutineScope()
|
||||
|
||||
var input by remember { mutableStateOf("") }
|
||||
var display by remember { mutableStateOf("") }
|
||||
var localErr by remember { mutableStateOf<String?>(null) }
|
||||
var remoteErr by remember { mutableStateOf<String?>(null) }
|
||||
var checking by remember { mutableStateOf(false) }
|
||||
var saving by remember { mutableStateOf(false) }
|
||||
var okRemote by remember { mutableStateOf(false) }
|
||||
|
||||
// Lokal validering speglar servern
|
||||
fun validateLocal(s: String): Boolean {
|
||||
val v = app.klottr.net.usernameLocalValidate(s)
|
||||
display = v.display
|
||||
localErr = when (v.status) {
|
||||
app.klottr.net.UsernameLocalStatus.EMPTY -> "Ange ett användarnamn."
|
||||
app.klottr.net.UsernameLocalStatus.INVALID_CHARS -> "Endast bokstäver, siffror och mellanslag tillåts."
|
||||
app.klottr.net.UsernameLocalStatus.TOO_FEW_LETTERS -> "Minst två bokstäver krävs."
|
||||
app.klottr.net.UsernameLocalStatus.OK -> null
|
||||
}
|
||||
return localErr == null
|
||||
}
|
||||
|
||||
// Debouncead server-check när display ändras
|
||||
LaunchedEffect(display) {
|
||||
remoteErr = null
|
||||
okRemote = false
|
||||
if (display.isBlank()) return@LaunchedEffect
|
||||
if (!validateLocal(display)) return@LaunchedEffect
|
||||
checking = true
|
||||
delay(400) // debounce
|
||||
val resp = withContext(Dispatchers.IO) { app.klottr.net.checkUsernameRemote(display) }
|
||||
checking = false
|
||||
if (resp == null) {
|
||||
remoteErr = "Kunde inte kontrollera namnet (nätfel). Försök igen."
|
||||
okRemote = false
|
||||
} else {
|
||||
if (resp.ok) {
|
||||
okRemote = true
|
||||
remoteErr = null
|
||||
} else {
|
||||
okRemote = false
|
||||
remoteErr = when (resp.reason) {
|
||||
"taken" -> "Upptaget – prova ett annat."
|
||||
"reserved" -> "Reserverat – välj något annat."
|
||||
"invalid" -> "Ogiltigt användarnamn."
|
||||
"too_few_letters" -> "Minst två bokstäver krävs."
|
||||
"invalid_chars" -> "Endast bokstäver, siffror, bindestreck och mellanslag tillåts."
|
||||
"already_set" -> {
|
||||
// Servern säger “du har redan ett namn” – stäng som lyckat
|
||||
remoteErr = null
|
||||
onSaved(display)
|
||||
return@LaunchedEffect
|
||||
}
|
||||
else -> "Ogiltigt användarnamn."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Dialog(
|
||||
onDismissRequest = { /* blockera stängning */ },
|
||||
properties = DialogProperties(
|
||||
dismissOnBackPress = false,
|
||||
dismissOnClickOutside = false,
|
||||
usePlatformDefaultWidth = false
|
||||
)
|
||||
) {
|
||||
Surface(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth(0.92f)
|
||||
.clip(RoundedCornerShape(16.dp)),
|
||||
tonalElevation = 6.dp
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier.padding(20.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp)
|
||||
) {
|
||||
UsernameDialogContent(
|
||||
titleText = "Välj användarnamn",
|
||||
messageText = "Du måste välja ett användarnamn för att fortsätta.",
|
||||
input = input,
|
||||
onInputChange = {
|
||||
input = it
|
||||
validateLocal(it) // Uppdaterar även 'display'
|
||||
},
|
||||
placeholder = "Användarnamn",
|
||||
titleStyle = MaterialTheme.typography.titleLarge.copy(
|
||||
color = MaterialTheme.colorScheme.onSurface
|
||||
),
|
||||
messageStyle = MaterialTheme.typography.bodyMedium.copy(
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant
|
||||
),
|
||||
inputTextStyle = MaterialTheme.typography.bodyMedium.copy(
|
||||
color = MaterialTheme.colorScheme.onSurface
|
||||
),
|
||||
statusText = when {
|
||||
checking && localErr == null && remoteErr == null -> "Kontrollerar …"
|
||||
localErr != null -> localErr
|
||||
remoteErr != null -> remoteErr
|
||||
okRemote && display.isNotBlank() -> "Namnet är ledigt."
|
||||
else -> null
|
||||
},
|
||||
statusStyle = when {
|
||||
localErr != null || remoteErr != null ->
|
||||
MaterialTheme.typography.bodySmall.copy(color = MaterialTheme.colorScheme.error)
|
||||
else ->
|
||||
MaterialTheme.typography.bodySmall.copy(color = MaterialTheme.colorScheme.onSurfaceVariant)
|
||||
},
|
||||
cursorColor = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(12.dp)) {
|
||||
Button(onClick = onLogout) { Text("Logga ut") }
|
||||
Spacer(Modifier.weight(1f))
|
||||
Button(
|
||||
onClick = {
|
||||
if (saving) return@Button
|
||||
if (!validateLocal(input)) return@Button
|
||||
if (!okRemote) return@Button
|
||||
saving = true
|
||||
val disp = app.klottr.net.usernameDisplayClean(input)
|
||||
scope.launch {
|
||||
try {
|
||||
val resp = withContext(Dispatchers.IO) { app.klottr.net.setUsernameRemote(disp) }
|
||||
if (resp?.ok == true && !resp.username.isNullOrBlank()) {
|
||||
onSaved(resp.username!!)
|
||||
} else {
|
||||
val r = resp?.reason
|
||||
remoteErr = when (r) {
|
||||
"taken" -> "Upptaget – prova ett annat."
|
||||
"reserved" -> "Reserverat – välj något annat."
|
||||
"invalid" -> "Ogiltigt användarnamn."
|
||||
"too_few_letters" -> "Minst två bokstäver krävs."
|
||||
"invalid_chars" -> "Endast bokstäver, siffror, bindestreck och mellanslag tillåts."
|
||||
else -> "Kunde inte spara. Försök igen."
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
saving = false
|
||||
}
|
||||
}
|
||||
},
|
||||
enabled = !saving && okRemote && localErr == null
|
||||
) {
|
||||
if (saving) {
|
||||
CircularProgressIndicator(modifier = Modifier.size(18.dp), strokeWidth = 2.dp)
|
||||
} else {
|
||||
Text("Spara")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------- About (shared, platform-fed) ---------- */
|
||||
|
||||
@Composable
|
||||
fun AboutDialog(onDismiss: () -> Unit) {
|
||||
val info = remember { appAboutInfo() } // platform provides
|
||||
AlertDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
properties = DialogProperties(usePlatformDefaultWidth = false),
|
||||
confirmButton = { TextButton(onClick = onDismiss) { Text("Stäng") } },
|
||||
text = {
|
||||
Box(
|
||||
Modifier
|
||||
.fillMaxWidth()
|
||||
.widthIn(max = 520.dp)
|
||||
) {
|
||||
KlottrAboutContent(
|
||||
header = {
|
||||
KlottrLogo(modifier = Modifier.height(48.dp))
|
||||
},
|
||||
lines = listOf(
|
||||
"Version ${info.versionName} (${info.buildType}) • build ${info.buildNumber}",
|
||||
"Byggd ${info.buildTimeUtc}",
|
||||
"Utvecklad av ${info.devBy}.",
|
||||
"IOS-ansvarig: ${info.iosLead}.",
|
||||
"Mejlkontakt: ${info.contactEmail}"
|
||||
),
|
||||
lineStyle = MaterialTheme.typography.bodyMedium.copy(
|
||||
color = MaterialTheme.colorScheme.onSurface
|
||||
),
|
||||
spacingDp = 12
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
/* ---------- Image viewer (shared, byte[] + Coil KMP, pinch/zoom) ---------- */
|
||||
|
||||
@Composable
|
||||
fun ImageViewerDialog(
|
||||
bytes: ByteArray,
|
||||
mimeType: String? = null,
|
||||
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) }
|
||||
|
||||
AsyncImage(
|
||||
model = ImageRequest.Builder(LocalPlatformContext.current)
|
||||
.data(bytes)
|
||||
.build(),
|
||||
contentDescription = null,
|
||||
modifier = 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
|
||||
)
|
||||
)
|
||||
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.align(Alignment.TopEnd)
|
||||
.padding(12.dp)
|
||||
) {
|
||||
Button(onClick = onClose) { Text("Stäng") }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user