From 52b23befe6acd4e5fc560770c3326ab7d79bd380 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 17 Oct 2018 08:36:25 +0000 Subject: [PATCH] Properly handle transient states --- .../eu/faircode/email/AdapterMessage.java | 88 +++++++++++-------- .../java/eu/faircode/email/SimpleTask.java | 5 ++ 2 files changed, 55 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index 5ecc9fb8..6cc1aa28 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -204,12 +204,6 @@ public class AdapterMessage extends PagedListAdapter 0 && show_expanded ? View.VISIBLE : View.GONE); + db.folder().liveFolders(message.account).removeObservers(owner); db.attachment().liveAttachments(message.id).removeObservers(owner); @@ -382,7 +376,6 @@ public class AdapterMessage extends PagedListAdapter>() { @Override public void onChanged(@Nullable List folders) { - boolean hasTrash = false; - boolean hasArchive = false; - boolean hasUser = false; - - if (folders != null) - for (EntityFolder folder : folders) { - if (EntityFolder.TRASH.equals(folder.type)) - hasTrash = true; - else if (EntityFolder.ARCHIVE.equals(folder.type)) - hasArchive = true; - else if (EntityFolder.USER.equals(folder.type)) - hasUser = true; - } + if (bnvActions.hasTransientState()) { + boolean hasTrash = false; + boolean hasArchive = false; + boolean hasUser = false; + + if (folders != null) + for (EntityFolder folder : folders) { + if (EntityFolder.TRASH.equals(folder.type)) + hasTrash = true; + else if (EntityFolder.ARCHIVE.equals(folder.type)) + hasArchive = true; + else if (EntityFolder.USER.equals(folder.type)) + hasUser = true; + } + + boolean inInbox = EntityFolder.INBOX.equals(message.folderType); + boolean inOutbox = EntityFolder.OUTBOX.equals(message.folderType); + boolean inArchive = EntityFolder.ARCHIVE.equals(message.folderType); + boolean inTrash = EntityFolder.TRASH.equals(message.folderType); - boolean inInbox = EntityFolder.INBOX.equals(message.folderType); - boolean inOutbox = EntityFolder.OUTBOX.equals(message.folderType); - boolean inArchive = EntityFolder.ARCHIVE.equals(message.folderType); - boolean inTrash = EntityFolder.TRASH.equals(message.folderType); + ActionData data = new ActionData(); + data.delete = (inTrash || !hasTrash || inOutbox); + data.message = message; + bnvActions.setTag(data); - ActionData data = new ActionData(); - data.delete = (inTrash || !hasTrash || inOutbox); - data.message = message; - bnvActions.setTag(data); + bnvActions.getMenu().findItem(R.id.action_delete).setVisible((message.uid != null && hasTrash) || (inOutbox && !TextUtils.isEmpty(message.error))); + bnvActions.getMenu().findItem(R.id.action_move).setVisible(message.uid != null && (!inInbox || hasUser)); + bnvActions.getMenu().findItem(R.id.action_archive).setVisible(message.uid != null && !inArchive && hasArchive); + bnvActions.getMenu().findItem(R.id.action_reply).setVisible(message.content && !inOutbox); - bnvActions.getMenu().findItem(R.id.action_delete).setVisible((message.uid != null && hasTrash) || (inOutbox && !TextUtils.isEmpty(message.error))); - bnvActions.getMenu().findItem(R.id.action_move).setVisible(message.uid != null && (!inInbox || hasUser)); - bnvActions.getMenu().findItem(R.id.action_archive).setVisible(message.uid != null && !inArchive && hasArchive); - bnvActions.getMenu().findItem(R.id.action_reply).setVisible(message.content && !inOutbox); + bnvActions.setVisibility(View.VISIBLE); - bnvActions.setVisibility(View.VISIBLE); + bnvActions.setHasTransientState(false); + } } }); @@ -438,7 +436,6 @@ public class AdapterMessage extends PagedListAdapter(); adapter.set(attachments); - grpAttachments.setVisibility(attachments.size() > 0 ? View.VISIBLE : View.GONE); if (message.content) { Bundle args = new Bundle(); @@ -542,6 +539,13 @@ public class AdapterMessage extends PagedListAdapter bodyTask = new SimpleTask() { + @Override + protected void onInit(Bundle args) { + btnImages.setHasTransientState(true); + tvBody.setHasTransientState(true); + pbBody.setHasTransientState(true); + } + @Override protected Spanned onLoad(final Context context, final Bundle args) throws Throwable { TupleMessageEx message = (TupleMessageEx) args.getSerializable("message"); @@ -559,12 +563,20 @@ public class AdapterMessage extends PagedListAdapter implements LifecycleObserver { this.stored = null; owner.getLifecycle().addObserver(this); + onInit(args); + // Run in background thread executor.submit(new Runnable() { @Override @@ -162,6 +164,9 @@ public abstract class SimpleTask implements LifecycleObserver { } } + protected void onInit(Bundle args) { + } + protected T onLoad(Context context, Bundle args) throws Throwable { // Be careful not to access members in outer scopes return null;