diff --git a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/blog/sharing/BlogSharingViewModel.kt b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/blog/sharing/BlogSharingViewModel.kt index 9d9dbf201f20cd3b61bdeefc8f74bfc0eb69fb30..6d23b0cfbf061a4588c22e4dfb5d4de83618a351 100644 --- a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/blog/sharing/BlogSharingViewModel.kt +++ b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/blog/sharing/BlogSharingViewModel.kt @@ -149,17 +149,4 @@ class BlogSharingViewModel @Inject constructor( contact: Contact, ): SharingManager.SharingStatus = blogSharingManager.getSharingStatus(txn, groupId, contact) - - private fun loadSharingStatus(txn: Transaction, groupId: GroupId) { - val map = contactManager.getContacts(txn).associate { contact -> - contact.id to blogSharingManager.getSharingStatus(txn, groupId, contact) - } - val sharing = map.filterValues { it == SHARING }.keys - txn.attach { - val online = - sharing.fold(0) { acc, it -> if (connectionRegistry.isConnected(it)) acc + 1 else acc } - _sharingStatus.value = map - _sharingInfo.value = SharingInfo(sharing.size, online) - } - } } diff --git a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forum/sharing/ForumSharingViewModel.kt b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forum/sharing/ForumSharingViewModel.kt index bca0cc4054064aa98a5372c9fb7fb2017c0b05d2..1d98b8557b147d91ba1f19ec5804a61880b37782 100644 --- a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forum/sharing/ForumSharingViewModel.kt +++ b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/forum/sharing/ForumSharingViewModel.kt @@ -141,17 +141,4 @@ class ForumSharingViewModel @Inject constructor( contact: Contact, ): SharingManager.SharingStatus = forumSharingManager.getSharingStatus(txn, groupId, contact) - - private fun loadSharingStatus(txn: Transaction, groupId: GroupId) { - val map = contactManager.getContacts(txn).associate { contact -> - contact.id to forumSharingManager.getSharingStatus(txn, groupId, contact) - } - val sharing = map.filterValues { it == SHARING }.keys - txn.attach { - val online = - sharing.fold(0) { acc, it -> if (connectionRegistry.isConnected(it)) acc + 1 else acc } - _sharingStatus.value = map - _sharingInfo.value = SharingInfo(sharing.size, online) - } - } } diff --git a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/privategroup/sharing/PrivateGroupSharingViewModel.kt b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/privategroup/sharing/PrivateGroupSharingViewModel.kt index d56919e140f59f07d30b8ad86779b8bee61b0737..c2a466d30e8c4f95b0f60152b7b364c472ea7fb4 100644 --- a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/privategroup/sharing/PrivateGroupSharingViewModel.kt +++ b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/privategroup/sharing/PrivateGroupSharingViewModel.kt @@ -123,11 +123,13 @@ class PrivateGroupSharingViewModel @Inject constructor( val members = privateGroupManager.getMembers(txn, groupId).map { loadGroupMemberItem(it, connectionRegistry) } + if (isCreator) loadSharingStatus(txn, groupId) + else loadSharingStatus(txn, members) + txn.attach { _isCreator.value = isCreator _members.clearAndAddAll(members) } - loadSharingStatus(txn, groupId, members, isCreator) } } @@ -233,33 +235,17 @@ class PrivateGroupSharingViewModel @Inject constructor( ): SharingManager.SharingStatus = privateGroupInvitationManager.getSharingStatus(txn, contact, groupId) - private fun loadSharingStatus( + fun loadSharingStatus( txn: Transaction, - groupId: GroupId, members: List<GroupMemberItem>, - isCreator: Boolean, ) { - val contacts = contactManager.getContacts(txn) - if (isCreator) { - val map = contacts.associate { contact -> - contact.id to privateGroupInvitationManager.getSharingStatus(txn, contact, groupId) - } - val sharing = map.filterValues { it == SHARING }.keys - txn.attach { - val online = - sharing.fold(0) { acc, it -> if (connectionRegistry.isConnected(it)) acc + 1 else acc } - _sharingStatus.value = map - _sharingInfo.value = SharingInfo(sharing.size, online) - } - } else { - val sharing = members.mapNotNull { it.contactId } - val map = sharing.associateWith { SHARING } - txn.attach { - val online = - sharing.fold(0) { acc, it -> if (connectionRegistry.isConnected(it)) acc + 1 else acc } - _sharingStatus.value = map - _sharingInfo.value = SharingInfo(sharing.size, online) - } + val sharing = members.mapNotNull { it.contactId } + val map = sharing.associateWith { SHARING } + txn.attach { + val online = + sharing.fold(0) { acc, it -> if (connectionRegistry.isConnected(it)) acc + 1 else acc } + _sharingStatus.value = map + _sharingInfo.value = SharingInfo(sharing.size, online) } } } diff --git a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/threadedgroup/sharing/ThreadedGroupSharingViewModel.kt b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/threadedgroup/sharing/ThreadedGroupSharingViewModel.kt index aea3e2eab2b01705506f94a8a8724265eb2a18ca..2ca8120dee102289c1eb78e871b0622c730df3d5 100644 --- a/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/threadedgroup/sharing/ThreadedGroupSharingViewModel.kt +++ b/briar-desktop/src/main/kotlin/org/briarproject/briar/desktop/threadedgroup/sharing/ThreadedGroupSharingViewModel.kt @@ -38,6 +38,7 @@ import org.briarproject.briar.api.conversation.ConversationManager import org.briarproject.briar.api.identity.AuthorManager import org.briarproject.briar.api.sharing.SharingConstants import org.briarproject.briar.api.sharing.SharingManager +import org.briarproject.briar.api.sharing.SharingManager.SharingStatus.SHARING import org.briarproject.briar.desktop.contact.ContactItem import org.briarproject.briar.desktop.contact.ContactsViewModel import org.briarproject.briar.desktop.threading.BriarExecutors @@ -50,7 +51,7 @@ abstract class ThreadedGroupSharingViewModel( contactManager: ContactManager, authorManager: AuthorManager, conversationManager: ConversationManager, - connectionRegistry: ConnectionRegistry, + private val connectionRegistry: ConnectionRegistry, briarExecutors: BriarExecutors, lifecycleManager: LifecycleManager, db: TransactionManager, @@ -143,6 +144,19 @@ abstract class ThreadedGroupSharingViewModel( contact: Contact, ): SharingManager.SharingStatus + protected open fun loadSharingStatus(txn: Transaction, groupId: GroupId) { + val map = contactManager.getContacts(txn).associate { contact -> + contact.id to getSharingStatusForContact(txn, groupId, contact) + } + val sharing = map.filterValues { it == SHARING }.keys + txn.attach { + val online = + sharing.fold(0) { acc, it -> if (connectionRegistry.isConnected(it)) acc + 1 else acc } + _sharingStatus.value = map + _sharingInfo.value = SharingInfo(sharing.size, online) + } + } + data class SharingInfo(val total: Int, val online: Int) { fun addContact(connected: Boolean) = copy( total = total + 1,