From 6872b36f74c40769f18354464317d62d19cf9378 Mon Sep 17 00:00:00 2001 From: kosyak Date: Tue, 9 Jan 2024 23:20:11 +0100 Subject: [PATCH] save expanded items in persistent storage --- .../ui/adapter/ConversationAdapter.java | 63 +++++++++++++------ 1 file changed, 44 insertions(+), 19 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/ConversationAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/ConversationAdapter.java index 3deb49146..735460bf4 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/ConversationAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/ConversationAdapter.java @@ -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 conversations; private OnConversationClickListener listener; @@ -64,7 +68,8 @@ public class ConversationAdapter private ListItem.Tag generalTag; private List items = new ArrayList<>(); - private Map> expandedItems = new HashMap<>(); + private Map> expandedItems = new HashMap<>(); + private boolean expandedItemsRestored = false; private Map>> groupedItems = new HashMap<>(); @@ -76,15 +81,6 @@ public class ConversationAdapter this.activity = activity; this.conversations = conversations; - /*prefs = activity.getSharedPreferences("expansionPrefs", Context.MODE_PRIVATE); - Set expandedAccounts = prefs.getStringSet("expandedAccounts", Collections.emptySet()); - for (String id : expandedAccounts) { - Set 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 accounts = activity.xmppConnectionService.getAccounts(); + + if (!expandedItemsRestored) { + prefs = activity.getSharedPreferences("expansionPrefs", Context.MODE_PRIVATE); + Set 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 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 expandedTags = expandedItems.getOrDefault(account, Collections.emptySet()); + Set expandedTags = expandedItems.getOrDefault(account, Collections.emptySet()); Map> 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 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 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 expandedTags = expandedItems.computeIfAbsent(account, a -> new HashSet<>()); - if (expandedTags.contains(tag)) { - expandedTags.remove(tag); + Set 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(); }); }