Consolidate duplicated logic to retrieve groups in common.

Merges all of these into GroupsInCommonRepository:
- ConversationSettingsRepository.getGroupsInCommon()
- CallLinkIncomingRequestRepository.getGroupsInCommon()
- ContactSearchPagedDataSourceRepository.getGroupsInCommon()
- ReviewUtil.getGroupsInCommonCount()
- AboutSheetRepository.getGroupsInCommonCount()
This commit is contained in:
Jeffrey Starke
2025-04-08 11:02:29 -04:00
committed by Michelle Tang
parent c9795141df
commit aa7b61ecb1
15 changed files with 112 additions and 144 deletions

View File

@@ -7,6 +7,7 @@ import androidx.lifecycle.LiveData
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.core.Single
import io.reactivex.rxjava3.schedulers.Schedulers
import kotlinx.coroutines.rx3.asObservable
import org.signal.core.util.concurrent.SignalExecutors
import org.signal.core.util.logging.Log
import org.signal.storageservice.protos.groups.local.DecryptedGroup
@@ -21,6 +22,7 @@ import org.thoughtcrime.securesms.database.model.StoryViewState
import org.thoughtcrime.securesms.dependencies.AppDependencies
import org.thoughtcrime.securesms.groups.GroupId
import org.thoughtcrime.securesms.groups.GroupProtoUtil
import org.thoughtcrime.securesms.groups.GroupsInCommonRepository
import org.thoughtcrime.securesms.groups.LiveGroup
import org.thoughtcrime.securesms.groups.v2.GroupAddMembersResult
import org.thoughtcrime.securesms.groups.v2.GroupManagementRepository
@@ -101,23 +103,8 @@ class ConversationSettingsRepository(
}
fun getGroupsInCommon(recipientId: RecipientId): Observable<List<Recipient>> {
return Recipient.observable(recipientId).flatMapSingle { recipient ->
if (recipient.hasGroupsInCommon) {
Single.fromCallable {
SignalDatabase
.groups
.getPushGroupsContainingMember(recipientId)
.asSequence()
.filter { it.members.contains(Recipient.self().id) }
.map(GroupRecord::recipientId)
.map(Recipient::resolved)
.sortedBy { gr -> gr.getDisplayName(context) }
.toList()
}.observeOn(Schedulers.io())
} else {
Single.just(listOf())
}
}
return GroupsInCommonRepository.getGroupsInCommon(context, recipientId)
.asObservable()
}
fun getGroupMembership(recipientId: RecipientId, consumer: (List<RecipientId>) -> Unit) {

View File

@@ -1,31 +0,0 @@
/*
* Copyright 2023 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.thoughtcrime.securesms.components.webrtc.requests
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.core.Single
import io.reactivex.rxjava3.schedulers.Schedulers
import org.thoughtcrime.securesms.contacts.paged.GroupsInCommon
import org.thoughtcrime.securesms.database.SignalDatabase
import org.thoughtcrime.securesms.recipients.Recipient
import org.thoughtcrime.securesms.recipients.RecipientId
class CallLinkIncomingRequestRepository {
fun getGroupsInCommon(recipientId: RecipientId): Observable<GroupsInCommon> {
return Recipient.observable(recipientId).flatMapSingle { recipient ->
if (recipient.hasGroupsInCommon) {
Single.fromCallable {
val groupsInCommon = SignalDatabase.groups.getGroupsContainingMember(recipient.id, true)
val total = groupsInCommon.size
val names = groupsInCommon.take(2).map { it.title!! }
GroupsInCommon(total, names)
}.observeOn(Schedulers.io())
} else {
Single.just(GroupsInCommon(0, listOf()))
}
}
}
}

View File

@@ -75,7 +75,7 @@ class CallLinkIncomingRequestSheet : ComposeBottomSheetDialogFragment() {
}
private val viewModel by viewModel {
CallLinkIncomingRequestViewModel(recipientId)
CallLinkIncomingRequestViewModel(requireContext(), recipientId)
}
@Composable

View File

@@ -11,15 +11,18 @@ import io.reactivex.rxjava3.core.BackpressureStrategy
import io.reactivex.rxjava3.core.Flowable
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.kotlin.plusAssign
import kotlinx.coroutines.rx3.asObservable
import org.thoughtcrime.securesms.groups.GroupsInCommonRepository
import org.thoughtcrime.securesms.groups.GroupsInCommonSummary
import org.thoughtcrime.securesms.recipients.Recipient
import org.thoughtcrime.securesms.recipients.RecipientId
import org.thoughtcrime.securesms.util.rx.RxStore
class CallLinkIncomingRequestViewModel(
private val context: Context,
private val recipientId: RecipientId
) : ViewModel() {
private val repository = CallLinkIncomingRequestRepository()
private val store = RxStore(CallLinkIncomingRequestState())
private val disposables = CompositeDisposable().apply {
add(store)
@@ -39,10 +42,16 @@ class CallLinkIncomingRequestViewModel(
)
}
disposables += store.update(repository.getGroupsInCommon(recipientId).toFlowable(BackpressureStrategy.LATEST)) { g, s ->
s.copy(groupsInCommon = g.toDisplayText(context))
disposables += store.update(getGroupsInCommon()) { groupsInCommon, state ->
state.copy(groupsInCommon = groupsInCommon.toDisplayText(context))
}
return store.stateFlowable
}
private fun getGroupsInCommon(): Flowable<GroupsInCommonSummary> {
return GroupsInCommonRepository.getGroupsInCommonSummary(context, recipientId)
.asObservable()
.toFlowable(BackpressureStrategy.LATEST)
}
}