save expanded items in persistent storage

This commit is contained in:
kosyak 2024-01-09 23:20:11 +01:00
parent 5f16051cf7
commit 6872b36f74

View file

@ -27,6 +27,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
@ -38,7 +39,6 @@ import eu.siacs.conversations.entities.Conversational;
import eu.siacs.conversations.entities.ListItem;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.ui.ConversationFragment;
import eu.siacs.conversations.ui.StartConversationActivity;
import eu.siacs.conversations.ui.XmppActivity;
import eu.siacs.conversations.ui.util.AvatarWorkerTask;
import eu.siacs.conversations.ui.util.StyledAttributes;
@ -55,6 +55,10 @@ public class ConversationAdapter
private static final int VIEW_TYPE_TAG = 1;
private static final int VIEW_TYPE_CONVERSATION = 2;
private static final String EXPANDED_ACCOUNTS_KEY = "expandedAccounts";
private static final String EXPANDED_TAG_KEY_PREFIX = "expandedTags_";
private final XmppActivity activity;
private final List<Conversation> conversations;
private OnConversationClickListener listener;
@ -64,7 +68,8 @@ public class ConversationAdapter
private ListItem.Tag generalTag;
private List<Object> items = new ArrayList<>();
private Map<Account, Set<ListItem.Tag>> expandedItems = new HashMap<>();
private Map<Account, Set<String>> expandedItems = new HashMap<>();
private boolean expandedItemsRestored = false;
private Map<Account, Map<ListItem.Tag, Set<Conversation>>> groupedItems = new HashMap<>();
@ -76,15 +81,6 @@ public class ConversationAdapter
this.activity = activity;
this.conversations = conversations;
/*prefs = activity.getSharedPreferences("expansionPrefs", Context.MODE_PRIVATE);
Set<String> expandedAccounts = prefs.getStringSet("expandedAccounts", Collections.emptySet());
for (String id : expandedAccounts) {
Set<String> tags = prefs.getStringSet("expandedTags" + id, Collections.emptySet());
Account account = activity.xmppConnectionService.findAccountByUuid(id);
}*/
final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(activity);
allowRelativeTimestamps = !p.getBoolean("always_full_timestamps", activity.getResources().getBoolean(R.bool.always_full_timestamps));
@ -100,6 +96,24 @@ public class ConversationAdapter
List<Account> accounts = activity.xmppConnectionService.getAccounts();
if (!expandedItemsRestored) {
prefs = activity.getSharedPreferences("expansionPrefs", Context.MODE_PRIVATE);
Set<String> expandedAccounts = new HashSet<>(prefs.getStringSet(EXPANDED_ACCOUNTS_KEY, Collections.emptySet()));
if (accounts.size() == 1) {
expandedAccounts.add(accounts.get(0).getUuid());
}
for (String id : expandedAccounts) {
Set<String> expandedTags = new HashSet<>(prefs.getStringSet(EXPANDED_TAG_KEY_PREFIX + id, Collections.emptySet()));
Account account = activity.xmppConnectionService.findAccountByUuid(id);
expandedItems.put(account, expandedTags);
}
expandedItemsRestored = true;
}
for (Account account : accounts) {
if (accounts.size() > 1) {
items.add(account);
@ -110,7 +124,7 @@ public class ConversationAdapter
boolean generalTagAdded = false;
int initialPosition = items.size();
Set<ListItem.Tag> expandedTags = expandedItems.getOrDefault(account, Collections.emptySet());
Set<String> expandedTags = expandedItems.getOrDefault(account, Collections.emptySet());
Map<ListItem.Tag, Set<Conversation>> groupedItems = new HashMap<>();
@ -135,7 +149,7 @@ public class ConversationAdapter
generalTagAdded = true;
}
if (accountExpanded && expandedTags.contains(generalTag)) {
if (accountExpanded && expandedTags.contains(generalTag.getName().toLowerCase(Locale.US))) {
items.add(item);
}
@ -161,7 +175,7 @@ public class ConversationAdapter
List<ListItem.Tag> itemTags = tagsToConversationCache.get(item);
if (itemTags.contains(tag)) {
if (accountExpanded && expandedTags.contains(tag)) {
if (accountExpanded && expandedTags.contains(tag.getName().toLowerCase(Locale.US))) {
items.add(item);
}
@ -270,6 +284,15 @@ public class ConversationAdapter
expandedItems.put(account, new HashSet<>());
}
Set<String> expandedAccounts = new HashSet<>();
for (Account a : expandedItems.keySet()) {
expandedAccounts.add(a.getUuid());
}
prefs.edit().putStringSet(EXPANDED_ACCOUNTS_KEY, expandedAccounts).apply();
notifyDataSetChanged();
});
}
@ -279,16 +302,18 @@ public class ConversationAdapter
viewHolder.text.setText(activity.getString(R.string.contact_tag_with_total, tag.getName(), getChildCount(account, tag)));
viewHolder.text.setBackgroundColor(tag.getColor());
viewHolder.arrow.setRotation(expandedItems.computeIfAbsent(account, a -> new HashSet<>()).contains(tag) ? 180 : 0);
viewHolder.arrow.setRotation(expandedItems.computeIfAbsent(account, a -> new HashSet<>()).contains(tag.getName().toLowerCase(Locale.US)) ? 180 : 0);
viewHolder.itemView.setOnClickListener(v -> {
Set<ListItem.Tag> expandedTags = expandedItems.computeIfAbsent(account, a -> new HashSet<>());
if (expandedTags.contains(tag)) {
expandedTags.remove(tag);
Set<String> expandedTags = expandedItems.computeIfAbsent(account, a -> new HashSet<>());
if (expandedTags.contains(tag.getName().toLowerCase(Locale.US))) {
expandedTags.remove(tag.getName().toLowerCase(Locale.US));
} else {
expandedTags.add(tag);
expandedTags.add(tag.getName().toLowerCase(Locale.US));
}
prefs.edit().putStringSet(EXPANDED_TAG_KEY_PREFIX + account.getUuid(), expandedItems.get(account)).apply();
notifyDataSetChanged();
});
}