mirror of
https://github.com/oxen-io/session-android.git
synced 2024-12-26 09:47:43 +00:00
Cleanup search processing
This commit is contained in:
parent
e3a33ea615
commit
38e73cf514
@ -22,6 +22,7 @@ import kotlinx.coroutines.Dispatchers
|
|||||||
import kotlinx.coroutines.flow.collectLatest
|
import kotlinx.coroutines.flow.collectLatest
|
||||||
import kotlinx.coroutines.flow.filter
|
import kotlinx.coroutines.flow.filter
|
||||||
import kotlinx.coroutines.flow.filterNotNull
|
import kotlinx.coroutines.flow.filterNotNull
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import network.loki.messenger.R
|
import network.loki.messenger.R
|
||||||
@ -58,6 +59,7 @@ import org.thoughtcrime.securesms.dependencies.DatabaseComponent
|
|||||||
import org.thoughtcrime.securesms.groups.OpenGroupManager
|
import org.thoughtcrime.securesms.groups.OpenGroupManager
|
||||||
import org.thoughtcrime.securesms.home.search.GlobalSearchAdapter
|
import org.thoughtcrime.securesms.home.search.GlobalSearchAdapter
|
||||||
import org.thoughtcrime.securesms.home.search.GlobalSearchInputLayout
|
import org.thoughtcrime.securesms.home.search.GlobalSearchInputLayout
|
||||||
|
import org.thoughtcrime.securesms.home.search.GlobalSearchResult
|
||||||
import org.thoughtcrime.securesms.home.search.GlobalSearchViewModel
|
import org.thoughtcrime.securesms.home.search.GlobalSearchViewModel
|
||||||
import org.thoughtcrime.securesms.messagerequests.MessageRequestsActivity
|
import org.thoughtcrime.securesms.messagerequests.MessageRequestsActivity
|
||||||
import org.thoughtcrime.securesms.mms.GlideApp
|
import org.thoughtcrime.securesms.mms.GlideApp
|
||||||
@ -239,8 +241,43 @@ class HomeActivity : PassphraseRequiredActionBarActivity(),
|
|||||||
}
|
}
|
||||||
// Get group results and display them
|
// Get group results and display them
|
||||||
launch {
|
launch {
|
||||||
globalSearchViewModel.result.collect { result ->
|
globalSearchViewModel.result.map { result ->
|
||||||
if (result.query.isEmpty()) {
|
result.query to when {
|
||||||
|
result.query.isEmpty() -> buildList {
|
||||||
|
add(GlobalSearchAdapter.Model.Header(R.string.contacts))
|
||||||
|
add(GlobalSearchAdapter.Model.SavedMessages(publicKey))
|
||||||
|
addAll(result.groupedContacts)
|
||||||
|
}
|
||||||
|
else -> buildList {
|
||||||
|
result.contactAndGroupList.takeUnless { it.isEmpty() }?.let {
|
||||||
|
add(GlobalSearchAdapter.Model.Header(R.string.contacts))
|
||||||
|
addAll(it)
|
||||||
|
}
|
||||||
|
result.messageResults.takeUnless { it.isEmpty() }?.let {
|
||||||
|
add(GlobalSearchAdapter.Model.Header(R.string.global_search_messages))
|
||||||
|
addAll(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.collectLatest(globalSearchAdapter::setNewData)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
EventBus.getDefault().register(this@HomeActivity)
|
||||||
|
if (intent.hasExtra(FROM_ONBOARDING)
|
||||||
|
&& intent.getBooleanExtra(FROM_ONBOARDING, false)) {
|
||||||
|
if (Build.VERSION.SDK_INT >= 33 &&
|
||||||
|
(getSystemService(NOTIFICATION_SERVICE) as NotificationManager).areNotificationsEnabled().not()) {
|
||||||
|
Permissions.with(this)
|
||||||
|
.request(Manifest.permission.POST_NOTIFICATIONS)
|
||||||
|
.execute()
|
||||||
|
}
|
||||||
|
configFactory.user
|
||||||
|
?.takeUnless { it.isBlockCommunityMessageRequestsSet() }
|
||||||
|
?.setCommunityMessageRequests(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private val GlobalSearchResult.groupedContacts: List<GlobalSearchAdapter.Model> get() {
|
||||||
class NamedValue<T>(val name: String?, val value: T)
|
class NamedValue<T>(val name: String?, val value: T)
|
||||||
|
|
||||||
// Unknown is temporarily to be grouped together with numbers title.
|
// Unknown is temporarily to be grouped together with numbers title.
|
||||||
@ -248,10 +285,7 @@ class HomeActivity : PassphraseRequiredActionBarActivity(),
|
|||||||
val numbersTitle = "#"
|
val numbersTitle = "#"
|
||||||
val unknownTitle = numbersTitle
|
val unknownTitle = numbersTitle
|
||||||
|
|
||||||
listOf(
|
return contacts
|
||||||
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
|
||||||
@ -275,45 +309,19 @@ class HomeActivity : PassphraseRequiredActionBarActivity(),
|
|||||||
GlobalSearchAdapter.Model.SubHeader(key)
|
GlobalSearchAdapter.Model.SubHeader(key)
|
||||||
) + contacts.sortedBy { it.name ?: it.value.accountID }.map { it.value }.map { GlobalSearchAdapter.Model.Contact(it, it.nickname ?: it.name, it.accountID == publicKey) }
|
) + contacts.sortedBy { it.name ?: it.value.accountID }.map { it.value }.map { GlobalSearchAdapter.Model.Contact(it, it.nickname ?: it.name, it.accountID == publicKey) }
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
val contactAndGroupList = result.contacts.map { GlobalSearchAdapter.Model.Contact(it, it.nickname ?: it.name, it.accountID == publicKey) } +
|
|
||||||
result.threads.map(GlobalSearchAdapter.Model::GroupConversation)
|
|
||||||
|
|
||||||
val contactResults = contactAndGroupList.toMutableList()
|
|
||||||
|
|
||||||
if (contactResults.isNotEmpty()) {
|
|
||||||
contactResults.add(0, GlobalSearchAdapter.Model.Header(R.string.conversations))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val unreadThreadMap = result.messages
|
private val GlobalSearchResult.contactAndGroupList: List<GlobalSearchAdapter.Model> get() =
|
||||||
|
contacts.map { GlobalSearchAdapter.Model.Contact(it, it.nickname ?: it.name, it.accountID == publicKey) } +
|
||||||
|
threads.map(GlobalSearchAdapter.Model::GroupConversation)
|
||||||
|
|
||||||
|
private val GlobalSearchResult.messageResults: List<GlobalSearchAdapter.Model> get() {
|
||||||
|
val unreadThreadMap = messages
|
||||||
.map { it.threadId }.toSet()
|
.map { it.threadId }.toSet()
|
||||||
.associateWith { mmsSmsDatabase.getUnreadCount(it) }
|
.associateWith { mmsSmsDatabase.getUnreadCount(it) }
|
||||||
|
|
||||||
val messageResults: MutableList<GlobalSearchAdapter.Model> = result.messages
|
return messages.map {
|
||||||
.map { GlobalSearchAdapter.Model.Message(it, unreadThreadMap[it.threadId] ?: 0, it.conversationRecipient.isLocalNumber) }
|
GlobalSearchAdapter.Model.Message(it, unreadThreadMap[it.threadId] ?: 0, it.conversationRecipient.isLocalNumber)
|
||||||
.toMutableList()
|
|
||||||
|
|
||||||
if (messageResults.isNotEmpty()) {
|
|
||||||
messageResults.add(0, GlobalSearchAdapter.Model.Header(R.string.global_search_messages))
|
|
||||||
}
|
|
||||||
|
|
||||||
contactResults + messageResults
|
|
||||||
}.let { globalSearchAdapter.setNewData(result.query, it) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
EventBus.getDefault().register(this@HomeActivity)
|
|
||||||
if (intent.hasExtra(FROM_ONBOARDING)
|
|
||||||
&& intent.getBooleanExtra(FROM_ONBOARDING, false)) {
|
|
||||||
if (Build.VERSION.SDK_INT >= 33 &&
|
|
||||||
(getSystemService(NOTIFICATION_SERVICE) as NotificationManager).areNotificationsEnabled().not()) {
|
|
||||||
Permissions.with(this)
|
|
||||||
.request(Manifest.permission.POST_NOTIFICATIONS)
|
|
||||||
.execute()
|
|
||||||
}
|
|
||||||
configFactory.user
|
|
||||||
?.takeUnless { it.isBlockCommunityMessageRequestsSet() }
|
|
||||||
?.setCommunityMessageRequests(false)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,6 +28,8 @@ class GlobalSearchAdapter(private val modelCallback: (Model)->Unit): RecyclerVie
|
|||||||
private var data: List<Model> = listOf()
|
private var data: List<Model> = listOf()
|
||||||
private var query: String? = null
|
private var query: String? = null
|
||||||
|
|
||||||
|
fun setNewData(data: Pair<String, List<Model>>) = setNewData(data.first, data.second)
|
||||||
|
|
||||||
fun setNewData(query: String, newData: List<Model>) {
|
fun setNewData(query: String, newData: List<Model>) {
|
||||||
val diffResult = DiffUtil.calculateDiff(GlobalSearchDiff(this.query, query, data, newData))
|
val diffResult = DiffUtil.calculateDiff(GlobalSearchDiff(this.query, query, data, newData))
|
||||||
this.query = query
|
this.query = query
|
||||||
|
Loading…
x
Reference in New Issue
Block a user