support conversations grouping on ShareWith screen

This commit is contained in:
kosyak 2024-01-10 00:27:08 +01:00
parent 31ac27f5ec
commit f50bb56ad7

View file

@ -13,12 +13,18 @@ import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Bookmark;
import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.ListItem;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.ui.adapter.ConversationAdapter;
import eu.siacs.conversations.xmpp.Jid;
@ -48,6 +54,8 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer
private ConversationAdapter mAdapter;
private final List<Conversation> mConversations = new ArrayList<>();
private final List<ListItem.Tag> tags = new ArrayList<>();
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
@ -95,7 +103,7 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer
setTitle(getString(R.string.title_activity_sharewith));
RecyclerView mListView = findViewById(R.id.choose_conversation_list);
mAdapter = new ConversationAdapter(this, this.mConversations, new ArrayList<>());
mAdapter = new ConversationAdapter(this, this.mConversations, this.tags);
mListView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
mListView.setAdapter(mAdapter);
mAdapter.setConversationClickListener((view, conversation) -> share(conversation));
@ -216,6 +224,57 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer
public void refreshUiReal() {
//TODO inject desired order to not resort on refresh
xmppConnectionService.populateWithOrderedConversations(mConversations, this.share != null && this.share.uris.size() == 0, false);
refreshTags();
mAdapter.notifyDataSetChanged();
}
private void refreshTags() {
this.mAdapter.setGroupingEnabled(xmppConnectionService.getPreferences().getBoolean("conversationsGroupByTags", false));
if (mAdapter.isGroupingEnabled()) {
List<ListItem.Tag> tags = new ArrayList<>();
final List<Account> accounts = xmppConnectionService.getAccounts();
for (final Account account : accounts) {
if (account.isEnabled()) {
for (Contact contact : account.getRoster().getContacts()) {
if (contact.showInContactList()) {
tags.addAll(contact.getTags(this));
}
}
for (Bookmark bookmark : account.getBookmarks()) {
tags.addAll(bookmark.getTags(this));
}
}
}
Comparator<Map.Entry<ListItem.Tag, Integer>> sortTagsBy = Map.Entry.comparingByValue(Comparator.reverseOrder());
sortTagsBy = sortTagsBy.thenComparing(entry -> entry.getKey().getName());
this.tags.clear();
this.tags.addAll(
tags.stream()
.collect(Collectors.toMap((x) -> x, (t) -> 1, (c1, c2) -> c1 + c2))
.entrySet().stream()
.sorted(sortTagsBy)
.map(e -> e.getKey()).collect(Collectors.toList())
);
ListItem.Tag channelTag = null;
int channelTagIndex = 0;
for (ListItem.Tag tag : this.tags) {
if (tag.getName().equals("Channel")) {
channelTag = tag;
break;
}
channelTagIndex++;
}
if (channelTag != null) {
this.tags.remove(channelTagIndex);
this.tags.add(0, channelTag);
}
}
}
}