fix(ui): Improve item suggestion selection logic
Corrects the behavior of item suggestion selection in the shopping list detail screen. - When a suggested item is clicked, the input field now correctly preserves previous entries (before the last comma or space) and only replaces the last part of the input with the selected item's name. - This ensures that users can select suggestions without losing previously typed items in a comma/space-separated input.
This commit is contained in:
@@ -90,9 +90,14 @@ fun ShoppingListDetailScreen(
|
||||
)
|
||||
|
||||
if (newItemName.isNotBlank()) {
|
||||
val suggestions = shoppingListWithItems?.items?.filter {
|
||||
it.name.contains(newItemName, ignoreCase = true) && it.isChecked
|
||||
} ?: emptyList()
|
||||
val currentSearchTerm = newItemName.substringAfterLast(',').trim()
|
||||
val suggestions = if (currentSearchTerm.isNotBlank()) {
|
||||
shoppingListWithItems?.items?.filter {
|
||||
it.name.contains(currentSearchTerm, ignoreCase = true) && it.isChecked
|
||||
} ?: emptyList()
|
||||
} else {
|
||||
emptyList()
|
||||
}
|
||||
|
||||
if (suggestions.isNotEmpty()) {
|
||||
LazyColumn(modifier = Modifier.heightIn(max = 150.dp)) {
|
||||
@@ -104,7 +109,19 @@ fun ShoppingListDetailScreen(
|
||||
.clickable {
|
||||
coroutineScope.launch {
|
||||
viewModel.saveShoppingListItem(item.copy(isChecked = false))
|
||||
viewModel.onNewItemNameChange("")
|
||||
val currentInput = viewModel.newItemName.value
|
||||
val lastCommaIndex = currentInput.lastIndexOf(',')
|
||||
val lastSpaceIndex = currentInput.lastIndexOf(' ')
|
||||
|
||||
val separatorIndex = maxOf(lastCommaIndex, lastSpaceIndex)
|
||||
|
||||
val prefix = if (separatorIndex != -1) {
|
||||
currentInput.substring(0, separatorIndex + 1)
|
||||
} else {
|
||||
""
|
||||
}
|
||||
val newInputValue = prefix
|
||||
viewModel.onNewItemNameChange(newInputValue)
|
||||
}
|
||||
},
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
|
||||
Reference in New Issue
Block a user