From 5354e4eaddb8809499ee2695dfd6ac8418298fbe Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 1 Jul 2019 14:29:35 +0200 Subject: [PATCH] Use dialog fragments for account/identitie --- .../eu/faircode/email/FragmentAccount.java | 132 +++++++++--------- .../eu/faircode/email/FragmentIdentity.java | 131 ++++++++--------- 2 files changed, 136 insertions(+), 127 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentAccount.java b/app/src/main/java/eu/faircode/email/FragmentAccount.java index 3d7bafec8c..6b645d95e3 100644 --- a/app/src/main/java/eu/faircode/email/FragmentAccount.java +++ b/app/src/main/java/eu/faircode/email/FragmentAccount.java @@ -21,7 +21,6 @@ package eu.faircode.email; import android.app.NotificationManager; import android.content.Context; -import android.content.DialogInterface; import android.content.Intent; import android.graphics.Color; import android.graphics.drawable.GradientDrawable; @@ -142,6 +141,8 @@ public class FragmentAccount extends FragmentBase { private int color = Color.TRANSPARENT; private static final int REQUEST_COLOR = 1; + private static final int REQUEST_SAVE = 2; + private static final int REQUEST_DELETE = 3; @Override public void onCreate(Bundle savedInstanceState) { @@ -1026,29 +1027,15 @@ public class FragmentAccount extends FragmentBase { @Override protected void onExecuted(Bundle args, Boolean dirty) { - if (dirty) - new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner()) - .setMessage(R.string.title_ask_save) - .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - new Handler().post(new Runnable() { - @Override - public void run() { - scroll.smoothScrollTo(0, btnSave.getBottom()); - } - }); - onSave(false); - } - }) - .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - getFragmentManager().popBackStack(); - } - }) - .show(); - else + if (dirty) { + Bundle aargs = new Bundle(); + aargs.putString("question", getString(R.string.title_ask_save)); + + FragmentDialogAsk fragment = new FragmentDialogAsk(); + fragment.setArguments(aargs); + fragment.setTargetFragment(FragmentAccount.this, REQUEST_SAVE); + fragment.show(getFragmentManager(), "account:save"); + } else getFragmentManager().popBackStack(); } @@ -1257,47 +1244,13 @@ public class FragmentAccount extends FragmentBase { } private void onMenuDelete() { - new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner()) - .setMessage(R.string.title_account_delete) - .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - Bundle args = new Bundle(); - args.putLong("id", id); + Bundle aargs = new Bundle(); + aargs.putString("question", getString(R.string.title_account_delete)); - new SimpleTask() { - @Override - protected void onPostExecute(Bundle args) { - Helper.setViewsEnabled(view, false); - pbWait.setVisibility(View.VISIBLE); - } - - @Override - protected Void onExecute(Context context, Bundle args) { - long id = args.getLong("id"); - - DB db = DB.getInstance(context); - db.account().setAccountTbd(id); - - ServiceSynchronize.reload(context, "delete account"); - - return null; - } - - @Override - protected void onExecuted(Bundle args, Void data) { - getFragmentManager().popBackStack(); - } - - @Override - protected void onException(Bundle args, Throwable ex) { - Helper.unexpectedError(getFragmentManager(), ex); - } - }.execute(FragmentAccount.this, args, "account:delete"); - } - }) - .setNegativeButton(android.R.string.cancel, null) - .show(); + FragmentDialogAsk fragment = new FragmentDialogAsk(); + fragment.setArguments(aargs); + fragment.setTargetFragment(FragmentAccount.this, REQUEST_DELETE); + fragment.show(getFragmentManager(), "account:delete"); } @Override @@ -1317,6 +1270,22 @@ public class FragmentAccount extends FragmentBase { setColor(args.getInt("color")); } break; + case REQUEST_SAVE: + if (resultCode == RESULT_OK) { + new Handler().post(new Runnable() { + @Override + public void run() { + scroll.smoothScrollTo(0, btnSave.getBottom()); + } + }); + onSave(false); + } else + getFragmentManager().popBackStack(); + break; + case REQUEST_DELETE: + if (resultCode == RESULT_OK) + onDelete(); + break; } } @@ -1329,6 +1298,41 @@ public class FragmentAccount extends FragmentBase { vwColor.setBackground(border); } + private void onDelete() { + Bundle args = new Bundle(); + args.putLong("id", id); + + new SimpleTask() { + @Override + protected void onPostExecute(Bundle args) { + Helper.setViewsEnabled(view, false); + pbWait.setVisibility(View.VISIBLE); + } + + @Override + protected Void onExecute(Context context, Bundle args) { + long id = args.getLong("id"); + + DB db = DB.getInstance(context); + db.account().setAccountTbd(id); + + ServiceSynchronize.reload(context, "delete account"); + + return null; + } + + @Override + protected void onExecuted(Bundle args, Void data) { + getFragmentManager().popBackStack(); + } + + @Override + protected void onException(Bundle args, Throwable ex) { + Helper.unexpectedError(getFragmentManager(), ex); + } + }.execute(FragmentAccount.this, args, "account:delete"); + } + private void setFolders(List _folders, EntityAccount account) { List folders = new ArrayList<>(); for (EntityFolder folder : _folders) diff --git a/app/src/main/java/eu/faircode/email/FragmentIdentity.java b/app/src/main/java/eu/faircode/email/FragmentIdentity.java index 916ced60e1..28d7032774 100644 --- a/app/src/main/java/eu/faircode/email/FragmentIdentity.java +++ b/app/src/main/java/eu/faircode/email/FragmentIdentity.java @@ -132,6 +132,8 @@ public class FragmentIdentity extends FragmentBase { private int color = Color.TRANSPARENT; private static final int REQUEST_COLOR = 1; + private static final int REQUEST_SAVE = 2; + private static final int REQUEST_DELETE = 3; @Override public void onCreate(Bundle savedInstanceState) { @@ -759,29 +761,15 @@ public class FragmentIdentity extends FragmentBase { @Override protected void onExecuted(Bundle args, Boolean dirty) { - if (dirty) - new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner()) - .setMessage(R.string.title_ask_save) - .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - new Handler().post(new Runnable() { - @Override - public void run() { - scroll.smoothScrollTo(0, btnSave.getBottom()); - } - }); - onSave(false); - } - }) - .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - getFragmentManager().popBackStack(); - } - }) - .show(); - else + if (dirty) { + Bundle aargs = new Bundle(); + aargs.putString("question", getString(R.string.title_ask_save)); + + FragmentDialogAsk fragment = new FragmentDialogAsk(); + fragment.setArguments(aargs); + fragment.setTargetFragment(FragmentIdentity.this, REQUEST_SAVE); + fragment.show(getFragmentManager(), "identity:save"); + } else getFragmentManager().popBackStack(); } @@ -1009,47 +997,13 @@ public class FragmentIdentity extends FragmentBase { } private void onMenuDelete() { - new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner()) - .setMessage(R.string.title_identity_delete) - .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - Bundle args = new Bundle(); - args.putLong("id", id); + Bundle aargs = new Bundle(); + aargs.putString("question", getString(R.string.title_identity_delete)); - new SimpleTask() { - @Override - protected void onPostExecute(Bundle args) { - Helper.setViewsEnabled(view, false); - pbWait.setVisibility(View.VISIBLE); - } - - @Override - protected Void onExecute(Context context, Bundle args) { - long id = args.getLong("id"); - - DB db = DB.getInstance(context); - db.identity().setIdentityTbd(id); - - ServiceSynchronize.reload(context, "delete identity"); - - return null; - } - - @Override - protected void onExecuted(Bundle args, Void data) { - getFragmentManager().popBackStack(); - } - - @Override - protected void onException(Bundle args, Throwable ex) { - Helper.unexpectedError(getFragmentManager(), ex); - } - }.execute(FragmentIdentity.this, args, "identity:delete"); - } - }) - .setNegativeButton(android.R.string.cancel, null) - .show(); + FragmentDialogAsk fragment = new FragmentDialogAsk(); + fragment.setArguments(aargs); + fragment.setTargetFragment(FragmentIdentity.this, REQUEST_DELETE); + fragment.show(getFragmentManager(), "identity:delete"); } @Override @@ -1069,6 +1023,22 @@ public class FragmentIdentity extends FragmentBase { setColor(args.getInt("color")); } break; + case REQUEST_SAVE: + if (resultCode == RESULT_OK) { + new Handler().post(new Runnable() { + @Override + public void run() { + scroll.smoothScrollTo(0, btnSave.getBottom()); + } + }); + onSave(false); + } else + getFragmentManager().popBackStack(); + break; + case REQUEST_DELETE: + if (resultCode == RESULT_OK) + onDelete(); + break; } } @@ -1080,4 +1050,39 @@ public class FragmentIdentity extends FragmentBase { border.setStroke(1, Helper.resolveColor(getContext(), R.attr.colorSeparator)); vwColor.setBackground(border); } + + private void onDelete() { + Bundle args = new Bundle(); + args.putLong("id", id); + + new SimpleTask() { + @Override + protected void onPostExecute(Bundle args) { + Helper.setViewsEnabled(view, false); + pbWait.setVisibility(View.VISIBLE); + } + + @Override + protected Void onExecute(Context context, Bundle args) { + long id = args.getLong("id"); + + DB db = DB.getInstance(context); + db.identity().setIdentityTbd(id); + + ServiceSynchronize.reload(context, "delete identity"); + + return null; + } + + @Override + protected void onExecuted(Bundle args, Void data) { + getFragmentManager().popBackStack(); + } + + @Override + protected void onException(Bundle args, Throwable ex) { + Helper.unexpectedError(getFragmentManager(), ex); + } + }.execute(FragmentIdentity.this, args, "identity:delete"); + } }