From 8f0f809c865daae886f21f731d39fda78428e36d Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 17 Dec 2018 08:44:05 +0100 Subject: [PATCH] Auto discard empty drafts --- .../eu/faircode/email/FragmentCompose.java | 42 +++++++++++++++---- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 64a918e149..3baa4f4389 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -70,6 +70,7 @@ import android.widget.Toast; import com.google.android.material.bottomnavigation.BottomNavigationView; import com.google.android.material.snackbar.Snackbar; +import org.jsoup.Jsoup; import org.openintents.openpgp.OpenPgpError; import org.openintents.openpgp.util.OpenPgpApi; import org.openintents.openpgp.util.OpenPgpServiceConnection; @@ -890,7 +891,9 @@ public class FragmentCompose extends FragmentEx { } private void handleExit() { - if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)) + if (isEmpty()) + onAction(R.id.action_delete); + else new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner()) .setMessage(R.string.title_ask_discard) .setPositiveButton(R.string.title_yes, new DialogInterface.OnClickListener() { @@ -931,6 +934,7 @@ public class FragmentCompose extends FragmentEx { args.putString("cc", etCc.getText().toString()); args.putString("bcc", etBcc.getText().toString()); args.putString("subject", etSubject.getText().toString()); + args.putBoolean("empty", isEmpty()); Spannable spannable = etBody.getText(); UnderlineSpan[] uspans = spannable.getSpans(0, spannable.length(), UnderlineSpan.class); @@ -943,6 +947,24 @@ public class FragmentCompose extends FragmentEx { actionLoader.load(this, args); } + private boolean isEmpty() { + if (!TextUtils.isEmpty(etExtra.getText().toString().trim())) + return false; + if (!TextUtils.isEmpty(etTo.getText().toString().trim())) + return false; + if (!TextUtils.isEmpty(etCc.getText().toString().trim())) + return false; + if (!TextUtils.isEmpty(etBcc.getText().toString().trim())) + return false; + if (!TextUtils.isEmpty(etSubject.getText().toString().trim())) + return false; + if (!TextUtils.isEmpty(Jsoup.parse(Html.toHtml(etBody.getText())).text().trim())) + return false; + if (rvAttachment.getAdapter().getItemCount() > 0) + return false; + return true; + } + private static EntityAttachment addAttachment(Context context, long id, Uri uri, boolean image) throws IOException { EntityAttachment attachment = new EntityAttachment(); @@ -1518,6 +1540,7 @@ public class FragmentCompose extends FragmentEx { String bcc = args.getString("bcc"); String subject = args.getString("subject"); String body = args.getString("body"); + boolean empty = args.getBoolean("empty"); EntityMessage draft; @@ -1605,12 +1628,14 @@ public class FragmentCompose extends FragmentEx { if (action == R.id.action_delete) { EntityOperation.queue(db, draft, EntityOperation.DELETE); - Handler handler = new Handler(Looper.getMainLooper()); - handler.post(new Runnable() { - public void run() { - Toast.makeText(context, R.string.title_draft_deleted, Toast.LENGTH_LONG).show(); - } - }); + if (!empty) { + Handler handler = new Handler(Looper.getMainLooper()); + handler.post(new Runnable() { + public void run() { + Toast.makeText(context, R.string.title_draft_deleted, Toast.LENGTH_LONG).show(); + } + }); + } } else if (action == R.id.action_save || action == R.id.menu_encrypt) { EntityOperation.queue(db, draft, EntityOperation.ADD); @@ -1852,7 +1877,8 @@ public class FragmentCompose extends FragmentEx { private ActivityBase.IBackPressedListener onBackPressedListener = new ActivityBase.IBackPressedListener() { @Override public boolean onBackPressed() { - handleExit(); + if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)) + handleExit(); return true; } };