From 19c8fbce7a8464f51997bbbdcf9e6b18ac607ef8 Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 30 Jun 2019 23:05:00 +0200 Subject: [PATCH] Use dialog fragments for answers and contacts --- .../eu/faircode/email/FragmentAnswer.java | 39 +++++---- .../eu/faircode/email/FragmentContacts.java | 84 ++++++++++++------- 2 files changed, 78 insertions(+), 45 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentAnswer.java b/app/src/main/java/eu/faircode/email/FragmentAnswer.java index 6c3325a61a..d8ef69850a 100644 --- a/app/src/main/java/eu/faircode/email/FragmentAnswer.java +++ b/app/src/main/java/eu/faircode/email/FragmentAnswer.java @@ -19,6 +19,7 @@ package eu.faircode.email; Copyright 2018-2019 by Marcel Bokhorst (M66B) */ +import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; @@ -34,7 +35,9 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; import androidx.constraintlayout.widget.Group; +import androidx.fragment.app.DialogFragment; import com.google.android.material.bottomnavigation.BottomNavigationView; @@ -80,20 +83,7 @@ public class FragmentAnswer extends FragmentBase { ibInfo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - Spanned spanned = HtmlHelper.fromHtml("

" + - getString(R.string.title_answer_template_name) + - "
" + - getString(R.string.title_answer_template_email) + - "

"); - - View dview = LayoutInflater.from(getContext()).inflate(R.layout.dialog_message, null); - TextView tvMessage = dview.findViewById(R.id.tvMessage); - - tvMessage.setText(spanned); - - new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner()) - .setView(dview) - .show(); + new FragmentInfo().show(getFragmentManager(), "rule:info"); } }); @@ -249,4 +239,25 @@ public class FragmentAnswer extends FragmentBase { } }.execute(this, args, "answer:save"); } + + public static class FragmentInfo extends DialogFragment { + @NonNull + @Override + public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { + Spanned spanned = HtmlHelper.fromHtml("

" + + getString(R.string.title_answer_template_name) + + "
" + + getString(R.string.title_answer_template_email) + + "

"); + + View dview = LayoutInflater.from(getContext()).inflate(R.layout.dialog_message, null); + TextView tvMessage = dview.findViewById(R.id.tvMessage); + + tvMessage.setText(spanned); + + return new AlertDialog.Builder(getContext()) + .setView(dview) + .create(); + } + } } diff --git a/app/src/main/java/eu/faircode/email/FragmentContacts.java b/app/src/main/java/eu/faircode/email/FragmentContacts.java index 7fdc58a2de..98ec8e9fd0 100644 --- a/app/src/main/java/eu/faircode/email/FragmentContacts.java +++ b/app/src/main/java/eu/faircode/email/FragmentContacts.java @@ -19,6 +19,7 @@ package eu.faircode.email; Copyright 2018-2019 by Marcel Bokhorst (M66B) */ +import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; @@ -36,8 +37,10 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; import androidx.appcompat.widget.SearchView; import androidx.constraintlayout.widget.Group; +import androidx.fragment.app.DialogFragment; import androidx.lifecycle.Observer; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -160,7 +163,7 @@ public class FragmentContacts extends FragmentBase { onMenuHelp(); return true; case R.id.menu_delete: - onMenuDelete(); + new FragmentDelete().show(getFragmentManager(), "contacts:delete"); return true; default: return super.onOptionsItemSelected(item); @@ -171,39 +174,58 @@ public class FragmentContacts extends FragmentBase { startActivity(getIntentHelp()); } - private void onMenuDelete() { - final View dview = LayoutInflater.from(getContext()).inflate(R.layout.dialog_message, null); - final TextView tvMessage = dview.findViewById(R.id.tvMessage); - - tvMessage.setText(getText(R.string.title_delete_contacts)); - - new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner()) - .setView(dview) - .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - new SimpleTask() { - @Override - protected Void onExecute(Context context, Bundle args) { - int count = DB.getInstance(context).contact().clearContacts(); - Log.i("Cleared contacts=" + count); - return null; - } - - @Override - protected void onException(Bundle args, Throwable ex) { - Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex); - } - }.execute(getContext(), getViewLifecycleOwner(), new Bundle(), "setup:privacy"); - } - }) - .setNegativeButton(android.R.string.cancel, null) - .show(); - } - private static Intent getIntentHelp() { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("https://github.com/M66B/open-source-email/blob/master/FAQ.md#user-content-faq84")); return intent; } + + public static class FragmentDelete extends DialogFragment { + private TwoStateOwner owner = new TwoStateOwner("contacts:delete"); + + @NonNull + @Override + public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { + final View dview = LayoutInflater.from(getContext()).inflate(R.layout.dialog_message, null); + final TextView tvMessage = dview.findViewById(R.id.tvMessage); + + tvMessage.setText(getText(R.string.title_delete_contacts)); + + return new AlertDialog.Builder(getContext()) + .setView(dview) + .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + new SimpleTask() { + @Override + protected Void onExecute(Context context, Bundle args) { + DB db = DB.getInstance(context); + int count = db.contact().clearContacts(); + Log.i("Cleared contacts=" + count); + return null; + } + + @Override + protected void onException(Bundle args, Throwable ex) { + Helper.unexpectedError(getContext(), owner, ex); + } + }.execute(getContext(), owner, new Bundle(), "contacts:delete"); + } + }) + .setNegativeButton(android.R.string.cancel, null) + .create(); + } + + @Override + public void onStart() { + super.onStart(); + owner.resume(); + } + + @Override + public void onDestroyView() { + owner.destroy(); + super.onDestroyView(); + } + } }