Improved showing of suggestions on chats screen

This commit is contained in:
Juha Heinanen
2026-01-18 23:15:26 +02:00
parent 47fe79cdff
commit 9d1d3f6bc7
@@ -66,6 +66,7 @@ import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalFocusManager import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.ImeAction import androidx.compose.ui.text.input.ImeAction
@@ -320,9 +321,7 @@ private fun Chats(
model = thumbNailUri, model = thumbNailUri,
contentDescription = "Avatar", contentDescription = "Avatar",
contentScale = ContentScale.Crop, contentScale = ContentScale.Crop,
modifier = Modifier modifier = Modifier.size(36.dp).clip(CircleShape),
.size(36.dp)
.clip(CircleShape),
) )
else else
CustomElements.TextAvatar(contact.name, contact.color) CustomElements.TextAvatar(contact.name, contact.color)
@@ -484,7 +483,8 @@ private fun NewChatPeer(ctx: Context, navController: NavController, account: Acc
) )
val suggestions by remember { contactNames } val suggestions by remember { contactNames }
var filteredSuggestions by remember { mutableStateOf(suggestions) } // 1. Change state to hold AnnotatedStrings
var filteredSuggestions by remember { mutableStateOf<List<AnnotatedString>>(emptyList()) }
var showSuggestions by remember { mutableStateOf(false) } var showSuggestions by remember { mutableStateOf(false) }
val lazyListState = rememberLazyListState() val lazyListState = rememberLazyListState()
val focusManager = LocalFocusManager.current val focusManager = LocalFocusManager.current
@@ -524,20 +524,21 @@ private fun NewChatPeer(ctx: Context, navController: NavController, account: Acc
state = lazyListState state = lazyListState
) { ) {
items( items(
// 3. Update key and clickable logic
items = filteredSuggestions, items = filteredSuggestions,
key = { suggestion -> suggestion } key = { suggestion -> suggestion.toString() }
) { suggestion -> ) { suggestion ->
Box( Box(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.clickable { .clickable {
newPeer = suggestion newPeer = suggestion.toString()
showSuggestions = false showSuggestions = false
} }
.padding(12.dp) .padding(12.dp)
) { ) {
Text( Text(
text = suggestion, text = suggestion, // Text composable accepts AnnotatedString
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
color = MaterialTheme.colorScheme.onSurface, color = MaterialTheme.colorScheme.onSurface,
fontSize = 18.sp fontSize = 18.sp
@@ -554,16 +555,20 @@ private fun NewChatPeer(ctx: Context, navController: NavController, account: Acc
placeholder = { Text(stringResource(R.string.new_chat_peer)) }, placeholder = { Text(stringResource(R.string.new_chat_peer)) },
onValueChange = { onValueChange = {
newPeer = it newPeer = it
showSuggestions = newPeer.length > 2 showSuggestions = newPeer.length > 1
// 2. Update filtering and mapping logic
filteredSuggestions = if (it.isEmpty()) { filteredSuggestions = if (it.isEmpty()) {
suggestions emptyList()
} else { } else {
suggestions.filter { suggestion -> val normalizedInput = Utils.unaccent(it)
newPeer.length > 2 && suggestion.startsWith( suggestions
newPeer, .filter { suggestion ->
ignoreCase = true it.length > 1 &&
) Utils.unaccent(suggestion).contains(normalizedInput, ignoreCase = true)
} }
.map { suggestion ->
Utils.buildAnnotatedStringWithHighlight(suggestion, it)
}
} }
}, },
modifier = Modifier.padding(end = 6.dp).fillMaxWidth(), modifier = Modifier.padding(end = 6.dp).fillMaxWidth(),
@@ -576,8 +581,7 @@ private fun NewChatPeer(ctx: Context, navController: NavController, account: Acc
modifier = Modifier.clickable { modifier = Modifier.clickable {
if (showSuggestions) if (showSuggestions)
showSuggestions = false showSuggestions = false
else newPeer = ""
newPeer = ""
}, },
tint = MaterialTheme.colorScheme.onSurfaceVariant tint = MaterialTheme.colorScheme.onSurfaceVariant
) )