Browse Source

Save message properties on config changes

main
M66B 6 years ago
parent
commit
b3a08aae56
3 changed files with 108 additions and 32 deletions
  1. +33
    -31
      app/src/main/java/eu/faircode/email/AdapterMessage.java
  2. +59
    -1
      app/src/main/java/eu/faircode/email/FragmentMessages.java
  3. +16
    -0
      app/src/main/java/eu/faircode/email/Helper.java

+ 33
- 31
app/src/main/java/eu/faircode/email/AdapterMessage.java View File

@ -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<TupleMessageEx, AdapterMess
private Context context;
private LifecycleOwner owner;
private ViewType viewType;
private IProperties properties;
private boolean avatars;
private boolean debug;
private LongSparseArray<Boolean> expanded = new LongSparseArray<>();
private LongSparseArray<Boolean> headers = new LongSparseArray<>();
private LongSparseArray<Boolean> 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<TupleMessageEx, AdapterMess
private void bindTo(final TupleMessageEx message) {
final DB db = DB.getInstance(context);
final boolean show_expanded = (expanded.get(message.id) != null && expanded.get(message.id));
boolean show_headers = (headers.get(message.id) != null && headers.get(message.id));
final boolean show_expanded = properties.isExpanded(message.id);
boolean show_headers = properties.showHeaders(message.id);
pbLoading.setVisibility(View.GONE);
@ -532,10 +528,7 @@ public class AdapterMessage extends PagedListAdapter<TupleMessageEx, AdapterMess
}
private void onShowImages(EntityMessage message) {
if (images.get(message.id) == null)
images.put(message.id, true);
else
images.put(message.id, !images.get(message.id));
properties.setImages(message.id, !properties.showImages(message.id));
Bundle args = new Bundle();
args.putSerializable("message", message);
@ -543,14 +536,11 @@ public class AdapterMessage extends PagedListAdapter<TupleMessageEx, AdapterMess
}
private void onExpandMessage(int pos, EntityMessage message) {
if (expanded.get(message.id) == null)
expanded.put(message.id, true);
else
expanded.put(message.id, !expanded.get(message.id));
notifyItemChanged(pos);
if (expanded.get(message.id))
boolean expanded = !properties.isExpanded(message.id);
properties.setExpanded(message.id, expanded);
if (expanded)
handleExpand(message.id);
notifyItemChanged(pos);
}
private SimpleTask<Spanned> bodyTask = new SimpleTask<Spanned>() {
@ -567,8 +557,8 @@ public class AdapterMessage extends PagedListAdapter<TupleMessageEx, AdapterMess
SpannedString ss = new SpannedString(body);
boolean has_images = (ss.getSpans(0, ss.length(), ImageSpan.class).length > 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<TupleMessageEx, AdapterMess
}
}
if (images.get(message.id) != null && images.get(message.id)) {
if (properties.showImages(message.id)) {
// Get cache folder
File dir = new File(context.getCacheDir(), "images");
dir.mkdir();
@ -785,7 +775,7 @@ public class AdapterMessage extends PagedListAdapter<TupleMessageEx, AdapterMess
private void onMore(final ActionData data) {
boolean inOutbox = EntityFolder.OUTBOX.equals(data.message.folderType);
boolean show_headers = (headers.get(data.message.id) != null && headers.get(data.message.id));
boolean show_headers = properties.showHeaders(data.message.id);
View anchor = bnvActions.findViewById(R.id.action_more);
PopupMenu popupMenu = new PopupMenu(context, anchor);
@ -882,12 +872,9 @@ public class AdapterMessage extends PagedListAdapter<TupleMessageEx, AdapterMess
}
private void onShowHeaders(ActionData data) {
if (headers.get(data.message.id) == null)
headers.put(data.message.id, true);
else
headers.put(data.message.id, !headers.get(data.message.id));
if (headers.get(data.message.id) && data.message.headers == null) {
boolean show_headers = !properties.showHeaders(data.message.id);
properties.setHeaders(data.message.id, show_headers);
if (show_headers) {
grpHeaders.setVisibility(View.VISIBLE);
pbHeaders.setVisibility(View.VISIBLE);
@ -976,8 +963,8 @@ public class AdapterMessage extends PagedListAdapter<TupleMessageEx, AdapterMess
}
@Override
protected void onLoaded(Bundle args, Void data) {
expanded.clear();
protected void onLoaded(Bundle args, Void ignored) {
properties.setExpanded(data.message.id, false);
notifyDataSetChanged();
}
}.load(context, owner, args);
@ -1312,11 +1299,12 @@ public class AdapterMessage extends PagedListAdapter<TupleMessageEx, AdapterMess
}
}
AdapterMessage(Context context, LifecycleOwner owner, ViewType viewType) {
AdapterMessage(Context context, LifecycleOwner owner, ViewType viewType, IProperties properties) {
super(DIFF_CALLBACK);
this.context = context;
this.owner = owner;
this.viewType = viewType;
this.properties = properties;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
@ -1402,4 +1390,18 @@ public class AdapterMessage extends PagedListAdapter<TupleMessageEx, AdapterMess
holder.wire();
}
}
interface IProperties {
void setExpanded(long id, boolean expand);
void setHeaders(long id, boolean show);
void setImages(long id, boolean show);
boolean isExpanded(long id);
boolean showHeaders(long id);
boolean showImages(long id);
}
}

+ 59
- 1
app/src/main/java/eu/faircode/email/FragmentMessages.java View File

@ -42,6 +42,7 @@ import android.widget.TextView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@ -85,6 +86,10 @@ public class FragmentMessages extends FragmentEx {
private AdapterMessage.ViewType viewType;
private LiveData<PagedList<TupleMessageEx>> messages = null;
private List<Long> expanded = new ArrayList<>();
private List<Long> headers = new ArrayList<>();
private List<Long> 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);


+ 16
- 0
app/src/main/java/eu/faircode/email/Helper.java View File

@ -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<Long> list) {
long[] result = new long[list.size()];
for (int i = 0; i < list.size(); i++)
result[i] = list.get(i);
return result;
}
static List<Long> fromLongArray(long[] array) {
List<Long> result = new ArrayList<>();
for (int i = 0; i < array.length; i++)
result.add(array[i]);
return result;
}
}

Loading…
Cancel
Save