From 7e13173bdd2857dbe07faa31d8d5874b8ab0770e Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 14 Feb 2021 09:42:24 +0100 Subject: [PATCH] Simplification --- .../eu/faircode/email/FragmentAccounts.java | 33 ++----------- .../email/FragmentDialogIdentity.java | 47 +++++++++++++++++++ .../eu/faircode/email/FragmentFolders.java | 40 ++-------------- .../eu/faircode/email/FragmentMessages.java | 39 ++------------- 4 files changed, 62 insertions(+), 97 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentAccounts.java b/app/src/main/java/eu/faircode/email/FragmentAccounts.java index ace680720c..d34cdc0c4b 100644 --- a/app/src/main/java/eu/faircode/email/FragmentAccounts.java +++ b/app/src/main/java/eu/faircode/email/FragmentAccounts.java @@ -42,7 +42,6 @@ import androidx.constraintlayout.widget.Group; import androidx.core.content.ContextCompat; import androidx.fragment.app.FragmentTransaction; import androidx.lifecycle.Observer; -import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.preference.PreferenceManager; import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.LinearLayoutManager; @@ -189,33 +188,11 @@ public class FragmentAccounts extends FragmentBase { fabCompose.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { - Bundle args = new Bundle(); - - new SimpleTask() { - @Override - protected EntityFolder onExecute(Context context, Bundle args) { - return DB.getInstance(context).folder().getPrimaryDrafts(); - } - - @Override - protected void onExecuted(Bundle args, EntityFolder drafts) { - if (drafts == null) - return; - - LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext()); - lbm.sendBroadcast( - new Intent(ActivityView.ACTION_VIEW_MESSAGES) - .putExtra("account", drafts.account) - .putExtra("folder", drafts.id) - .putExtra("type", drafts.type)); - } - - @Override - protected void onException(Bundle args, Throwable ex) { - Log.unexpectedError(getParentFragmentManager(), ex); - } - }.execute(FragmentAccounts.this, args, "account:drafts"); - + FragmentDialogIdentity.onDrafts( + getContext(), + getViewLifecycleOwner(), + getParentFragmentManager(), + fabCompose, -1L); return true; } }); diff --git a/app/src/main/java/eu/faircode/email/FragmentDialogIdentity.java b/app/src/main/java/eu/faircode/email/FragmentDialogIdentity.java index a0e6281696..650dccb059 100644 --- a/app/src/main/java/eu/faircode/email/FragmentDialogIdentity.java +++ b/app/src/main/java/eu/faircode/email/FragmentDialogIdentity.java @@ -38,6 +38,7 @@ import androidx.appcompat.app.AlertDialog; import androidx.constraintlayout.widget.Group; import androidx.fragment.app.FragmentManager; import androidx.lifecycle.LifecycleOwner; +import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.preference.PreferenceManager; import com.google.android.material.floatingactionbutton.FloatingActionButton; @@ -188,4 +189,50 @@ public class FragmentDialogIdentity extends FragmentDialogBase { } }.execute(context, owner, args, "identity:compose"); } + + static void onDrafts(Context context, LifecycleOwner owner, FragmentManager manager, FloatingActionButton fabCompose, long account) { + Bundle args = new Bundle(); + args.putLong("account", account); + + new SimpleTask() { + @Override + protected void onPreExecute(Bundle args) { + fabCompose.setEnabled(false); + } + + @Override + protected void onPostExecute(Bundle args) { + fabCompose.setEnabled(true); + } + + @Override + protected EntityFolder onExecute(Context context, Bundle args) { + long account = args.getLong("account"); + + DB db = DB.getInstance(context); + if (account < 0) + return db.folder().getPrimaryDrafts(); + else + return db.folder().getFolderByType(account, EntityFolder.DRAFTS); + } + + @Override + protected void onExecuted(Bundle args, EntityFolder drafts) { + if (drafts == null) + return; + + LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context); + lbm.sendBroadcast( + new Intent(ActivityView.ACTION_VIEW_MESSAGES) + .putExtra("account", drafts.account) + .putExtra("folder", drafts.id) + .putExtra("type", drafts.type)); + } + + @Override + protected void onException(Bundle args, Throwable ex) { + Log.unexpectedError(manager, ex); + } + }.execute(context, owner, args, "view:drafts"); + } } diff --git a/app/src/main/java/eu/faircode/email/FragmentFolders.java b/app/src/main/java/eu/faircode/email/FragmentFolders.java index f12c0976e3..a9d46894eb 100644 --- a/app/src/main/java/eu/faircode/email/FragmentFolders.java +++ b/app/src/main/java/eu/faircode/email/FragmentFolders.java @@ -47,7 +47,6 @@ import androidx.constraintlayout.widget.Group; import androidx.core.content.ContextCompat; import androidx.fragment.app.FragmentTransaction; import androidx.lifecycle.Observer; -import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.preference.PreferenceManager; import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.LinearLayoutManager; @@ -211,40 +210,11 @@ public class FragmentFolders extends FragmentBase { fabCompose.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { - Bundle args = new Bundle(); - args.putLong("account", account); - - new SimpleTask() { - @Override - protected EntityFolder onExecute(Context context, Bundle args) { - long account = args.getLong("account"); - - DB db = DB.getInstance(context); - if (account < 0) - return db.folder().getPrimaryDrafts(); - else - return db.folder().getFolderByType(account, EntityFolder.DRAFTS); - } - - @Override - protected void onExecuted(Bundle args, EntityFolder drafts) { - if (drafts == null) - return; - - LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext()); - lbm.sendBroadcast( - new Intent(ActivityView.ACTION_VIEW_MESSAGES) - .putExtra("account", drafts.account) - .putExtra("folder", drafts.id) - .putExtra("type", drafts.type)); - } - - @Override - protected void onException(Bundle args, Throwable ex) { - Log.unexpectedError(getParentFragmentManager(), ex); - } - }.execute(FragmentFolders.this, args, "folders:drafts"); - + FragmentDialogIdentity.onDrafts( + getContext(), + getViewLifecycleOwner(), + getParentFragmentManager(), + fabCompose, account); return true; } }); diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index fe6af7de6a..69fd826b9e 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -919,40 +919,11 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. fabCompose.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { - Bundle args = new Bundle(); - args.putLong("account", account); - - new SimpleTask() { - @Override - protected EntityFolder onExecute(Context context, Bundle args) { - long account = args.getLong("account"); - - DB db = DB.getInstance(context); - if (account < 0) - return db.folder().getPrimaryDrafts(); - else - return db.folder().getFolderByType(account, EntityFolder.DRAFTS); - } - - @Override - protected void onExecuted(Bundle args, EntityFolder drafts) { - if (drafts == null) - return; - - LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext()); - lbm.sendBroadcast( - new Intent(ActivityView.ACTION_VIEW_MESSAGES) - .putExtra("account", drafts.account) - .putExtra("folder", drafts.id) - .putExtra("type", drafts.type)); - } - - @Override - protected void onException(Bundle args, Throwable ex) { - Log.unexpectedError(getParentFragmentManager(), ex); - } - }.execute(FragmentMessages.this, args, "messages:drafts"); - + FragmentDialogIdentity.onDrafts( + getContext(), + getViewLifecycleOwner(), + getParentFragmentManager(), + fabCompose, -1L); return true; } });