From f73476bdfc36bb7cf55d4609c704d67d85d41c30 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 11 Sep 2019 14:03:59 +0200 Subject: [PATCH] Added dialog life cycle --- .../eu/faircode/email/AdapterMessage.java | 10 ++-- .../eu/faircode/email/FragmentAccount.java | 8 ++-- .../eu/faircode/email/FragmentAnswer.java | 2 +- .../eu/faircode/email/FragmentCompose.java | 42 ++++++++--------- .../eu/faircode/email/FragmentContacts.java | 2 +- .../eu/faircode/email/FragmentDialogEx.java | 47 +++++++++++++++---- .../faircode/email/FragmentDialogFolder.java | 4 +- .../email/FragmentDialogMarkdown.java | 2 +- .../eu/faircode/email/FragmentFolder.java | 4 +- .../eu/faircode/email/FragmentIdentity.java | 6 +-- .../eu/faircode/email/FragmentMessages.java | 5 +- .../eu/faircode/email/FragmentOperations.java | 2 +- .../java/eu/faircode/email/FragmentOrder.java | 3 +- .../eu/faircode/email/FragmentQuickSetup.java | 2 +- .../java/eu/faircode/email/FragmentRule.java | 8 ++-- .../main/java/eu/faircode/email/Helper.java | 2 +- 16 files changed, 89 insertions(+), 60 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index 19d06f0b4f..d40a9dd15b 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -3787,7 +3787,7 @@ public class AdapterMessage extends RecyclerView.Adapter _folders, EntityAccount account) { diff --git a/app/src/main/java/eu/faircode/email/FragmentAnswer.java b/app/src/main/java/eu/faircode/email/FragmentAnswer.java index 5e053fbfef..6d08313322 100644 --- a/app/src/main/java/eu/faircode/email/FragmentAnswer.java +++ b/app/src/main/java/eu/faircode/email/FragmentAnswer.java @@ -262,7 +262,7 @@ public class FragmentAnswer extends FragmentBase { protected void onException(Bundle args, Throwable ex) { Helper.unexpectedError(getFragmentManager(), ex); } - }.execute(FragmentAnswer.this, args, "answer:delete"); + }.execute(this, args, "answer:delete"); } public static class FragmentInfo extends FragmentDialogEx { diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 381a127278..8376092ca0 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -650,7 +650,7 @@ public class FragmentCompose extends FragmentBase { protected void onException(Bundle args, Throwable ex) { Helper.unexpectedError(getFragmentManager(), ex); } - }.execute(FragmentCompose.this, args, "compose:refdelete"); + }.execute(this, args, "compose:refdelete"); } private void onReferenceEdit() { @@ -733,7 +733,7 @@ public class FragmentCompose extends FragmentBase { protected void onException(Bundle args, Throwable ex) { Helper.unexpectedError(getFragmentManager(), ex); } - }.execute(FragmentCompose.this, args, "compose:refedit"); + }.execute(this, args, "compose:refedit"); } private void onReferenceImages() { @@ -1655,7 +1655,7 @@ public class FragmentCompose extends FragmentBase { protected void onException(Bundle args, Throwable ex) { Helper.unexpectedError(getFragmentManager(), ex); } - }.execute(getContext(), getViewLifecycleOwner(), args, "compose:picked"); + }.execute(this, args, "compose:picked"); } private void onAnswerSelected(Bundle args) { @@ -2946,7 +2946,7 @@ public class FragmentCompose extends FragmentBase { protected void onException(Bundle args, Throwable ex) { Helper.unexpectedError(getFragmentManager(), ex); } - }.execute(FragmentCompose.this, args, "compose:show"); + }.execute(this, args, "compose:show"); } private void showDraft(final EntityMessage draft) { @@ -3070,7 +3070,7 @@ public class FragmentCompose extends FragmentBase { protected void onException(Bundle args, Throwable ex) { Helper.unexpectedError(getFragmentManager(), ex); } - }.execute(FragmentCompose.this, args, "compose:show"); + }.execute(this, args, "compose:show"); } private Html.ImageGetter cidGetter = new Html.ImageGetter() { @@ -3473,7 +3473,7 @@ public class FragmentCompose extends FragmentBase { protected void onException(Bundle args, Throwable ex) { Helper.unexpectedError(getFragmentManager(), ex); } - }.execute(getContext(), getActivity(), new Bundle(), "compose:answer"); + }.execute(this, new Bundle(), "compose:answer"); return new AlertDialog.Builder(getContext()) .setTitle(R.string.title_insert_template) @@ -3552,15 +3552,13 @@ public class FragmentCompose extends FragmentBase { long id = getArguments().getLong("id"); boolean remind = getArguments().getBoolean("remind", false); - Context context = getContext(); - - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); int send_delayed = prefs.getInt("send_delayed", 0); - final int[] sendDelayedValues = context.getResources().getIntArray(R.array.sendDelayedValues); - final String[] sendDelayedNames = context.getResources().getStringArray(R.array.sendDelayedNames); + final int[] sendDelayedValues = getResources().getIntArray(R.array.sendDelayedValues); + final String[] sendDelayedNames = getResources().getStringArray(R.array.sendDelayedNames); - View dview = LayoutInflater.from(context).inflate(R.layout.dialog_send, null); + View dview = LayoutInflater.from(getContext()).inflate(R.layout.dialog_send, null); final TextView tvTo = dview.findViewById(R.id.tvTo); final TextView tvVia = dview.findViewById(R.id.tvVia); final CheckBox cbPlainOnly = dview.findViewById(R.id.cbPlainOnly); @@ -3574,8 +3572,8 @@ public class FragmentCompose extends FragmentBase { tvSendAt.setText(null); tvRemindAttachment.setVisibility(remind ? View.VISIBLE : View.GONE); - DB db = DB.getInstance(context); - db.message().liveMessage(id).observe(getActivity(), new Observer() { + DB db = DB.getInstance(getContext()); + db.message().liveMessage(id).observe(getViewLifecycleOwner(), new Observer() { @Override public void onChanged(TupleMessageEx draft) { int plus = (draft.cc == null ? 0 : draft.cc.length) + @@ -3588,15 +3586,15 @@ public class FragmentCompose extends FragmentBase { if (draft.ui_snoozed == null) { if (send_delayed == 0) - tvSendAt.setText(context.getString(R.string.title_now)); + tvSendAt.setText(getString(R.string.title_now)); else for (int pos = 0; pos < sendDelayedValues.length; pos++) if (sendDelayedValues[pos] == send_delayed) { - tvSendAt.setText(context.getString(R.string.title_after, sendDelayedNames[pos])); + tvSendAt.setText(getString(R.string.title_after, sendDelayedNames[pos])); break; } } else { - DateFormat DTF = Helper.getDateTimeInstance(context, SimpleDateFormat.MEDIUM, SimpleDateFormat.SHORT); + DateFormat DTF = Helper.getDateTimeInstance(getContext(), SimpleDateFormat.MEDIUM, SimpleDateFormat.SHORT); DateFormat D = new SimpleDateFormat("E"); tvSendAt.setText(D.format(draft.ui_snoozed) + " " + DTF.format(draft.ui_snoozed)); } @@ -3626,7 +3624,7 @@ public class FragmentCompose extends FragmentBase { protected void onException(Bundle args, Throwable ex) { Helper.unexpectedError(getFragmentManager(), ex); } - }.execute(context, getActivity(), args, "compose:plain_only"); + }.execute(FragmentDialogSend.this, args, "compose:plain_only"); } }); @@ -3653,7 +3651,7 @@ public class FragmentCompose extends FragmentBase { protected void onException(Bundle args, Throwable ex) { Helper.unexpectedError(getFragmentManager(), ex); } - }.execute(context, getActivity(), args, "compose:plain_only"); + }.execute(FragmentDialogSend.this, args, "compose:encrypt"); } }); @@ -3661,7 +3659,7 @@ public class FragmentCompose extends FragmentBase { @Override public void onClick(View view) { Bundle args = new Bundle(); - args.putString("title", context.getString(R.string.title_send_at)); + args.putString("title", getString(R.string.title_send_at)); args.putLong("id", id); FragmentDialogDuration fragment = new FragmentDialogDuration(); @@ -3671,7 +3669,7 @@ public class FragmentCompose extends FragmentBase { } }); - return new AlertDialog.Builder(context) + return new AlertDialog.Builder(getContext()) .setView(dview) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override @@ -3721,7 +3719,7 @@ public class FragmentCompose extends FragmentBase { protected void onException(Bundle args, Throwable ex) { Helper.unexpectedError(getFragmentManager(), ex); } - }.execute(context, getActivity(), args, "compose:snooze"); + }.execute(this, args, "compose:snooze"); } } diff --git a/app/src/main/java/eu/faircode/email/FragmentContacts.java b/app/src/main/java/eu/faircode/email/FragmentContacts.java index fb5a6fb6bb..c0d5e99e4d 100644 --- a/app/src/main/java/eu/faircode/email/FragmentContacts.java +++ b/app/src/main/java/eu/faircode/email/FragmentContacts.java @@ -193,7 +193,7 @@ public class FragmentContacts extends FragmentBase { protected void onException(Bundle args, Throwable ex) { Helper.unexpectedError(getFragmentManager(), ex); } - }.execute(getContext(), getActivity(), new Bundle(), "contacts:delete"); + }.execute(FragmentDelete.this, new Bundle(), "contacts:delete"); } }) .setNegativeButton(android.R.string.cancel, null) diff --git a/app/src/main/java/eu/faircode/email/FragmentDialogEx.java b/app/src/main/java/eu/faircode/email/FragmentDialogEx.java index 8b45bf03ee..41427b1097 100644 --- a/app/src/main/java/eu/faircode/email/FragmentDialogEx.java +++ b/app/src/main/java/eu/faircode/email/FragmentDialogEx.java @@ -11,44 +11,75 @@ import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.lifecycle.Lifecycle; import androidx.lifecycle.LifecycleObserver; +import androidx.lifecycle.LifecycleOwner; +import androidx.lifecycle.LifecycleRegistry; import androidx.lifecycle.OnLifecycleEvent; import static android.app.Activity.RESULT_CANCELED; public class FragmentDialogEx extends DialogFragment { private boolean once = false; + private LifecycleOwner owner; + private LifecycleRegistry registry; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); + owner = new LifecycleOwner() { + @NonNull + @Override + public Lifecycle getLifecycle() { + return registry; + } + }; + registry = new LifecycleRegistry(owner); + registry.setCurrentState(Lifecycle.State.CREATED); Log.i("Create " + this); } @Override public void onResume() { + registry.setCurrentState(Lifecycle.State.RESUMED); super.onResume(); Log.i("Resume " + this); } @Override public void onPause() { + registry.setCurrentState(Lifecycle.State.STARTED); super.onPause(); Log.i("Pause " + this); } @Override public void onDestroy() { + registry.setCurrentState(Lifecycle.State.DESTROYED); super.onDestroy(); Log.i("Destroy " + this); } @Override public void onStart() { + registry.setCurrentState(Lifecycle.State.STARTED); try { super.onStart(); } catch (Throwable ex) { Log.e(ex); } + Log.i("Start " + this); + } + + @Override + public void onStop() { + registry.setCurrentState(Lifecycle.State.CREATED); + super.onStop(); + Log.i("Stop " + this); + } + + @NonNull + @Override + public LifecycleOwner getViewLifecycleOwner() { + return owner; } @Override @@ -73,15 +104,13 @@ public class FragmentDialogEx extends DialogFragment { super.setTargetFragment(fragment, requestCode); Log.i("Set target " + this + " " + fragment); - if (getView() != null) { - fragment.getViewLifecycleOwner().getLifecycle().addObserver(new LifecycleObserver() { - @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) - public void onDestroy() { - Log.i("Reset target " + FragmentDialogEx.this); - FragmentDialogEx.super.setTargetFragment(null, requestCode); - } - }); - } + fragment.getViewLifecycleOwner().getLifecycle().addObserver(new LifecycleObserver() { + @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) + public void onDestroy() { + Log.i("Reset target " + FragmentDialogEx.this); + FragmentDialogEx.super.setTargetFragment(null, requestCode); + } + }); } protected void sendResult(int result) { diff --git a/app/src/main/java/eu/faircode/email/FragmentDialogFolder.java b/app/src/main/java/eu/faircode/email/FragmentDialogFolder.java index e24c6ef399..3bc834706b 100644 --- a/app/src/main/java/eu/faircode/email/FragmentDialogFolder.java +++ b/app/src/main/java/eu/faircode/email/FragmentDialogFolder.java @@ -53,7 +53,7 @@ public class FragmentDialogFolder extends FragmentDialogEx { LinearLayoutManager llm = new LinearLayoutManager(getContext()); rvFolder.setLayoutManager(llm); - final AdapterFolder adapter = new AdapterFolder(getContext(), getActivity(), + final AdapterFolder adapter = new AdapterFolder(getContext(), getViewLifecycleOwner(), account, false, new AdapterFolder.IFolderSelectedListener() { @Override public void onFolderSelected(TupleFolderEx folder) { @@ -106,7 +106,7 @@ public class FragmentDialogFolder extends FragmentDialogEx { protected void onException(Bundle args, Throwable ex) { Helper.unexpectedError(getFragmentManager(), ex); } - }.execute(getContext(), getActivity(), args, "folder:select"); + }.execute(this, args, "folder:select"); return new AlertDialog.Builder(getContext()) .setTitle(title) diff --git a/app/src/main/java/eu/faircode/email/FragmentDialogMarkdown.java b/app/src/main/java/eu/faircode/email/FragmentDialogMarkdown.java index c970562f64..b256b45447 100644 --- a/app/src/main/java/eu/faircode/email/FragmentDialogMarkdown.java +++ b/app/src/main/java/eu/faircode/email/FragmentDialogMarkdown.java @@ -90,7 +90,7 @@ public class FragmentDialogMarkdown extends FragmentDialogEx { protected void onException(Bundle args, Throwable ex) { Helper.unexpectedError(getFragmentManager(), ex); } - }.execute(getContext(), getActivity(), getArguments(), "markdown:read"); + }.execute(this, getArguments(), "markdown:read"); return dialog; } diff --git a/app/src/main/java/eu/faircode/email/FragmentFolder.java b/app/src/main/java/eu/faircode/email/FragmentFolder.java index 1bfd4a571f..81c68c4d61 100644 --- a/app/src/main/java/eu/faircode/email/FragmentFolder.java +++ b/app/src/main/java/eu/faircode/email/FragmentFolder.java @@ -507,7 +507,7 @@ public class FragmentFolder extends FragmentBase { else Helper.unexpectedError(getFragmentManager(), ex); } - }.execute(FragmentFolder.this, args, "folder:save"); + }.execute(this, args, "folder:save"); } private void onDelete() { @@ -551,6 +551,6 @@ public class FragmentFolder extends FragmentBase { else Helper.unexpectedError(getFragmentManager(), ex); } - }.execute(FragmentFolder.this, args, "folder:delete"); + }.execute(this, args, "folder:delete"); } } diff --git a/app/src/main/java/eu/faircode/email/FragmentIdentity.java b/app/src/main/java/eu/faircode/email/FragmentIdentity.java index 133806c888..16892e9e12 100644 --- a/app/src/main/java/eu/faircode/email/FragmentIdentity.java +++ b/app/src/main/java/eu/faircode/email/FragmentIdentity.java @@ -508,7 +508,7 @@ public class FragmentIdentity extends FragmentBase { else Helper.unexpectedError(getFragmentManager(), ex); } - }.execute(FragmentIdentity.this, args, "identity:config"); + }.execute(this, args, "identity:config"); } private void onSave(boolean should) { @@ -815,7 +815,7 @@ public class FragmentIdentity extends FragmentBase { else showError(ex); } - }.execute(FragmentIdentity.this, args, "identity:save"); + }.execute(this, args, "identity:save"); } private void showError(Throwable ex) { @@ -1134,7 +1134,7 @@ public class FragmentIdentity extends FragmentBase { protected void onException(Bundle args, Throwable ex) { Helper.unexpectedError(getFragmentManager(), ex); } - }.execute(FragmentIdentity.this, args, "identity:delete"); + }.execute(this, args, "identity:delete"); } private void onHtml(Bundle args) { diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index ada4003380..50ea8a03fd 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -304,6 +304,9 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. primary = args.getLong("primary", -1); connected = args.getBoolean("connected", false); + if (folder > 0 && type == null) + Log.e("Messages for folder without type"); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); swipenav = prefs.getBoolean("swipenav", true); @@ -4706,7 +4709,7 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences. protected void onException(Bundle args, Throwable ex) { Helper.unexpectedError(getFragmentManager(), ex); } - }.execute(getContext(), getActivity(), new Bundle(), "messages:accounts"); + }.execute(this, new Bundle(), "messages:accounts"); return new AlertDialog.Builder(getContext()) .setAdapter(adapter, new DialogInterface.OnClickListener() { diff --git a/app/src/main/java/eu/faircode/email/FragmentOperations.java b/app/src/main/java/eu/faircode/email/FragmentOperations.java index 2607fdf455..922a19b458 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOperations.java +++ b/app/src/main/java/eu/faircode/email/FragmentOperations.java @@ -154,7 +154,7 @@ public class FragmentOperations extends FragmentBase { protected void onException(Bundle args, Throwable ex) { Helper.unexpectedError(getFragmentManager(), ex); } - }.execute(getContext(), getActivity(), new Bundle(), "operations:delete"); + }.execute(DialogDelete.this, new Bundle(), "operations:delete"); } }) .setNegativeButton(android.R.string.cancel, null) diff --git a/app/src/main/java/eu/faircode/email/FragmentOrder.java b/app/src/main/java/eu/faircode/email/FragmentOrder.java index 756073d29e..3aca699aa5 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOrder.java +++ b/app/src/main/java/eu/faircode/email/FragmentOrder.java @@ -220,8 +220,7 @@ public class FragmentOrder extends FragmentBase { protected void onException(Bundle args, Throwable ex) { Helper.unexpectedError(getFragmentManager(), ex); } - }.execute(getContext(), getViewLifecycleOwner(), args, "order:set"); - + }.execute(this, args, "order:set"); } private ItemTouchHelper.Callback touchHelper = new ItemTouchHelper.Callback() { diff --git a/app/src/main/java/eu/faircode/email/FragmentQuickSetup.java b/app/src/main/java/eu/faircode/email/FragmentQuickSetup.java index 76b4ea1e7e..18e515f718 100644 --- a/app/src/main/java/eu/faircode/email/FragmentQuickSetup.java +++ b/app/src/main/java/eu/faircode/email/FragmentQuickSetup.java @@ -481,7 +481,7 @@ public class FragmentQuickSetup extends FragmentBase { }); } - }.execute(FragmentQuickSetup.this, args, "setup:quick"); + }.execute(this, args, "setup:quick"); } @Override diff --git a/app/src/main/java/eu/faircode/email/FragmentRule.java b/app/src/main/java/eu/faircode/email/FragmentRule.java index 2a30e251c5..c954c1e635 100644 --- a/app/src/main/java/eu/faircode/email/FragmentRule.java +++ b/app/src/main/java/eu/faircode/email/FragmentRule.java @@ -577,7 +577,7 @@ public class FragmentRule extends FragmentBase { protected void onException(Bundle args, Throwable ex) { Helper.unexpectedError(getFragmentManager(), ex); } - }.execute(FragmentRule.this, args, "rule:delete"); + }.execute(this, args, "rule:delete"); } private void onScheduleStart(Intent data) { @@ -724,7 +724,7 @@ public class FragmentRule extends FragmentBase { protected void onException(Bundle args, Throwable ex) { Helper.unexpectedError(getFragmentManager(), ex); } - }.execute(FragmentRule.this, rargs, "rule:get"); + }.execute(this, rargs, "rule:get"); } private void showActionParameters(int type) { @@ -1043,7 +1043,7 @@ public class FragmentRule extends FragmentBase { LinearLayoutManager llm = new LinearLayoutManager(getContext()); rvMessage.setLayoutManager(llm); - final AdapterRuleMatch adapter = new AdapterRuleMatch(getContext(), getActivity()); + final AdapterRuleMatch adapter = new AdapterRuleMatch(getContext(), getViewLifecycleOwner()); rvMessage.setAdapter(adapter); tvNoMessages.setVisibility(View.GONE); @@ -1093,7 +1093,7 @@ public class FragmentRule extends FragmentBase { protected void onException(Bundle args, Throwable ex) { Helper.unexpectedError(getFragmentManager(), ex); } - }.execute(getContext(), getActivity(), args, "rule:check"); + }.execute(this, args, "rule:check"); return new AlertDialog.Builder(getContext()) .setTitle(R.string.title_rule_matched) diff --git a/app/src/main/java/eu/faircode/email/Helper.java b/app/src/main/java/eu/faircode/email/Helper.java index f85a87cb95..ef21f10159 100644 --- a/app/src/main/java/eu/faircode/email/Helper.java +++ b/app/src/main/java/eu/faircode/email/Helper.java @@ -507,7 +507,7 @@ public class Helper { else ToastEx.makeText(context, ex.toString(), Toast.LENGTH_LONG).show(); } - }.execute(context, getActivity(), new Bundle(), "error:unexpected"); + }.execute(FragmentDialogUnexpected.this, new Bundle(), "error:unexpected"); } }) .create();