1
0
Fork 0
mirror of https://github.com/anyproto/anytype-kotlin.git synced 2025-06-08 05:47:05 +09:00

DROID-2650 Global search | Highlight the title in search results (#1477)

This commit is contained in:
Konstantin Ivanov 2024-08-13 16:06:09 +02:00 committed by GitHub
parent d7876d604e
commit 12edfa4101
Signed by: github
GPG key ID: B5690EEEBB952194
2 changed files with 55 additions and 10 deletions

View file

@ -452,14 +452,9 @@ private fun GlobalSearchItem(
)
.align(Alignment.CenterStart)
) {
Text(
text = globalSearchItemView.title.ifEmpty {
stringResource(id = R.string.untitled)
},
style = PreviewTitle2Medium,
color = colorResource(id = R.color.text_primary),
maxLines = 1,
overflow = TextOverflow.Ellipsis
DefaultTitleWithHighlights(
text = globalSearchItemView.title,
nameMeta = globalSearchItemView.nameMeta
)
when(val meta = globalSearchItemView.meta) {
is GlobalSearchItemView.Meta.Block -> {
@ -581,6 +576,36 @@ private fun DefaultMetaBlockWithHighlights(
)
}
@Composable
private fun DefaultTitleWithHighlights(
text: String,
nameMeta: GlobalSearchItemView.NameMeta?
) {
val title = when {
text.isEmpty() -> AnnotatedString(stringResource(id = R.string.untitled))
nameMeta == null -> AnnotatedString(text)
else -> AnnotatedString(
text = text,
spanStyles = nameMeta.highlights.map { range ->
AnnotatedString.Range(
SpanStyle(
background = colorResource(id = R.color.palette_light_ice)
),
range.first,
range.last
)
}
)
}
Text(
text = title,
style = PreviewTitle2Medium,
color = colorResource(id = R.color.text_primary),
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
}
@Composable
private fun DefaultMetaRelationWithHighlights(
title: String,

View file

@ -363,8 +363,14 @@ data class GlobalSearchItemView(
val meta: Meta,
val links: List<Id> = emptyList(),
val backlinks: List<Id> = emptyList(),
val pinned: Boolean = false
val pinned: Boolean = false,
val nameMeta: NameMeta? = null
) {
data class NameMeta(
val name: String,
val highlights: List<IntRange> = emptyList()
)
sealed class Meta {
data object None : Meta()
data class Default(
@ -488,6 +494,20 @@ suspend fun Command.SearchWithMeta.Result.view(
}
} else {
GlobalSearchItemView.Meta.None
}
},
nameMeta = metas.getNameMeta()
)
}
private fun List<Command.SearchWithMeta.Result.Meta>.getNameMeta(): GlobalSearchItemView.NameMeta? {
val meta =
firstOrNull { (it.source as? Command.SearchWithMeta.Result.Meta.Source.Relation)?.key == Relations.NAME }
return if (meta != null) {
GlobalSearchItemView.NameMeta(
name = meta.highlight.orEmpty(),
highlights = meta.ranges
)
} else {
null
}
}