2020-05-11 08:19:26 +02:00
|
|
|
package org.thoughtcrime.securesms.loki.fragments
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import network.loki.messenger.R
|
2021-07-09 05:18:48 +02:00
|
|
|
import org.thoughtcrime.securesms.util.ContactUtilities
|
2021-05-18 08:03:47 +02:00
|
|
|
import org.session.libsession.utilities.recipients.Recipient
|
2020-05-11 08:19:26 +02:00
|
|
|
import org.thoughtcrime.securesms.util.AsyncLoader
|
|
|
|
|
|
|
|
sealed class ContactSelectionListItem {
|
|
|
|
class Header(val name: String) : ContactSelectionListItem()
|
|
|
|
class Contact(val recipient: Recipient) : ContactSelectionListItem()
|
|
|
|
}
|
|
|
|
|
|
|
|
class ContactSelectionListLoader(context: Context, val mode: Int, val filter: String?) : AsyncLoader<List<ContactSelectionListItem>>(context) {
|
|
|
|
|
|
|
|
object DisplayMode {
|
2020-07-15 06:26:20 +02:00
|
|
|
const val FLAG_CONTACTS = 1
|
2020-05-11 08:19:26 +02:00
|
|
|
const val FLAG_CLOSED_GROUPS = 1 shl 1
|
|
|
|
const val FLAG_OPEN_GROUPS = 1 shl 2
|
2020-07-15 06:26:20 +02:00
|
|
|
const val FLAG_ALL = FLAG_CONTACTS or FLAG_CLOSED_GROUPS or FLAG_OPEN_GROUPS
|
2020-05-11 08:19:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun isFlagSet(flag: Int): Boolean {
|
|
|
|
return mode and flag > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun loadInBackground(): List<ContactSelectionListItem> {
|
|
|
|
val contacts = ContactUtilities.getAllContacts(context).filter {
|
|
|
|
if (filter.isNullOrEmpty()) return@filter true
|
2021-02-17 02:27:00 +01:00
|
|
|
it.toShortString().contains(filter.trim(), true) || it.address.serialize().contains(filter.trim(), true)
|
2020-05-11 08:19:26 +02:00
|
|
|
}.sortedBy {
|
2021-02-17 02:27:00 +01:00
|
|
|
it.toShortString()
|
2020-05-11 08:19:26 +02:00
|
|
|
}
|
|
|
|
val list = mutableListOf<ContactSelectionListItem>()
|
|
|
|
if (isFlagSet(DisplayMode.FLAG_CLOSED_GROUPS)) {
|
|
|
|
list.addAll(getClosedGroups(contacts))
|
|
|
|
}
|
|
|
|
if (isFlagSet(DisplayMode.FLAG_OPEN_GROUPS)) {
|
|
|
|
list.addAll(getOpenGroups(contacts))
|
|
|
|
}
|
2020-07-15 06:26:20 +02:00
|
|
|
if (isFlagSet(DisplayMode.FLAG_CONTACTS)) {
|
|
|
|
list.addAll(getContacts(contacts))
|
2020-05-11 08:19:26 +02:00
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2021-02-17 02:27:00 +01:00
|
|
|
private fun getContacts(contacts: List<Recipient>): List<ContactSelectionListItem> {
|
2020-05-11 08:19:26 +02:00
|
|
|
return getItems(contacts, context.getString(R.string.fragment_contact_selection_contacts_title)) {
|
2021-02-17 02:27:00 +01:00
|
|
|
!it.isGroupRecipient
|
2020-05-11 08:19:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 02:27:00 +01:00
|
|
|
private fun getClosedGroups(contacts: List<Recipient>): List<ContactSelectionListItem> {
|
2020-05-11 08:19:26 +02:00
|
|
|
return getItems(contacts, context.getString(R.string.fragment_contact_selection_closed_groups_title)) {
|
2021-02-17 02:27:00 +01:00
|
|
|
it.address.isClosedGroup
|
2020-05-11 08:19:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 02:27:00 +01:00
|
|
|
private fun getOpenGroups(contacts: List<Recipient>): List<ContactSelectionListItem> {
|
2020-05-11 08:19:26 +02:00
|
|
|
return getItems(contacts, context.getString(R.string.fragment_contact_selection_open_groups_title)) {
|
2021-02-17 02:27:00 +01:00
|
|
|
it.address.isOpenGroup
|
2020-05-11 08:19:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 02:27:00 +01:00
|
|
|
private fun getItems(contacts: List<Recipient>, title: String, contactFilter: (Recipient) -> Boolean): List<ContactSelectionListItem> {
|
2020-05-11 08:19:26 +02:00
|
|
|
val items = contacts.filter(contactFilter).map {
|
2021-02-17 02:27:00 +01:00
|
|
|
ContactSelectionListItem.Contact(it)
|
2020-05-11 08:19:26 +02:00
|
|
|
}
|
|
|
|
if (items.isEmpty()) return listOf()
|
|
|
|
val header = ContactSelectionListItem.Header(title)
|
|
|
|
return listOf(header) + items
|
|
|
|
}
|
|
|
|
}
|