From ba242ac0b674a1d78abae879ae01f67ae377861f Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 1 Jul 2019 16:46:40 +0200 Subject: [PATCH] Use dialog fragment to select account --- .../eu/faircode/email/DialogFragmentEx.java | 16 +++- .../eu/faircode/email/FragmentMessages.java | 94 +++++++++++-------- 2 files changed, 65 insertions(+), 45 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/DialogFragmentEx.java b/app/src/main/java/eu/faircode/email/DialogFragmentEx.java index 78bd558762..0526d2cbfd 100644 --- a/app/src/main/java/eu/faircode/email/DialogFragmentEx.java +++ b/app/src/main/java/eu/faircode/email/DialogFragmentEx.java @@ -10,6 +10,8 @@ import androidx.fragment.app.Fragment; import static android.app.Activity.RESULT_CANCELED; public class DialogFragmentEx extends DialogFragment { + private boolean once = false; + @Override public void onDismiss(@NonNull DialogInterface dialog) { super.onDismiss(dialog); @@ -17,11 +19,15 @@ public class DialogFragmentEx extends DialogFragment { } protected void sendResult(int result) { - Fragment target = getTargetFragment(); - if (target != null) { - Intent data = new Intent(); - data.putExtra("args", getArguments()); - target.onActivityResult(getTargetRequestCode(), result, null); + if (!once) { + once = true; + Fragment target = getTargetFragment(); + Log.i("Dialog target=" + target + " result=" + result); + if (target != null) { + Intent data = new Intent(); + data.putExtra("args", getArguments()); + target.onActivityResult(getTargetRequestCode(), result, data); + } } } } diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index 5f81a235d6..a5b838a4d5 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -81,7 +81,6 @@ import androidx.appcompat.widget.PopupMenu; import androidx.appcompat.widget.SearchView; import androidx.constraintlayout.widget.Group; import androidx.documentfile.provider.DocumentFile; -import androidx.fragment.app.DialogFragment; import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; @@ -254,6 +253,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. private static final int REQUEST_MESSAGES_MOVE = 16; static final int REQUEST_PRINT = 17; private static final int REQUEST_SEARCH = 18; + private static final int REQUEST_ACCOUNT = 19; static final String ACTION_STORE_RAW = BuildConfig.APPLICATION_ID + ".STORE_RAW"; static final String ACTION_STORE_ATTACHMENT = BuildConfig.APPLICATION_ID + ".STORE_ATTACHMENT"; @@ -2274,37 +2274,10 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. ib.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { - Bundle args = new Bundle(); - - new SimpleTask>() { - @Override - protected List onExecute(Context context, Bundle args) { - DB db = DB.getInstance(context); - return db.account().getSynchronizingAccounts(); - } - - @Override - protected void onExecuted(Bundle args, List accounts) { - final ArrayAdapter adapter = - new ArrayAdapter<>(getContext(), R.layout.spinner_item1, android.R.id.text1, accounts); - - new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner()) - .setAdapter(adapter, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - dialog.dismiss(); - EntityAccount account = adapter.getItem(which); - onMenuFolders(account.id); - } - }) - .show(); - } - - @Override - protected void onException(Bundle args, Throwable ex) { - Helper.unexpectedError(getFragmentManager(), ex); - } - }.execute(FragmentMessages.this, args, "messages:accounts"); + FragmentDialogAccount fragment = new FragmentDialogAccount(); + fragment.setArguments(new Bundle()); + fragment.setTargetFragment(FragmentMessages.this, REQUEST_ACCOUNT); + fragment.show(getFragmentManager(), "messages:accounts"); return true; } }); @@ -3376,10 +3349,8 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. } break; case REQUEST_PRINT: - if (resultCode == RESULT_OK && data != null) { - Bundle args = data.getBundleExtra("args"); - onPrint(args.getLong("id")); - } + if (resultCode == RESULT_OK && data != null) + onPrint(data.getBundleExtra("args")); break; case REQUEST_SEARCH: if (resultCode == RESULT_OK && data != null) { @@ -3389,6 +3360,12 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. args.getLong("folder"), true, args.getString("query")); } break; + case REQUEST_ACCOUNT: + if (resultCode == RESULT_OK && data != null) { + Bundle args = data.getBundleExtra("args"); + onMenuFolders(args.getLong("account")); + } + break; } } @@ -4118,9 +4095,9 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. private WebView printWebView = null; - private void onPrint(long id) { - Bundle args = new Bundle(); - args.putLong("id", id); + private void onPrint(Bundle args) { + Bundle pargs = new Bundle(); + pargs.putLong("id", args.getLong("id")); new SimpleTask() { @Override @@ -4224,7 +4201,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. protected void onException(Bundle args, Throwable ex) { Helper.unexpectedError(getFragmentManager(), ex); } - }.execute(this, args, "message:print"); + }.execute(this, pargs, "message:print"); } static void search( @@ -4361,6 +4338,43 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. } } + public static class FragmentDialogAccount extends DialogFragmentEx { + @NonNull + @Override + public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { + final ArrayAdapter adapter = new ArrayAdapter<>(getContext(), R.layout.spinner_item1, android.R.id.text1); + + new SimpleTask>() { + @Override + protected List onExecute(Context context, Bundle args) { + DB db = DB.getInstance(context); + return db.account().getSynchronizingAccounts(); + } + + @Override + protected void onExecuted(Bundle args, List accounts) { + adapter.addAll(accounts); + } + + @Override + protected void onException(Bundle args, Throwable ex) { + Helper.unexpectedError(getFragmentManager(), ex); + } + }.execute(getContext(), getActivity(), new Bundle(), "messages:accounts"); + + return new AlertDialog.Builder(getContext()) + .setAdapter(adapter, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + EntityAccount account = adapter.getItem(which); + getArguments().putLong("account", account.id); + sendResult(RESULT_OK); + } + }) + .create(); + } + } + public static class FragmentDialogError extends DialogFragmentEx { @NonNull @Override