split roster by different accounts

This commit is contained in:
kosyak 2023-12-27 04:09:18 +01:00
parent 54ac152019
commit 510f93ce78
2 changed files with 144 additions and 49 deletions

View file

@ -81,6 +81,7 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -1620,11 +1621,13 @@ public class StartConversationActivity extends XmppActivity implements XmppConne
class ExpandableListItemAdapter extends ListItemAdapter implements ExpandableListAdapter { class ExpandableListItemAdapter extends ListItemAdapter implements ExpandableListAdapter {
private List<ListItem.Tag> tags = new ArrayList<>();
private String generalTagName = activity.getString(R.string.contact_tag_general); 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<ListItem.Tag, List<ListItem>> groupedItems = new HashMap<>(); private List<Object> tagsAndAccounts = new ArrayList<>();
private Set<Account> expandedAccounts = new HashSet<>();
private Map<Account, Map<ListItem.Tag, List<ListItem>>> groupedItems = new HashMap<>();
public ExpandableListItemAdapter(XmppActivity activity, List<ListItem> objects) { public ExpandableListItemAdapter(XmppActivity activity, List<ListItem> objects) {
super(activity, objects); super(activity, objects);
@ -1633,26 +1636,39 @@ public class StartConversationActivity extends XmppActivity implements XmppConne
@Override @Override
public void onChanged() { public void onChanged() {
if (activity instanceof StartConversationActivity && ((StartConversationActivity) activity).groupingEnabled) { if (activity instanceof StartConversationActivity && ((StartConversationActivity) activity).groupingEnabled) {
tags.clear(); tagsAndAccounts.clear();
tags.addAll(mTagsAdapter.tags);
for (int i=tags.size()-1;i>=0;i--) { List<Account> accounts = xmppConnectionService.getAccounts();
if (UIHelper.isStatusTag(activity, tags.get(i))) {
tags.remove(tags.get(i)); List<ListItem.Tag> tags = new ArrayList<>();
for (ListItem.Tag tag : mTagsAdapter.tags) {
if (!UIHelper.isStatusTag(activity, tag)) {
tags.add(tag);
} }
} }
groupedItems.clear(); groupedItems.clear();
boolean generalTagAdded = false; for (Account account : accounts) {
if (accounts.size() > 1) {
tagsAndAccounts.add(account);
}
for (int i=0;i<ExpandableListItemAdapter.super.getCount();i++) { if (expandedAccounts.contains(account) || accounts.size() == 1) {
boolean generalTagAdded = false;
int initialPosition = tagsAndAccounts.size();
tagsAndAccounts.addAll(tags);
Map<ListItem.Tag, List<ListItem>> groupedItems = new HashMap<>();
for (int i = 0; i < ExpandableListItemAdapter.super.getCount(); i++) {
ListItem item = getItem(i); ListItem item = getItem(i);
List<ListItem.Tag> itemTags = item.getTags(activity); List<ListItem.Tag> itemTags = item.getTags(activity);
if (itemTags.size() == 0 || (itemTags.size() == 1 && UIHelper.isStatusTag(activity, itemTags.get(0)))) { if (itemTags.size() == 0 || (itemTags.size() == 1 && UIHelper.isStatusTag(activity, itemTags.get(0)))) {
if (!generalTagAdded) { if (!generalTagAdded) {
tags.add(0, generalTag); tagsAndAccounts.add(initialPosition, generalTag);
generalTagAdded = true; generalTagAdded = true;
} }
@ -1666,13 +1682,20 @@ public class StartConversationActivity extends XmppActivity implements XmppConne
List<ListItem> group = groupedItems.computeIfAbsent(itemTag, tag -> new ArrayList<>()); List<ListItem> group = groupedItems.computeIfAbsent(itemTag, tag -> new ArrayList<>());
group.add(item); group.add(item);
android.util.Log.e("27fd add", group.size() + " " + itemTag.getName());
} }
} }
} }
for (int i=tags.size()-1;i>=0;i--) { for (int i = tagsAndAccounts.size() - 1; i >= initialPosition; i--) {
if (groupedItems.get(tags.get(i)) == null) { ListItem.Tag tag = (ListItem.Tag) tagsAndAccounts.get(i);
tags.remove(tags.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 @Override
public int getGroupCount() { public int getGroupCount() {
return tags.size(); return tagsAndAccounts.size();
} }
@Override @Override
public int getChildrenCount(int groupPosition) { public int getChildrenCount(int groupPosition) {
return groupedItems.get(tags.get(groupPosition)).size(); return getChildsList(groupPosition).size();
} }
@Override @Override
public Object getGroup(int groupPosition) { public Object getGroup(int groupPosition) {
return tags.get(groupPosition); return tagsAndAccounts.get(groupPosition);
} }
@Override @Override
public Object getChild(int groupPosition, int childPosition) { public Object getChild(int groupPosition, int childPosition) {
return groupedItems.get(tags.get(groupPosition)).get(childPosition); return getChildsList(groupPosition).get(childPosition);
} }
@Override @Override
public long getGroupId(int groupPosition) { public long getGroupId(int groupPosition) {
return tags.get(groupPosition).hashCode(); return tagsAndAccounts.get(groupPosition).hashCode();
} }
@Override @Override
public long getChildId(int groupPosition, int childPosition) { 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 @Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 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;
v = activity.getLayoutInflater().inflate(R.layout.contact_group, parent, false);
TextView tv = v.findViewById(R.id.text); TextView tv = v.findViewById(R.id.text);
tv.setText(activity.getString(R.string.contact_tag_with_total, tag.getName(), getChildrenCount(groupPosition))); tv.setText(activity.getString(R.string.contact_tag_with_total, tag.getName(), getChildrenCount(groupPosition)));
tv.setBackgroundColor(tag.getColor()); 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; return v;
} }
@Override @Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 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); return super.getView(super.getPosition(item), convertView, parent);
} }
@ -1736,12 +1777,20 @@ public class StartConversationActivity extends XmppActivity implements XmppConne
@Override @Override
public void onGroupExpanded(int groupPosition) { public void onGroupExpanded(int groupPosition) {
Object tagOrAccount = tagsAndAccounts.get(groupPosition);
if (tagOrAccount instanceof Account) {
expandedAccounts.add((Account) tagOrAccount);
notifyDataSetChanged();
}
} }
@Override @Override
public void onGroupCollapsed(int groupPosition) { public void onGroupCollapsed(int groupPosition) {
Object tagOrAccount = tagsAndAccounts.get(groupPosition);
if (tagOrAccount instanceof Account) {
expandedAccounts.remove((Account) tagOrAccount);
notifyDataSetChanged();
}
} }
@Override @Override
@ -1753,5 +1802,28 @@ public class StartConversationActivity extends XmppActivity implements XmppConne
public long getCombinedGroupId(long groupId) { public long getCombinedGroupId(long groupId) {
return (groupId & 0x7FFFFFFF) << 32; return (groupId & 0x7FFFFFFF) << 32;
} }
private List<ListItem> 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);
}
}
}
} }
} }

View file

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:fontFamily="sans-serif-medium"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_marginStart="40dp"
android:layout_marginEnd="16dp" />
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1px"
android:background="?color_background_tertiary" />
</FrameLayout>