From b3a08aae5643efb690aa85e343b371c9ffac39e0 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 17 Oct 2018 07:51:29 +0000 Subject: [PATCH] Save message properties on config changes --- .../eu/faircode/email/AdapterMessage.java | 64 ++++++++++--------- .../eu/faircode/email/FragmentMessages.java | 60 ++++++++++++++++- .../main/java/eu/faircode/email/Helper.java | 16 +++++ 3 files changed, 108 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index f5acdac6..d17b5c4a 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -49,7 +49,6 @@ import android.text.method.LinkMovementMethod; import android.text.style.ImageSpan; import android.text.style.URLSpan; import android.util.Log; -import android.util.LongSparseArray; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; @@ -103,14 +102,11 @@ public class AdapterMessage extends PagedListAdapter expanded = new LongSparseArray<>(); - private LongSparseArray headers = new LongSparseArray<>(); - private LongSparseArray images = new LongSparseArray<>(); - private DateFormat df = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.LONG); enum ViewType {UNIFIED, FOLDER, THREAD, SEARCH} @@ -257,8 +253,8 @@ public class AdapterMessage extends PagedListAdapter bodyTask = new SimpleTask() { @@ -567,8 +557,8 @@ public class AdapterMessage extends PagedListAdapter 0); - boolean show_expanded = (expanded.get(message.id) != null && expanded.get(message.id)); - boolean show_images = (images.get(message.id) != null && images.get(message.id)); + boolean show_expanded = properties.isExpanded(message.id); + boolean show_images = properties.showImages(message.id); btnImages.setVisibility(has_images && show_expanded && !show_images ? View.VISIBLE : View.GONE); pbBody.setVisibility(View.GONE); @@ -603,7 +593,7 @@ public class AdapterMessage extends PagedListAdapter> messages = null; + private List expanded = new ArrayList<>(); + private List headers = new ArrayList<>(); + private List images = new ArrayList<>(); + private BoundaryCallbackMessages searchCallback = null; private ExecutorService executor = Executors.newCachedThreadPool(Helper.backgroundThreadFactory); @@ -170,7 +175,46 @@ public class FragmentMessages extends FragmentEx { else viewType = AdapterMessage.ViewType.SEARCH; - adapter = new AdapterMessage(getContext(), getViewLifecycleOwner(), viewType); + adapter = new AdapterMessage(getContext(), getViewLifecycleOwner(), viewType, new AdapterMessage.IProperties() { + @Override + public void setExpanded(long id, boolean expand) { + if (expand) + expanded.add(id); + else + expanded.remove(id); + } + + @Override + public void setHeaders(long id, boolean show) { + if (show) + headers.add(id); + else + headers.remove(id); + } + + @Override + public void setImages(long id, boolean show) { + if (show) + images.add(id); + else + images.remove(id); + } + + @Override + public boolean isExpanded(long id) { + return expanded.contains(id); + } + + @Override + public boolean showHeaders(long id) { + return headers.contains(id); + } + + @Override + public boolean showImages(long id) { + return images.contains(id); + } + }); rvMessage.setAdapter(adapter); new ItemTouchHelper(new ItemTouchHelper.Callback() { @@ -395,10 +439,24 @@ public class FragmentMessages extends FragmentEx { return view; } + @Override + public void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + outState.putLongArray("expanded", Helper.toLongArray(expanded)); + outState.putLongArray("headers", Helper.toLongArray(headers)); + outState.putLongArray("images", Helper.toLongArray(images)); + } + @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); + if (savedInstanceState != null) { + expanded = Helper.fromLongArray(savedInstanceState.getLongArray("expanded")); + headers = Helper.fromLongArray(savedInstanceState.getLongArray("headers")); + images = Helper.fromLongArray(savedInstanceState.getLongArray("images")); + } + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); grpHintSupport.setVisibility(prefs.getBoolean("app_support", false) ? View.GONE : View.VISIBLE); grpHintActions.setVisibility(prefs.getBoolean("message_actions", false) || viewType != AdapterMessage.ViewType.THREAD ? View.GONE : View.VISIBLE); diff --git a/app/src/main/java/eu/faircode/email/Helper.java b/app/src/main/java/eu/faircode/email/Helper.java index f7f61e6b..7126eda7 100644 --- a/app/src/main/java/eu/faircode/email/Helper.java +++ b/app/src/main/java/eu/faircode/email/Helper.java @@ -53,6 +53,8 @@ import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.text.DecimalFormat; +import java.util.ArrayList; +import java.util.List; import java.util.concurrent.ThreadFactory; import javax.mail.Address; @@ -353,4 +355,18 @@ public class Helper { return true; return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("pro", false); } + + static long[] toLongArray(List list) { + long[] result = new long[list.size()]; + for (int i = 0; i < list.size(); i++) + result[i] = list.get(i); + return result; + } + + static List fromLongArray(long[] array) { + List result = new ArrayList<>(); + for (int i = 0; i < array.length; i++) + result.add(array[i]); + return result; + } }