shared(ui): add WriteDocContent; app(write): integrate and fix focus/placeholder/cursor; outbox: persist image using captured URI
This commit is contained in:
164
shared/src/commonMain/kotlin/app/klottr/ui/WriteDocContent.kt
Normal file
164
shared/src/commonMain/kotlin/app/klottr/ui/WriteDocContent.kt
Normal file
@@ -0,0 +1,164 @@
|
||||
package app.klottr.ui
|
||||
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.clickable
|
||||
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.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.heightIn
|
||||
import androidx.compose.foundation.layout.imePadding
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.text.BasicText
|
||||
import androidx.compose.foundation.text.BasicTextField
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.remember
|
||||
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.focus.FocusRequester
|
||||
import androidx.compose.ui.focus.focusRequester
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.ImageBitmap
|
||||
import androidx.compose.ui.graphics.SolidColor // För tydlig cursor-färg
|
||||
import androidx.compose.ui.graphics.takeOrElse
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.text.TextStyle
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Material-fritt formulär för att skriva ett dokument (rubrik, text, valfri bild).
|
||||
* All plattformslogik (bildväljare, fil-IO) hanteras av anroparen via callbacks.
|
||||
*/
|
||||
@Composable
|
||||
fun WriteDocContent(
|
||||
title: String,
|
||||
onTitleChange: (String) -> Unit,
|
||||
titleError: String?,
|
||||
body: String,
|
||||
onBodyChange: (String) -> Unit,
|
||||
hasImage: Boolean,
|
||||
imagePreview: ImageBitmap?,
|
||||
onPickImage: () -> Unit,
|
||||
onRemoveImage: () -> Unit,
|
||||
rowTextStyle: TextStyle,
|
||||
modifier: Modifier = Modifier
|
||||
) {
|
||||
val base = rowTextStyle.color.takeOrElse { Color(0xFFDDDDDD) }
|
||||
val dim = base.copy(alpha = 0.65f)
|
||||
val hint = base.copy(alpha = 0.55f)
|
||||
val border = base.copy(alpha = 0.40f)
|
||||
val error = Color(0xFFB3261E)
|
||||
val shape = RoundedCornerShape(8.dp)
|
||||
val titleFocus = remember { FocusRequester() }
|
||||
val bodyFocus = remember { FocusRequester() }
|
||||
|
||||
|
||||
Column(
|
||||
modifier = modifier
|
||||
.verticalScroll(rememberScrollState())
|
||||
.imePadding(),
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp)
|
||||
) {
|
||||
// Rubrik
|
||||
BasicText("Rubrik", style = rowTextStyle.copy(fontWeight = FontWeight.Medium))
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.border(1.dp, if (titleError == null) border else error, shape)
|
||||
.clickable { titleFocus.requestFocus() }
|
||||
.padding(horizontal = 12.dp, vertical = 10.dp)
|
||||
) {
|
||||
if (title.isBlank()) {
|
||||
BasicText("Skriv en rubrik…", style = rowTextStyle.copy(color = hint))
|
||||
}
|
||||
BasicTextField(
|
||||
value = title,
|
||||
onValueChange = onTitleChange,
|
||||
textStyle = rowTextStyle,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.focusRequester(titleFocus),
|
||||
cursorBrush = SolidColor(rowTextStyle.color.takeOrElse { Color.White })
|
||||
)
|
||||
}
|
||||
if (titleError != null) {
|
||||
BasicText(titleError, style = rowTextStyle.copy(color = error))
|
||||
}
|
||||
|
||||
// Bildförhandsvisning (om någon)
|
||||
if (imagePreview != null) {
|
||||
Image(
|
||||
bitmap = imagePreview,
|
||||
contentDescription = null,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.heightIn(min = 120.dp, max = 240.dp)
|
||||
.border(1.dp, border, shape),
|
||||
contentScale = ContentScale.Fit
|
||||
)
|
||||
}
|
||||
|
||||
// Bild-knappar (material-fria)
|
||||
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.border(1.dp, border, shape)
|
||||
.background(Color.Transparent, shape)
|
||||
.clickable(onClick = onPickImage)
|
||||
.padding(horizontal = 12.dp, vertical = 8.dp),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
BasicText(if (hasImage) "Byt bild" else "Välj bild", style = rowTextStyle)
|
||||
}
|
||||
if (hasImage) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.border(1.dp, border, shape)
|
||||
.clickable(onClick = onRemoveImage)
|
||||
.padding(horizontal = 12.dp, vertical = 8.dp),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
BasicText("Ta bort bild", style = rowTextStyle)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Brödtext
|
||||
BasicText("Text", style = rowTextStyle.copy(color = dim, fontWeight = FontWeight.Medium))
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.heightIn(min = 120.dp)
|
||||
.border(1.dp, border, shape)
|
||||
.padding(horizontal = 12.dp, vertical = 10.dp)
|
||||
) {
|
||||
if (body.isBlank()) {
|
||||
BasicText("Din text…", style = rowTextStyle.copy(color = hint))
|
||||
}
|
||||
BasicTextField(
|
||||
value = body,
|
||||
onValueChange = onBodyChange,
|
||||
textStyle = rowTextStyle,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.matchParentSize(), // Klick var som helst i boxen ger fokus
|
||||
cursorBrush = SolidColor(rowTextStyle.color.takeOrElse { // Synlig cursor i mörkt läge
|
||||
Color.White
|
||||
} )
|
||||
)
|
||||
}
|
||||
Spacer(Modifier.width(1.dp)) // Liten spacer i botten för scroll-känsla
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user