mirror of
https://github.com/bleackstorm/satiea.git
synced 2026-12-03 07:41:56 +01:00
497 lines
21 KiB
Kotlin
497 lines
21 KiB
Kotlin
package com.example.ui.planning
|
|
|
|
import androidx.compose.foundation.BorderStroke
|
|
import androidx.compose.foundation.layout.Arrangement
|
|
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.height
|
|
import androidx.compose.foundation.layout.heightIn
|
|
import androidx.compose.foundation.layout.imePadding
|
|
import androidx.compose.foundation.layout.navigationBarsPadding
|
|
import androidx.compose.foundation.layout.padding
|
|
import androidx.compose.foundation.layout.size
|
|
import androidx.compose.foundation.layout.width
|
|
import androidx.compose.foundation.lazy.LazyColumn
|
|
import androidx.compose.foundation.lazy.items
|
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
|
import androidx.compose.foundation.text.KeyboardOptions
|
|
import androidx.compose.material.icons.Icons
|
|
import androidx.compose.material.icons.automirrored.filled.ReceiptLong
|
|
import androidx.compose.material.icons.filled.Add
|
|
import androidx.compose.material.icons.filled.CalendarMonth
|
|
import androidx.compose.material.icons.filled.Close
|
|
import androidx.compose.material.icons.filled.DeleteOutline
|
|
import androidx.compose.material.icons.filled.ErrorOutline
|
|
import androidx.compose.material3.AssistChip
|
|
import androidx.compose.material3.Button
|
|
import androidx.compose.material3.Card
|
|
import androidx.compose.material3.CardDefaults
|
|
import androidx.compose.material3.Checkbox
|
|
import androidx.compose.material3.CircularProgressIndicator
|
|
import androidx.compose.material3.DatePicker
|
|
import androidx.compose.material3.DatePickerDialog
|
|
import androidx.compose.material3.ExperimentalMaterial3Api
|
|
import androidx.compose.material3.FilterChip
|
|
import androidx.compose.material3.Icon
|
|
import androidx.compose.material3.IconButton
|
|
import androidx.compose.material3.MaterialTheme
|
|
import androidx.compose.material3.ModalBottomSheet
|
|
import androidx.compose.material3.OutlinedButton
|
|
import androidx.compose.material3.OutlinedTextField
|
|
import androidx.compose.material3.Surface
|
|
import androidx.compose.material3.Switch
|
|
import androidx.compose.material3.Text
|
|
import androidx.compose.material3.TextButton
|
|
import androidx.compose.material3.rememberDatePickerState
|
|
import androidx.compose.material3.rememberModalBottomSheetState
|
|
import androidx.compose.runtime.Composable
|
|
import androidx.compose.runtime.getValue
|
|
import androidx.compose.runtime.mutableStateOf
|
|
import androidx.compose.runtime.remember
|
|
import androidx.compose.runtime.setValue
|
|
import androidx.compose.ui.Alignment
|
|
import androidx.compose.ui.Modifier
|
|
import androidx.compose.ui.platform.testTag
|
|
import androidx.compose.ui.text.font.FontWeight
|
|
import androidx.compose.ui.text.input.KeyboardType
|
|
import androidx.compose.ui.text.style.TextAlign
|
|
import androidx.compose.ui.text.style.TextOverflow
|
|
import androidx.compose.ui.unit.dp
|
|
import com.example.data.model.FoodUnit
|
|
import com.example.data.model.ReceiptScanUiState
|
|
import com.example.data.model.ScannedReceiptItem
|
|
import com.example.data.model.StorageLocation
|
|
import com.example.ui.common.FoodCategorySelectorRow
|
|
import java.time.Instant
|
|
import java.time.LocalDate
|
|
import java.time.ZoneOffset
|
|
import java.time.format.DateTimeFormatter
|
|
import java.util.Locale
|
|
|
|
@OptIn(ExperimentalMaterial3Api::class)
|
|
@Composable
|
|
fun ReceiptReviewSheet(
|
|
scanState: ReceiptScanUiState,
|
|
onUpdateItem: (ScannedReceiptItem) -> Unit,
|
|
onRemoveItem: (String) -> Unit,
|
|
onAddBlankItem: () -> Unit,
|
|
onConfirmImport: (items: List<ScannedReceiptItem>, alsoCheckShoppingList: Boolean) -> Unit,
|
|
onRetryScan: () -> Unit,
|
|
onDismiss: () -> Unit
|
|
) {
|
|
if (scanState is ReceiptScanUiState.Idle) return
|
|
|
|
val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)
|
|
var alsoCheckShoppingList by remember { mutableStateOf(true) }
|
|
|
|
ModalBottomSheet(
|
|
onDismissRequest = onDismiss,
|
|
sheetState = sheetState,
|
|
shape = RoundedCornerShape(topStart = 24.dp, topEnd = 24.dp)
|
|
) {
|
|
Column(
|
|
modifier = Modifier
|
|
.fillMaxWidth()
|
|
.navigationBarsPadding()
|
|
.imePadding()
|
|
.padding(horizontal = 20.dp, vertical = 8.dp),
|
|
verticalArrangement = Arrangement.spacedBy(12.dp)
|
|
) {
|
|
when (scanState) {
|
|
ReceiptScanUiState.Idle -> Unit
|
|
|
|
ReceiptScanUiState.Scanning -> {
|
|
Column(
|
|
modifier = Modifier
|
|
.fillMaxWidth()
|
|
.padding(vertical = 32.dp)
|
|
.testTag("receipt_scanning_indicator"),
|
|
horizontalAlignment = Alignment.CenterHorizontally,
|
|
verticalArrangement = Arrangement.spacedBy(14.dp)
|
|
) {
|
|
CircularProgressIndicator()
|
|
Text(
|
|
text = "Lecture de votre ticket en cours...",
|
|
style = MaterialTheme.typography.titleMedium,
|
|
fontWeight = FontWeight.Bold,
|
|
textAlign = TextAlign.Center
|
|
)
|
|
Text(
|
|
text = "Identification des aliments, quantités, catégories et emplacements de conservation.",
|
|
style = MaterialTheme.typography.bodySmall,
|
|
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
|
textAlign = TextAlign.Center
|
|
)
|
|
}
|
|
}
|
|
|
|
is ReceiptScanUiState.Error -> {
|
|
Column(
|
|
modifier = Modifier
|
|
.fillMaxWidth()
|
|
.padding(vertical = 20.dp)
|
|
.testTag("receipt_scan_error"),
|
|
horizontalAlignment = Alignment.CenterHorizontally,
|
|
verticalArrangement = Arrangement.spacedBy(12.dp)
|
|
) {
|
|
Icon(
|
|
imageVector = Icons.Default.ErrorOutline,
|
|
contentDescription = null,
|
|
tint = MaterialTheme.colorScheme.error,
|
|
modifier = Modifier.size(40.dp)
|
|
)
|
|
Text(
|
|
text = "Analyse impossible",
|
|
style = MaterialTheme.typography.titleMedium,
|
|
fontWeight = FontWeight.Bold
|
|
)
|
|
Text(
|
|
text = scanState.message,
|
|
style = MaterialTheme.typography.bodyMedium,
|
|
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
|
textAlign = TextAlign.Center
|
|
)
|
|
Row(
|
|
modifier = Modifier.fillMaxWidth(),
|
|
horizontalArrangement = Arrangement.spacedBy(10.dp)
|
|
) {
|
|
OutlinedButton(
|
|
onClick = onDismiss,
|
|
modifier = Modifier.weight(1f)
|
|
) {
|
|
Text("Fermer")
|
|
}
|
|
Button(
|
|
onClick = onRetryScan,
|
|
modifier = Modifier.weight(1f)
|
|
) {
|
|
Text("Réessayer")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
is ReceiptScanUiState.Preview -> {
|
|
val selectedValidCount = scanState.items.count {
|
|
it.selected && it.name.isNotBlank() && it.quantity > 0.0
|
|
}
|
|
|
|
Row(
|
|
modifier = Modifier.fillMaxWidth(),
|
|
verticalAlignment = Alignment.CenterVertically,
|
|
horizontalArrangement = Arrangement.SpaceBetween
|
|
) {
|
|
Row(
|
|
verticalAlignment = Alignment.CenterVertically,
|
|
modifier = Modifier.weight(1f)
|
|
) {
|
|
Icon(
|
|
imageVector = Icons.AutoMirrored.Filled.ReceiptLong,
|
|
contentDescription = null,
|
|
tint = MaterialTheme.colorScheme.primary
|
|
)
|
|
Spacer(modifier = Modifier.width(8.dp))
|
|
Column {
|
|
Text(
|
|
text = "Vérification du ticket",
|
|
style = MaterialTheme.typography.titleLarge,
|
|
fontWeight = FontWeight.Bold
|
|
)
|
|
Text(
|
|
text = "${scanState.items.size} aliment${if (scanState.items.size > 1) "s" else ""} détecté${if (scanState.items.size > 1) "s" else ""} • modifiez avant ajout",
|
|
style = MaterialTheme.typography.bodySmall,
|
|
color = MaterialTheme.colorScheme.onSurfaceVariant
|
|
)
|
|
}
|
|
}
|
|
TextButton(
|
|
onClick = onAddBlankItem,
|
|
modifier = Modifier.testTag("add_blank_receipt_item_button")
|
|
) {
|
|
Icon(Icons.Default.Add, contentDescription = null, modifier = Modifier.size(16.dp))
|
|
Spacer(modifier = Modifier.width(4.dp))
|
|
Text("Ligne")
|
|
}
|
|
}
|
|
|
|
LazyColumn(
|
|
modifier = Modifier
|
|
.fillMaxWidth()
|
|
.heightIn(max = 420.dp)
|
|
.testTag("receipt_review_list"),
|
|
verticalArrangement = Arrangement.spacedBy(10.dp)
|
|
) {
|
|
items(scanState.items, key = { it.id }) { scannedItem ->
|
|
ScannedItemReviewCard(
|
|
item = scannedItem,
|
|
onUpdate = onUpdateItem,
|
|
onRemove = { onRemoveItem(scannedItem.id) }
|
|
)
|
|
}
|
|
}
|
|
|
|
Surface(
|
|
shape = RoundedCornerShape(14.dp),
|
|
color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.45f),
|
|
modifier = Modifier.fillMaxWidth()
|
|
) {
|
|
Row(
|
|
modifier = Modifier
|
|
.fillMaxWidth()
|
|
.padding(horizontal = 14.dp, vertical = 8.dp),
|
|
verticalAlignment = Alignment.CenterVertically,
|
|
horizontalArrangement = Arrangement.SpaceBetween
|
|
) {
|
|
Text(
|
|
text = "Cocher aussi dans la liste de courses",
|
|
style = MaterialTheme.typography.bodySmall,
|
|
modifier = Modifier.weight(1f)
|
|
)
|
|
Switch(
|
|
checked = alsoCheckShoppingList,
|
|
onCheckedChange = { alsoCheckShoppingList = it },
|
|
modifier = Modifier.testTag("receipt_also_check_shopping_switch")
|
|
)
|
|
}
|
|
}
|
|
|
|
Row(
|
|
modifier = Modifier.fillMaxWidth(),
|
|
horizontalArrangement = Arrangement.spacedBy(10.dp)
|
|
) {
|
|
OutlinedButton(
|
|
onClick = onDismiss,
|
|
shape = RoundedCornerShape(14.dp),
|
|
modifier = Modifier.weight(0.4f)
|
|
) {
|
|
Text("Annuler")
|
|
}
|
|
Button(
|
|
onClick = { onConfirmImport(scanState.items, alsoCheckShoppingList) },
|
|
enabled = selectedValidCount > 0,
|
|
shape = RoundedCornerShape(14.dp),
|
|
modifier = Modifier
|
|
.weight(0.6f)
|
|
.testTag("confirm_receipt_import_button")
|
|
) {
|
|
Text("Ajouter $selectedValidCount au stock")
|
|
}
|
|
}
|
|
Spacer(modifier = Modifier.height(8.dp))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@OptIn(ExperimentalMaterial3Api::class)
|
|
@Composable
|
|
private fun ScannedItemReviewCard(
|
|
item: ScannedReceiptItem,
|
|
onUpdate: (ScannedReceiptItem) -> Unit,
|
|
onRemove: () -> Unit
|
|
) {
|
|
var qtyText by remember(item.id) {
|
|
mutableStateOf(
|
|
if (item.quantity % 1.0 == 0.0) item.quantity.toInt().toString() else item.quantity.toString()
|
|
)
|
|
}
|
|
var showDatePicker by remember { mutableStateOf(false) }
|
|
val today = remember { LocalDate.now() }
|
|
val dateFmt = remember { DateTimeFormatter.ofPattern("d MMM", Locale.FRENCH) }
|
|
|
|
Card(
|
|
shape = RoundedCornerShape(16.dp),
|
|
colors = CardDefaults.cardColors(
|
|
containerColor = if (item.selected) {
|
|
MaterialTheme.colorScheme.surface
|
|
} else {
|
|
MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.35f)
|
|
}
|
|
),
|
|
border = BorderStroke(
|
|
width = 1.dp,
|
|
color = if (item.selected) {
|
|
MaterialTheme.colorScheme.outlineVariant
|
|
} else {
|
|
MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.3f)
|
|
}
|
|
),
|
|
modifier = Modifier.fillMaxWidth()
|
|
) {
|
|
Column(
|
|
modifier = Modifier
|
|
.fillMaxWidth()
|
|
.padding(12.dp),
|
|
verticalArrangement = Arrangement.spacedBy(8.dp)
|
|
) {
|
|
Row(
|
|
modifier = Modifier.fillMaxWidth(),
|
|
verticalAlignment = Alignment.CenterVertically
|
|
) {
|
|
Checkbox(
|
|
checked = item.selected,
|
|
onCheckedChange = { onUpdate(item.copy(selected = it)) }
|
|
)
|
|
Column(modifier = Modifier.weight(1f)) {
|
|
if (item.rawLabel.isNotBlank() && !item.rawLabel.equals(item.name, ignoreCase = true)) {
|
|
Text(
|
|
text = "Ticket : ${item.rawLabel}",
|
|
style = MaterialTheme.typography.labelSmall,
|
|
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
|
maxLines = 1,
|
|
overflow = TextOverflow.Ellipsis
|
|
)
|
|
}
|
|
OutlinedTextField(
|
|
value = item.name,
|
|
onValueChange = { onUpdate(item.copy(name = it)) },
|
|
placeholder = { Text("Nom de l'aliment") },
|
|
singleLine = true,
|
|
shape = RoundedCornerShape(12.dp),
|
|
modifier = Modifier.fillMaxWidth()
|
|
)
|
|
}
|
|
IconButton(onClick = onRemove) {
|
|
Icon(
|
|
imageVector = Icons.Default.DeleteOutline,
|
|
contentDescription = "Retirer ${item.name}",
|
|
tint = MaterialTheme.colorScheme.onSurfaceVariant
|
|
)
|
|
}
|
|
}
|
|
|
|
Row(
|
|
modifier = Modifier.fillMaxWidth(),
|
|
horizontalArrangement = Arrangement.spacedBy(8.dp),
|
|
verticalAlignment = Alignment.CenterVertically
|
|
) {
|
|
OutlinedTextField(
|
|
value = qtyText,
|
|
onValueChange = { raw ->
|
|
qtyText = raw
|
|
val parsed = raw.replace(',', '.').toDoubleOrNull()
|
|
if (parsed != null && parsed > 0.0) {
|
|
onUpdate(item.copy(quantity = parsed))
|
|
}
|
|
},
|
|
label = { Text("Qté") },
|
|
singleLine = true,
|
|
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Decimal),
|
|
shape = RoundedCornerShape(12.dp),
|
|
modifier = Modifier.width(92.dp)
|
|
)
|
|
|
|
Row(horizontalArrangement = Arrangement.spacedBy(4.dp)) {
|
|
FoodUnit.entries.forEach { unit ->
|
|
FilterChip(
|
|
selected = item.unit == unit,
|
|
onClick = { onUpdate(item.copy(unit = unit)) },
|
|
label = { Text(unit.label) }
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
Row(
|
|
modifier = Modifier.fillMaxWidth(),
|
|
horizontalArrangement = Arrangement.spacedBy(6.dp)
|
|
) {
|
|
StorageLocation.entries.forEach { loc ->
|
|
FilterChip(
|
|
selected = item.location == loc,
|
|
onClick = { onUpdate(item.copy(location = loc)) },
|
|
label = { Text(loc.displayName, maxLines = 1, overflow = TextOverflow.Ellipsis) },
|
|
modifier = Modifier.weight(1f)
|
|
)
|
|
}
|
|
}
|
|
|
|
FoodCategorySelectorRow(
|
|
selectedCategory = item.category,
|
|
onCategorySelected = { cat -> onUpdate(item.copy(category = cat)) }
|
|
)
|
|
|
|
Row(
|
|
modifier = Modifier.fillMaxWidth(),
|
|
horizontalArrangement = Arrangement.spacedBy(6.dp),
|
|
verticalAlignment = Alignment.CenterVertically
|
|
) {
|
|
AssistChip(
|
|
onClick = { showDatePicker = true },
|
|
leadingIcon = {
|
|
Icon(
|
|
imageVector = Icons.Default.CalendarMonth,
|
|
contentDescription = null,
|
|
modifier = Modifier.size(16.dp)
|
|
)
|
|
},
|
|
label = {
|
|
Text(
|
|
text = item.expirationDateEpochDay?.let {
|
|
"DLC : ${LocalDate.ofEpochDay(it).format(dateFmt)}"
|
|
} ?: "DLC : aucune",
|
|
maxLines = 1
|
|
)
|
|
}
|
|
)
|
|
FilterChip(
|
|
selected = item.expirationDateEpochDay == today.plusDays(3).toEpochDay(),
|
|
onClick = { onUpdate(item.copy(expirationDateEpochDay = today.plusDays(3).toEpochDay())) },
|
|
label = { Text("+3 j") }
|
|
)
|
|
FilterChip(
|
|
selected = item.expirationDateEpochDay == today.plusDays(7).toEpochDay(),
|
|
onClick = { onUpdate(item.copy(expirationDateEpochDay = today.plusDays(7).toEpochDay())) },
|
|
label = { Text("+7 j") }
|
|
)
|
|
if (item.expirationDateEpochDay != null) {
|
|
IconButton(
|
|
onClick = { onUpdate(item.copy(expirationDateEpochDay = null)) },
|
|
modifier = Modifier.size(32.dp)
|
|
) {
|
|
Icon(
|
|
imageVector = Icons.Default.Close,
|
|
contentDescription = "Effacer la DLC",
|
|
modifier = Modifier.size(16.dp)
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (showDatePicker) {
|
|
val initialMillis = (item.expirationDateEpochDay ?: today.plusDays(5).toEpochDay()) * 86_400_000L
|
|
val datePickerState = rememberDatePickerState(initialSelectedDateMillis = initialMillis)
|
|
DatePickerDialog(
|
|
onDismissRequest = { showDatePicker = false },
|
|
confirmButton = {
|
|
TextButton(
|
|
onClick = {
|
|
datePickerState.selectedDateMillis?.let { millis ->
|
|
val pickedEpoch = Instant.ofEpochMilli(millis)
|
|
.atZone(ZoneOffset.UTC)
|
|
.toLocalDate()
|
|
.toEpochDay()
|
|
onUpdate(item.copy(expirationDateEpochDay = pickedEpoch))
|
|
}
|
|
showDatePicker = false
|
|
}
|
|
) {
|
|
Text("Valider")
|
|
}
|
|
},
|
|
dismissButton = {
|
|
TextButton(onClick = { showDatePicker = false }) {
|
|
Text("Annuler")
|
|
}
|
|
}
|
|
) {
|
|
DatePicker(state = datePickerState)
|
|
}
|
|
}
|
|
}
|