From 510f93ce783cfcb343e006076faa499e493c1bca Mon Sep 17 00:00:00 2001 From: kosyak Date: Wed, 27 Dec 2023 04:09:18 +0100 Subject: [PATCH] split roster by different accounts --- .../ui/StartConversationActivity.java | 170 +++++++++++++----- src/main/res/layout/contact_account.xml | 23 +++ 2 files changed, 144 insertions(+), 49 deletions(-) create mode 100644 src/main/res/layout/contact_account.xml diff --git a/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java b/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java index 32074a522..7465201da 100644 --- a/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java @@ -81,6 +81,7 @@ import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Function; import java.util.stream.Collectors; @@ -1620,11 +1621,13 @@ public class StartConversationActivity extends XmppActivity implements XmppConne class ExpandableListItemAdapter extends ListItemAdapter implements ExpandableListAdapter { - private List tags = new ArrayList<>(); private String generalTagName = activity.getString(R.string.contact_tag_general); - private ListItem.Tag generalTag = new ListItem.Tag(generalTagName, UIHelper.getColorForName(generalTagName,true)); + private ListItem.Tag generalTag = new ListItem.Tag(generalTagName, UIHelper.getColorForName(generalTagName, true)); - private Map> groupedItems = new HashMap<>(); + private List tagsAndAccounts = new ArrayList<>(); + private Set expandedAccounts = new HashSet<>(); + + private Map>> groupedItems = new HashMap<>(); public ExpandableListItemAdapter(XmppActivity activity, List objects) { super(activity, objects); @@ -1633,46 +1636,66 @@ public class StartConversationActivity extends XmppActivity implements XmppConne @Override public void onChanged() { if (activity instanceof StartConversationActivity && ((StartConversationActivity) activity).groupingEnabled) { - tags.clear(); - tags.addAll(mTagsAdapter.tags); + tagsAndAccounts.clear(); - for (int i=tags.size()-1;i>=0;i--) { - if (UIHelper.isStatusTag(activity, tags.get(i))) { - tags.remove(tags.get(i)); + List accounts = xmppConnectionService.getAccounts(); + + List tags = new ArrayList<>(); + for (ListItem.Tag tag : mTagsAdapter.tags) { + if (!UIHelper.isStatusTag(activity, tag)) { + tags.add(tag); } } groupedItems.clear(); - boolean generalTagAdded = false; - - for (int i=0;i itemTags = item.getTags(activity); - - if (itemTags.size() == 0 || (itemTags.size() == 1 && UIHelper.isStatusTag(activity, itemTags.get(0)))) { - if (!generalTagAdded) { - tags.add(0, generalTag); - generalTagAdded = true; - } - - List group = groupedItems.computeIfAbsent(generalTag, tag -> new ArrayList<>()); - group.add(item); - } else { - for (ListItem.Tag itemTag : itemTags) { - if (UIHelper.isStatusTag(activity, itemTag)) { - continue; - } - - List group = groupedItems.computeIfAbsent(itemTag, tag -> new ArrayList<>()); - group.add(item); - } + for (Account account : accounts) { + if (accounts.size() > 1) { + tagsAndAccounts.add(account); } - } - for (int i=tags.size()-1;i>=0;i--) { - if (groupedItems.get(tags.get(i)) == null) { - tags.remove(tags.get(i)); + if (expandedAccounts.contains(account) || accounts.size() == 1) { + boolean generalTagAdded = false; + int initialPosition = tagsAndAccounts.size(); + + tagsAndAccounts.addAll(tags); + + Map> groupedItems = new HashMap<>(); + + for (int i = 0; i < ExpandableListItemAdapter.super.getCount(); i++) { + ListItem item = getItem(i); + List itemTags = item.getTags(activity); + + if (itemTags.size() == 0 || (itemTags.size() == 1 && UIHelper.isStatusTag(activity, itemTags.get(0)))) { + if (!generalTagAdded) { + tagsAndAccounts.add(initialPosition, generalTag); + generalTagAdded = true; + } + + List group = groupedItems.computeIfAbsent(generalTag, tag -> new ArrayList<>()); + group.add(item); + } else { + for (ListItem.Tag itemTag : itemTags) { + if (UIHelper.isStatusTag(activity, itemTag)) { + continue; + } + + List group = groupedItems.computeIfAbsent(itemTag, tag -> new ArrayList<>()); + group.add(item); + + android.util.Log.e("27fd add", group.size() + " " + itemTag.getName()); + } + } + } + + for (int i = tagsAndAccounts.size() - 1; i >= initialPosition; i--) { + ListItem.Tag tag = (ListItem.Tag) tagsAndAccounts.get(i); + if (groupedItems.get(tag) == null) { + tagsAndAccounts.remove(tagsAndAccounts.lastIndexOf(tag)); + } + } + + ExpandableListItemAdapter.this.groupedItems.put(account, groupedItems); } } } @@ -1682,50 +1705,68 @@ public class StartConversationActivity extends XmppActivity implements XmppConne @Override public int getGroupCount() { - return tags.size(); + return tagsAndAccounts.size(); } @Override public int getChildrenCount(int groupPosition) { - return groupedItems.get(tags.get(groupPosition)).size(); + return getChildsList(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { - return tags.get(groupPosition); + return tagsAndAccounts.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { - return groupedItems.get(tags.get(groupPosition)).get(childPosition); + return getChildsList(groupPosition).get(childPosition); } @Override public long getGroupId(int groupPosition) { - return tags.get(groupPosition).hashCode(); + return tagsAndAccounts.get(groupPosition).hashCode(); } @Override public long getChildId(int groupPosition, int childPosition) { - return groupedItems.get(tags.get(groupPosition)).get(childPosition).getJid().hashCode(); + return getChildsList(groupPosition).get(childPosition).getJid().hashCode(); } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { - ListItem.Tag tag = tags.get(groupPosition); + Object obj = tagsAndAccounts.get(groupPosition); - View v = activity.getLayoutInflater().inflate(R.layout.contact_group, parent, false); + View v; + if (obj instanceof ListItem.Tag) { + ListItem.Tag tag = (ListItem.Tag) obj; - TextView tv = v.findViewById(R.id.text); - tv.setText(activity.getString(R.string.contact_tag_with_total, tag.getName(), getChildrenCount(groupPosition))); - tv.setBackgroundColor(tag.getColor()); + v = activity.getLayoutInflater().inflate(R.layout.contact_group, parent, false); + + TextView tv = v.findViewById(R.id.text); + tv.setText(activity.getString(R.string.contact_tag_with_total, tag.getName(), getChildrenCount(groupPosition))); + tv.setBackgroundColor(tag.getColor()); + } else { + Account acc = (Account) obj; + + v = activity.getLayoutInflater().inflate(R.layout.contact_account, parent, false); + + TextView tv = v.findViewById(R.id.text); + tv.setText(acc.getJid().toString()); + + if (acc.isOnlineAndConnected()) { + tv.setBackgroundColor(StyledAttributes.getColor(activity, R.attr.TextColorOnline)); + } else { + tv.setBackgroundColor(StyledAttributes.getColor(activity, android.R.attr.textColorSecondary)); + } + } return v; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { - ListItem item = groupedItems.get(tags.get(groupPosition)).get(childPosition); + ListItem item = getChildsList(groupPosition).get(childPosition); return super.getView(super.getPosition(item), convertView, parent); } @@ -1736,12 +1777,20 @@ public class StartConversationActivity extends XmppActivity implements XmppConne @Override public void onGroupExpanded(int groupPosition) { - + Object tagOrAccount = tagsAndAccounts.get(groupPosition); + if (tagOrAccount instanceof Account) { + expandedAccounts.add((Account) tagOrAccount); + notifyDataSetChanged(); + } } @Override public void onGroupCollapsed(int groupPosition) { - + Object tagOrAccount = tagsAndAccounts.get(groupPosition); + if (tagOrAccount instanceof Account) { + expandedAccounts.remove((Account) tagOrAccount); + notifyDataSetChanged(); + } } @Override @@ -1753,5 +1802,28 @@ public class StartConversationActivity extends XmppActivity implements XmppConne public long getCombinedGroupId(long groupId) { return (groupId & 0x7FFFFFFF) << 32; } + + private List getChildsList(int groupPosition) { + Object tagOrAccount = tagsAndAccounts.get(groupPosition); + if (tagOrAccount instanceof Account) { + return Collections.emptyList(); + } else { + Account account = null; + + for (int i = groupPosition; i >= 0; i--) { + Object item = tagsAndAccounts.get(i); + if (item instanceof Account) { + account = (Account) item; + break; + } + } + + if (account == null) { + return groupedItems.get(xmppConnectionService.getAccounts().get(0)).get((ListItem.Tag) tagOrAccount); + } else { + return groupedItems.get(account).get((ListItem.Tag) tagOrAccount); + } + } + } } } diff --git a/src/main/res/layout/contact_account.xml b/src/main/res/layout/contact_account.xml new file mode 100644 index 000000000..7d5943ea0 --- /dev/null +++ b/src/main/res/layout/contact_account.xml @@ -0,0 +1,23 @@ + + + + + + + + \ No newline at end of file