Skip to content
Snippets Groups Projects
Verified Commit 67dec15d authored by Mikolai Gütschow's avatar Mikolai Gütschow
Browse files

use ByteArray as uniqueId instead of wrapper classes implementing empty interface

parent 2f05eea3
No related branches found
No related tags found
1 merge request!276Refactor contact item
Pipeline #13180 passed
......@@ -45,10 +45,7 @@ data class ContactItem(
override val timestamp: Long,
val avatar: ImageBitmap?,
) : ContactListItem {
data class Id(val id: ContactId) : ContactListItemId
override val wrapperId = Id(id)
override val uniqueId: ByteArray = ByteArray(4) { i -> (id.int shr (i * 8)).toByte() }
override val displayName = getContactDisplayName(name, alias)
constructor(
......@@ -56,7 +53,7 @@ data class ContactItem(
authorInfo: AuthorInfo,
isConnected: Boolean,
groupCount: MessageTracker.GroupCount,
avatar: ImageBitmap?
avatar: ImageBitmap?,
) : this(
id = contact.id,
authorId = contact.author.id,
......@@ -96,7 +93,7 @@ fun loadContactItem(
authorManager: AuthorManager,
connectionRegistry: ConnectionRegistry,
conversationManager: ConversationManager,
attachmentReader: AttachmentReader
attachmentReader: AttachmentReader,
): ContactItem {
val authorInfo = authorManager.getAuthorInfo(txn, contact)
return ContactItem(
......
......@@ -157,7 +157,7 @@ fun ContactList(
) {
items(
items = contactList,
key = { item -> item.wrapperId },
key = { item -> item.uniqueId },
contentType = { item -> item::class }
) { item ->
ListItemView(
......
......@@ -21,7 +21,5 @@ package org.briarproject.briar.desktop.contact
interface ContactListItem {
val displayName: String
val timestamp: Long
val wrapperId: ContactListItemId
val uniqueId: ByteArray
}
interface ContactListItemId
......@@ -81,7 +81,7 @@ constructor(
private val _pendingContactList = mutableStateListOf<PendingContactItem>()
private val _filterBy = mutableStateOf("")
private val _selectedContactId = mutableStateOf<ContactListItemId?>(null)
private val _selectedContactListItem = mutableStateOf<ContactListItem?>(null)
private val _contactIdToBeRemoved = mutableStateOf<PendingContactId?>(null)
// todo: check impact on performance due to reconstructing whole list on every change
......@@ -97,13 +97,13 @@ constructor(
}
val filterBy = _filterBy.asState()
val selectedContactId = derivedStateOf {
val selectedContactListItem = derivedStateOf {
// reset selected contact to null if not part of list after filtering
val wrapperId = _selectedContactId.value
if (wrapperId == null || combinedContactList.value.find { it.wrapperId == wrapperId } != null) {
wrapperId
val item = _selectedContactListItem.value
if (item == null || combinedContactList.value.find { it.uniqueId.contentEquals(item.uniqueId) } != null) {
item
} else {
_selectedContactId.value = null
_selectedContactListItem.value = null
null
}
}
......@@ -123,10 +123,11 @@ constructor(
}
fun selectContact(contactItem: ContactListItem) {
_selectedContactId.value = contactItem.wrapperId
_selectedContactListItem.value = contactItem
}
fun isSelected(contactItem: ContactListItem) = _selectedContactId.value == contactItem.wrapperId
fun isSelected(contactItem: ContactListItem) =
_selectedContactListItem.value?.uniqueId.contentEquals(contactItem.uniqueId)
fun removePendingContact(contactItem: PendingContactItem) {
if (contactItem.state == PendingContactState.FAILED) {
......
......@@ -22,7 +22,6 @@ import org.briarproject.bramble.api.contact.PendingContact
import org.briarproject.bramble.api.contact.PendingContactId
import org.briarproject.bramble.api.contact.PendingContactState
import org.briarproject.briar.desktop.contact.ContactListItem
import org.briarproject.briar.desktop.contact.ContactListItemId
data class PendingContactItem(
val id: PendingContactId,
......@@ -30,9 +29,8 @@ data class PendingContactItem(
override val timestamp: Long,
val state: PendingContactState,
) : ContactListItem {
data class Id(val id: PendingContactId) : ContactListItemId
override val wrapperId = Id(id)
override val uniqueId: ByteArray = id.bytes
override val displayName = alias
constructor(contact: PendingContact, state: PendingContactState) : this(
......
......@@ -80,14 +80,14 @@ fun PrivateMessageScreen(
)
VerticalDivider()
Column(modifier = Modifier.weight(1f).fillMaxHeight()) {
val wrapperId = viewModel.selectedContactId.value
if (wrapperId == null) {
val item = viewModel.selectedContactListItem.value
if (item == null) {
NoContactSelected()
} else when (wrapperId) {
is ContactItem.Id -> {
ConversationScreen(wrapperId.id)
} else when (item) {
is ContactItem -> {
ConversationScreen(item.id)
}
is PendingContactItem.Id -> {
is PendingContactItem -> {
PendingContactSelected()
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment