Skip to content

Commit a15086d

Browse files
committed
Improve logic to check if all threadIds are present in the selected folder
1 parent db4dff2 commit a15086d

File tree

2 files changed

+63
-36
lines changed

2 files changed

+63
-36
lines changed

app/src/main/java/org/thoughtcrime/securesms/conversationlist/AddToFolderBottomSheet.kt

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,36 @@ class AddToFolderBottomSheet private constructor(private val onDismissListener:
5353
fun onDismiss()
5454
}
5555

56+
enum class ThreadType(val value: Int) {
57+
INDIVIDUAL(1),
58+
GROUP(2),
59+
OTHER(3)
60+
}
61+
5662
private val viewModel by viewModel { ConversationListViewModel(isArchived = false) }
5763

5864
companion object {
5965
private const val ARG_FOLDERS = "argument.folders"
6066
private const val ARG_THREAD_IDS = "argument.thread.ids"
61-
private const val ARG_ARE_ALL_INDIVIDUAL_CHATS = "argument.are.all.individual.chats"
62-
private const val ARG_ARE_ALL_GROUP_CHATS = "argument.are.all.group.chats"
67+
private const val ARG_THREAD_TYPES = "argument.thread.types"
6368

6469
/**
6570
* Shows a bottom sheet that allows a thread to be added to a folder.
6671
*
6772
* @param folders list of available folders to add a thread to
6873
* @param threadIds list of threads that are going to be added
69-
* @param areAllIndividualChats whether the threads are all individual/1:1 chats as opposed to group chats
70-
* @param areAllGroupChats whether the threads are all group chats as opposed to individual/1:1 chats
74+
* @param threadTypes list of ThreadType of threads present in threadIds
7175
*/
7276
@JvmStatic
73-
fun showChatFolderSheet(folders: List<ChatFolderRecord>, threadIds: List<Long>, areAllIndividualChats : Boolean, areAllGroupChats : Boolean, onDismissListener: OnDismissListener): ComposeBottomSheetDialogFragment {
77+
fun showChatFolderSheet(folders: List<ChatFolderRecord>, threadIds: List<Long>, threadTypes: List<Int>, onDismissListener: OnDismissListener): ComposeBottomSheetDialogFragment {
78+
assert(threadIds.size==threadTypes.size) {
79+
"ThreadIds and ThreadTypes should have same size"
80+
}
7481
return AddToFolderBottomSheet(onDismissListener).apply {
7582
arguments = bundleOf(
7683
ARG_FOLDERS to folders,
7784
ARG_THREAD_IDS to threadIds.toLongArray(),
78-
ARG_ARE_ALL_INDIVIDUAL_CHATS to areAllIndividualChats,
79-
ARG_ARE_ALL_GROUP_CHATS to areAllGroupChats,
85+
ARG_THREAD_TYPES to threadTypes.toIntArray(),
8086
)
8187
}
8288
}
@@ -86,13 +92,10 @@ class AddToFolderBottomSheet private constructor(private val onDismissListener:
8692
override fun SheetContent() {
8793
val folders = requireArguments().getParcelableArrayListCompat(ARG_FOLDERS, ChatFolderRecord::class.java)?.filter { it.folderType != ChatFolderRecord.FolderType.ALL }
8894
val threadIds = requireArguments().getLongArray(ARG_THREAD_IDS)?.asList() ?: throw IllegalArgumentException("At least one ThreadId is expected!")
89-
val areAllIndividualChats = requireArguments().getBoolean(ARG_ARE_ALL_INDIVIDUAL_CHATS)
90-
val areAllGroupChats = requireArguments().getBoolean(ARG_ARE_ALL_GROUP_CHATS)
91-
95+
val threadTypes = requireArguments().getIntArray(ARG_THREAD_TYPES)?.asList() ?: throw IllegalArgumentException("At least one ThreadId is expected!")
9296
AddToChatFolderSheetContent(
9397
threadIds = threadIds,
94-
areAllIndividualChats = areAllIndividualChats,
95-
areAllGroupChats = areAllGroupChats,
98+
threadTypes = threadTypes,
9699
folders = remember { folders ?: emptyList() },
97100
onClick = { folder, isAlreadyAdded ->
98101
if (isAlreadyAdded) {
@@ -121,8 +124,7 @@ class AddToFolderBottomSheet private constructor(private val onDismissListener:
121124
@Composable
122125
private fun AddToChatFolderSheetContent(
123126
threadIds: List<Long>,
124-
areAllIndividualChats: Boolean,
125-
areAllGroupChats: Boolean,
127+
threadTypes: List<Int>,
126128
folders: List<ChatFolderRecord>,
127129
onClick: (ChatFolderRecord, Boolean) -> Unit = { _, _ -> },
128130
onCreate: () -> Unit = {}
@@ -147,11 +149,7 @@ private fun AddToChatFolderSheetContent(
147149
.background(color = MaterialTheme.colorScheme.surface, shape = RoundedCornerShape(18.dp))
148150
) {
149151
items(folders) { folder ->
150-
val isIncludedViaChatType = (areAllIndividualChats && folder.showIndividualChats) || (areAllGroupChats && folder.showGroupChats)
151-
val isIncludedExplicitly = folder.includedChats.containsAll(threadIds)
152-
val isExcludedExplicitly = folder.excludedChats.containsAll(threadIds)
153-
154-
val isAlreadyAdded = (isIncludedExplicitly || isIncludedViaChatType) && !isExcludedExplicitly
152+
val isAlreadyAdded = isThreadListAlreadyAdded(folder, threadIds, threadTypes)
155153

156154
Row(
157155
verticalAlignment = Alignment.CenterVertically,
@@ -227,15 +225,36 @@ private fun AddToChatFolderSheetContent(
227225
}
228226
}
229227

228+
fun isThreadListAlreadyAdded(folder: ChatFolderRecord, threadIds: List<Long>, threadTypes: List<Int>): Boolean {
229+
val isAnyExcluded = threadIds.any {
230+
folder.excludedChats.contains(it)
231+
}
232+
if (isAnyExcluded) {
233+
return false
234+
}
235+
236+
if (folder.showIndividualChats) {
237+
return threadTypes.indices.all { index ->
238+
threadTypes[index] == AddToFolderBottomSheet.ThreadType.INDIVIDUAL.value || folder.includedChats.contains(threadIds[index])
239+
}
240+
}
241+
if (folder.showGroupChats) {
242+
return threadTypes.indices.all { index ->
243+
threadTypes[index] == AddToFolderBottomSheet.ThreadType.GROUP.value || folder.includedChats.contains(threadIds[index])
244+
}
245+
}
246+
247+
return folder.includedChats.containsAll(threadIds)
248+
}
249+
230250
@SignalPreview
231251
@Composable
232252
private fun AddToChatFolderSheetContentPreview() {
233253
Previews.BottomSheetPreview {
234254
AddToChatFolderSheetContent(
235255
folders = listOf(ChatFolderRecord(name = "Friends"), ChatFolderRecord(name = "Work")),
236256
threadIds = listOf(1),
237-
areAllIndividualChats = true,
238-
areAllGroupChats = false
257+
threadTypes = listOf(0)
239258
)
240259
}
241260
}

app/src/main/java/org/thoughtcrime/securesms/conversationlist/ConversationListFragment.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,45 +1580,53 @@ public void onEvent(MessageSender.MessageSentEvent event) {
15801580
}
15811581

15821582
private void showAddToFolderBottomSheet(Conversation conversation) {
1583+
boolean isIndividual = conversation.getThreadRecord().getRecipient().isIndividual();
1584+
boolean isGroup = conversation.getThreadRecord().getRecipient().isGroup();
1585+
int type;
1586+
if (isIndividual) {
1587+
type = AddToFolderBottomSheet.ThreadType.INDIVIDUAL.getValue();
1588+
} else if (isGroup) {
1589+
type = AddToFolderBottomSheet.ThreadType.GROUP.getValue();
1590+
} else {
1591+
type = AddToFolderBottomSheet.ThreadType.OTHER.getValue();
1592+
}
15831593
showAddToFolderBottomSheet(
15841594
Collections.singletonList(conversation.getThreadRecord().getThreadId()),
1585-
conversation.getThreadRecord().getRecipient().isIndividual(),
1586-
conversation.getThreadRecord().getRecipient().isGroup()
1595+
Collections.singletonList(type)
15871596
);
15881597
}
15891598

15901599
private void showAddToFolderBottomSheet(Set<Conversation> conversations) {
15911600
List<Long> threadIds = new ArrayList<>();
1592-
boolean areAllIndividualChats = true;
1593-
boolean areAllGroupChats = true;
1601+
List<Integer> threadTypes = new ArrayList<>();
15941602

15951603
for (Conversation conversation : conversations) {
15961604
threadIds.add(conversation.getThreadRecord().getThreadId());
15971605
boolean isIndividual = conversation.getThreadRecord().getRecipient().isIndividual();
15981606
boolean isGroup = conversation.getThreadRecord().getRecipient().isGroup();
1599-
1600-
if (!isIndividual) {
1601-
areAllIndividualChats = false;
1602-
}
1603-
if (!isGroup) {
1604-
areAllGroupChats = false;
1607+
int type;
1608+
if (isIndividual) {
1609+
type = AddToFolderBottomSheet.ThreadType.INDIVIDUAL.getValue();
1610+
} else if (isGroup) {
1611+
type = AddToFolderBottomSheet.ThreadType.GROUP.getValue();
1612+
} else {
1613+
type = AddToFolderBottomSheet.ThreadType.OTHER.getValue();
16051614
}
1615+
threadTypes.add(type);
16061616
}
16071617

16081618
showAddToFolderBottomSheet(
16091619
threadIds,
1610-
areAllIndividualChats,
1611-
areAllGroupChats
1620+
threadTypes
16121621
);
16131622
}
16141623

1615-
private void showAddToFolderBottomSheet(List<Long> threadIds, Boolean areAllIndividualChats, Boolean areAllGroupChats) {
1624+
private void showAddToFolderBottomSheet(List<Long> threadIds, List<Integer> threadTypes) {
16161625
List<ChatFolderRecord> folders = viewModel.getFolders().stream().map(ChatFolderMappingModel::getChatFolder).collect(Collectors.toList());
16171626
AddToFolderBottomSheet.showChatFolderSheet(
16181627
folders,
16191628
threadIds,
1620-
areAllIndividualChats,
1621-
areAllGroupChats,
1629+
threadTypes,
16221630
this::endActionModeIfActive
16231631
).show(getParentFragmentManager(), BottomSheetUtil.STANDARD_BOTTOM_SHEET_FRAGMENT_TAG);
16241632
}

0 commit comments

Comments
 (0)