Lifecyle aware dialogs

pull/145/head
M66B 6 years ago
parent 11617bc585
commit fdcbd799f6

@ -69,7 +69,6 @@ import javax.net.ssl.HttpsURLConnection;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AlertDialog;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
@ -345,7 +344,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
private void checkFirst() {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (prefs.getBoolean("first", true)) {
new AlertDialog.Builder(this)
new DialogBuilderLifecycle(this, this)
.setMessage(getString(R.string.title_hint_sync))
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
@ -561,7 +560,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
final Intent update = new Intent(Intent.ACTION_VIEW, Uri.parse(info.html_url));
if (update.resolveActivity(getPackageManager()) != null)
new AlertDialog.Builder(ActivityView.this)
new DialogBuilderLifecycle(ActivityView.this, ActivityView.this)
.setMessage(getString(R.string.title_updated, info.tag_name))
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
@ -660,7 +659,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB
if (faq.resolveActivity(getPackageManager()) == null)
Helper.view(this, getIntentRate());
else {
new AlertDialog.Builder(this)
new DialogBuilderLifecycle(this, this)
.setMessage(R.string.title_issue)
.setPositiveButton(R.string.title_yes, new DialogInterface.OnClickListener() {
@Override

@ -41,7 +41,6 @@ import java.util.List;
import java.util.Locale;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.PopupMenu;
import androidx.lifecycle.LifecycleOwner;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
@ -219,7 +218,7 @@ public class AdapterFolder extends RecyclerView.Adapter<AdapterFolder.ViewHolder
break;
case action_empty_trash:
new AlertDialog.Builder(context)
new DialogBuilderLifecycle(context, owner)
.setMessage(R.string.title_empty_trash_ask)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override

@ -52,7 +52,6 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.PopupMenu;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.LifecycleOwner;
@ -275,7 +274,7 @@ public class AdapterMessage extends PagedListAdapter<TupleMessageEx, AdapterMess
args.putInt("action", target.getItemId());
if (target.getItemId() == action_delete) {
new AlertDialog.Builder(context)
new DialogBuilderLifecycle(context, owner)
.setMessage(R.string.title_ask_delete)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override

@ -0,0 +1,36 @@
package eu.faircode.email;
import android.content.Context;
import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.OnLifecycleEvent;
public class DialogBuilderLifecycle extends AlertDialog.Builder implements LifecycleObserver {
private LifecycleOwner owner;
private AlertDialog dialog;
public DialogBuilderLifecycle(Context context, LifecycleOwner owner) {
super(context);
this.owner = owner;
}
public DialogBuilderLifecycle(Context context, int themeResId, LifecycleOwner owner) {
super(context, themeResId);
this.owner = owner;
}
@Override
public AlertDialog create() {
dialog = super.create();
owner.getLifecycle().addObserver(this);
return dialog;
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroy() {
dialog.dismiss();
}
}

@ -81,7 +81,6 @@ import javax.mail.Session;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.constraintlayout.widget.Group;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentTransaction;
@ -548,7 +547,7 @@ public class FragmentAccount extends FragmentEx {
grpFolders.setVisibility(View.GONE);
btnSave.setVisibility(View.GONE);
new AlertDialog.Builder(getContext())
new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner())
.setMessage(Helper.formatThrowable(ex))
.setPositiveButton(android.R.string.cancel, null)
.create()
@ -753,7 +752,7 @@ public class FragmentAccount extends FragmentEx {
btnSave.setEnabled(true);
pbSave.setVisibility(View.GONE);
new AlertDialog.Builder(getContext())
new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner())
.setMessage(Helper.formatThrowable(ex))
.setPositiveButton(android.R.string.cancel, null)
.create()
@ -766,7 +765,7 @@ public class FragmentAccount extends FragmentEx {
ibDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(getContext())
new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner())
.setMessage(R.string.title_account_delete)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override

@ -82,7 +82,6 @@ import javax.mail.internet.InternetAddress;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.constraintlayout.widget.Group;
import androidx.core.content.ContextCompat;
import androidx.cursoradapter.widget.SimpleCursorAdapter;
@ -237,7 +236,7 @@ public class FragmentCompose extends FragmentEx {
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int action = item.getItemId();
if (action == R.id.action_delete) {
new AlertDialog.Builder(getContext())
new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner())
.setMessage(R.string.title_ask_delete)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
@ -419,7 +418,8 @@ public class FragmentCompose extends FragmentEx {
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
handleExit();
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED))
handleExit();
return true;
case R.id.menu_bold:
case R.id.menu_italic:
@ -513,22 +513,21 @@ public class FragmentCompose extends FragmentEx {
}
private void handleExit() {
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED))
new AlertDialog.Builder(getContext())
.setMessage(R.string.title_ask_delete)
.setPositiveButton(R.string.title_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
onAction(R.id.action_delete);
}
})
.setNegativeButton(R.string.title_no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.show();
new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner())
.setMessage(R.string.title_ask_delete)
.setPositiveButton(R.string.title_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
onAction(R.id.action_delete);
}
})
.setNegativeButton(R.string.title_no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.show();
}
private void handlePickContact(int requestCode, Intent data) {

@ -44,7 +44,6 @@ import javax.mail.Session;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.Observer;
public class FragmentFolder extends FragmentEx {
@ -211,7 +210,7 @@ public class FragmentFolder extends FragmentEx {
ibDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(getContext())
new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner())
.setMessage(R.string.title_folder_delete)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override

@ -54,7 +54,6 @@ import javax.mail.Transport;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.constraintlayout.widget.Group;
import androidx.lifecycle.Observer;
@ -403,7 +402,7 @@ public class FragmentIdentity extends FragmentEx {
btnSave.setEnabled(true);
pbSave.setVisibility(View.GONE);
new AlertDialog.Builder(getContext())
new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner())
.setMessage(Helper.formatThrowable(ex))
.setPositiveButton(android.R.string.cancel, null)
.create()
@ -416,7 +415,7 @@ public class FragmentIdentity extends FragmentEx {
ibDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(getContext())
new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner())
.setMessage(R.string.title_identity_delete)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override

@ -87,7 +87,6 @@ import javax.mail.internet.InternetAddress;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.PopupMenu;
import androidx.constraintlayout.widget.Group;
import androidx.core.content.ContextCompat;
@ -320,7 +319,7 @@ public class FragmentMessage extends FragmentEx {
View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_link, null);
final EditText etLink = view.findViewById(R.id.etLink);
etLink.setText(url);
new AlertDialog.Builder(getContext())
new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner())
.setView(view)
.setPositiveButton(R.string.title_yes, new DialogInterface.OnClickListener() {
@Override
@ -865,7 +864,7 @@ public class FragmentMessage extends FragmentEx {
}
private void onActionSpam() {
new AlertDialog.Builder(getContext())
new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner())
.setMessage(R.string.title_ask_spam)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
@ -921,7 +920,7 @@ public class FragmentMessage extends FragmentEx {
boolean delete = (Boolean) bottom_navigation.getTag();
if (delete) {
// No trash or is trash
new AlertDialog.Builder(getContext())
new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner())
.setMessage(R.string.title_ask_delete)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override

@ -65,7 +65,6 @@ import java.util.List;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer;
@ -176,7 +175,7 @@ public class FragmentSetup extends FragmentEx {
btnDoze.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(getContext())
new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner())
.setMessage(R.string.title_setup_doze_instructions)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override

Loading…
Cancel
Save