mirror of
https://github.com/oxen-io/session-android.git
synced 2025-05-08 04:36:51 +00:00
Simplify search processing
This commit is contained in:
parent
a52bafbf98
commit
f2e72d794b
@ -288,47 +288,37 @@ class HomeActivity : PassphraseRequiredActionBarActivity(),
|
|||||||
launch {
|
launch {
|
||||||
globalSearchViewModel.result.collect { result ->
|
globalSearchViewModel.result.collect { result ->
|
||||||
if (result.query.isEmpty()) {
|
if (result.query.isEmpty()) {
|
||||||
class NamedValue<T>(val name: String, val value: T)
|
class NamedValue<T>(val name: String?, val value: T)
|
||||||
|
|
||||||
val hasNames = result.contacts
|
val unknown = resources.getString(R.string.unknown)
|
||||||
|
|
||||||
|
listOf(
|
||||||
|
GlobalSearchAdapter.Model.Header(R.string.contacts),
|
||||||
|
GlobalSearchAdapter.Model.SavedMessages(publicKey)
|
||||||
|
) + result.contacts
|
||||||
// Remove ourself, we're shown above.
|
// Remove ourself, we're shown above.
|
||||||
.filter { it.accountID != publicKey }
|
.filter { it.accountID != publicKey }
|
||||||
// Get the name that we will display and sort by, and uppercase it to
|
// Get the name that we will display and sort by, and uppercase it to
|
||||||
// help with sorting and we need the char uppercased later.
|
// help with sorting and we need the char uppercased later.
|
||||||
.mapNotNull { (it.nickname?.takeIf { it.isNotEmpty() } ?: it.name?.takeIf { it.isNotEmpty() })
|
.map { (it.nickname?.takeIf(String::isNotEmpty) ?: it.name?.takeIf(String::isNotEmpty))
|
||||||
?.let { name -> NamedValue(name.uppercase(), it) } }
|
.let { name -> NamedValue(name?.uppercase(), it) } }
|
||||||
// Digits are all grouped under a #, the rest are grouped by their first character.uppercased()
|
// Digits are all grouped under a #, the rest are grouped by their first character.uppercased()
|
||||||
.groupBy { (it.name.first().takeUnless(Char::isDigit) ?: '#') }
|
// If there is no name, they go under Unknown
|
||||||
|
.groupBy { it.name?.run { first().takeUnless(Char::isDigit)?.toString() ?: "#" } ?: unknown }
|
||||||
// place the # at the end, after all the names starting with alphabetic chars
|
// place the # at the end, after all the names starting with alphabetic chars
|
||||||
.toSortedMap(compareBy {
|
.toSortedMap(compareBy {
|
||||||
when (it) {
|
when (it) {
|
||||||
'#' -> Char.MAX_VALUE - 1
|
unknown -> Char.MAX_VALUE
|
||||||
else -> it
|
"#" -> Char.MAX_VALUE - 1
|
||||||
|
else -> it.first()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
// Flatten the map of char to lists into an actual List that can be displayed.
|
// Flatten the map of char to lists into an actual List that can be displayed.
|
||||||
.flatMap { (key, contacts) ->
|
.flatMap { (key, contacts) ->
|
||||||
listOf(
|
listOf(
|
||||||
GlobalSearchAdapter.Model.SubHeader(key.toString())
|
GlobalSearchAdapter.Model.SubHeader(key)
|
||||||
) + contacts.sortedBy { it.name }.map { it.value }.map(GlobalSearchAdapter.Model::Contact)
|
) + contacts.sortedBy { it.name ?: it.value.accountID }.map { it.value }.map(GlobalSearchAdapter.Model::Contact)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Similarly we want to display
|
|
||||||
val noNames = result.contacts
|
|
||||||
.filter { it.accountID != publicKey }
|
|
||||||
.filter { it.nickname.isNullOrEmpty() && it.name.isNullOrEmpty() }
|
|
||||||
.sortedBy { it.accountID }
|
|
||||||
.map { GlobalSearchAdapter.Model.Contact(it) }
|
|
||||||
|
|
||||||
buildList {
|
|
||||||
add(GlobalSearchAdapter.Model.Header(R.string.contacts))
|
|
||||||
add(GlobalSearchAdapter.Model.SavedMessages(publicKey))
|
|
||||||
addAll(hasNames)
|
|
||||||
noNames.takeIf { it.isNotEmpty() }?.let {
|
|
||||||
add(GlobalSearchAdapter.Model.Header(R.string.unknown))
|
|
||||||
addAll(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
val currentUserPublicKey = publicKey
|
val currentUserPublicKey = publicKey
|
||||||
val contactAndGroupList = result.contacts.map(GlobalSearchAdapter.Model::Contact) +
|
val contactAndGroupList = result.contacts.map(GlobalSearchAdapter.Model::Contact) +
|
||||||
|
Loading…
x
Reference in New Issue
Block a user