From 8a1f1a14a7bbcb399d1cc0765498f92347d0713b Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 17 Oct 2018 08:15:29 +0000 Subject: [PATCH] Auto expand --- .../eu/faircode/email/AdapterMessage.java | 45 ----------- .../eu/faircode/email/FragmentMessages.java | 81 ++++++++++++++++++- 2 files changed, 79 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index d17b5c4a..5ecc9fb8 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -538,8 +538,6 @@ public class AdapterMessage extends PagedListAdapter() { - @Override - protected Void onLoad(Context context, Bundle args) { - long id = args.getLong("id"); - - DB db = DB.getInstance(context); - try { - db.beginTransaction(); - - EntityMessage message = db.message().getMessage(id); - EntityFolder folder = db.folder().getFolder(message.folder); - - if (!EntityFolder.OUTBOX.equals(folder.type)) { - if (!message.content) - EntityOperation.queue(db, message, EntityOperation.BODY); - - if (!message.ui_seen) { - db.message().setMessageUiSeen(message.id, true); - EntityOperation.queue(db, message, EntityOperation.SEEN, true); - } - } - - db.setTransactionSuccessful(); - } finally { - db.endTransaction(); - } - - EntityOperation.process(context); - - return null; - } - - @Override - protected void onException(Bundle args, Throwable ex) { - Helper.unexpectedError(context, ex); - } - }.load(context, owner, args); - } - private static final DiffUtil.ItemCallback DIFF_CALLBACK = new DiffUtil.ItemCallback() { @Override diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index 67d81d38..14d4bb9d 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -86,6 +86,7 @@ public class FragmentMessages extends FragmentEx { private AdapterMessage.ViewType viewType; private LiveData> messages = null; + private boolean autoExpand = true; private List expanded = new ArrayList<>(); private List headers = new ArrayList<>(); private List images = new ArrayList<>(); @@ -178,9 +179,10 @@ public class FragmentMessages extends FragmentEx { adapter = new AdapterMessage(getContext(), getViewLifecycleOwner(), viewType, new AdapterMessage.IProperties() { @Override public void setExpanded(long id, boolean expand) { - if (expand) + if (expand) { expanded.add(id); - else + handleExpand(id); + } else expanded.remove(id); } @@ -797,6 +799,38 @@ public class FragmentMessages extends FragmentEx { return; } + if (viewType == AdapterMessage.ViewType.THREAD && autoExpand) { + autoExpand = false; + + int count = 0; + int unseen = 0; + TupleMessageEx single = null; + TupleMessageEx see = null; + for (int i = 0; i < messages.size(); i++) { + TupleMessageEx message = messages.get(i); + if (!EntityFolder.ARCHIVE.equals(message.folderType)) { + count++; + single = message; + if (!message.ui_seen) { + unseen++; + see = message; + } + } + } + + TupleMessageEx expand = null; + if (count == 1) + expand = single; + else if (unseen == 1) + expand = see; + + if (expand != null) { + expanded.add(expand.id); + if (!expand.ui_seen) + handleExpand(expand.id); + } + } + Log.i(Helper.TAG, "Submit messages=" + messages.size()); adapter.submitList(messages); @@ -818,4 +852,47 @@ public class FragmentMessages extends FragmentEx { }); } + + private void handleExpand(long id) { + Bundle args = new Bundle(); + args.putLong("id", id); + + new SimpleTask() { + @Override + protected Void onLoad(Context context, Bundle args) { + long id = args.getLong("id"); + + DB db = DB.getInstance(context); + try { + db.beginTransaction(); + + EntityMessage message = db.message().getMessage(id); + EntityFolder folder = db.folder().getFolder(message.folder); + + if (!EntityFolder.OUTBOX.equals(folder.type)) { + if (!message.content) + EntityOperation.queue(db, message, EntityOperation.BODY); + + if (!message.ui_seen) { + db.message().setMessageUiSeen(message.id, true); + EntityOperation.queue(db, message, EntityOperation.SEEN, true); + } + } + + db.setTransactionSuccessful(); + } finally { + db.endTransaction(); + } + + EntityOperation.process(context); + + return null; + } + + @Override + protected void onException(Bundle args, Throwable ex) { + Helper.unexpectedError(getContext(), ex); + } + }.load(this, args); + } }