Add lifecycle check in SnackbarAsyncTask.

This commit is contained in:
Alex Hart 2020-09-15 10:34:32 -03:00 committed by Greyson Parrelli
parent 9dc33eff3a
commit 0271e4c918
3 changed files with 40 additions and 20 deletions

View File

@ -123,11 +123,13 @@ public class ConversationListArchiveFragment extends ConversationListFragment im
@SuppressLint("StaticFieldLeak") @SuppressLint("StaticFieldLeak")
@Override @Override
protected void onItemSwiped(long threadId, int unreadCount) { protected void onItemSwiped(long threadId, int unreadCount) {
new SnackbarAsyncTask<Long>(getView(), new SnackbarAsyncTask<Long>(getViewLifecycleOwner().getLifecycle(),
requireView(),
getResources().getQuantityString(R.plurals.ConversationListFragment_moved_conversations_to_inbox, 1, 1), getResources().getQuantityString(R.plurals.ConversationListFragment_moved_conversations_to_inbox, 1, 1),
getString(R.string.ConversationListFragment_undo), getString(R.string.ConversationListFragment_undo),
getResources().getColor(R.color.amber_500), getResources().getColor(R.color.amber_500),
Snackbar.LENGTH_LONG, false) Snackbar.LENGTH_LONG,
false)
{ {
@Override @Override
protected void executeAction(@Nullable Long parameter) { protected void executeAction(@Nullable Long parameter) {

View File

@ -679,7 +679,8 @@ public class ConversationListFragment extends MainFragment implements ActionMode
int count = selectedConversations.size(); int count = selectedConversations.size();
String snackBarTitle = getResources().getQuantityString(getArchivedSnackbarTitleRes(), count, count); String snackBarTitle = getResources().getQuantityString(getArchivedSnackbarTitleRes(), count, count);
new SnackbarAsyncTask<Void>(getView(), new SnackbarAsyncTask<Void>(getViewLifecycleOwner().getLifecycle(),
requireView(),
snackBarTitle, snackBarTitle,
getString(R.string.ConversationListFragment_undo), getString(R.string.ConversationListFragment_undo),
getResources().getColor(R.color.amber_500), getResources().getColor(R.color.amber_500),
@ -1002,11 +1003,13 @@ public class ConversationListFragment extends MainFragment implements ActionMode
@SuppressLint("StaticFieldLeak") @SuppressLint("StaticFieldLeak")
protected void onItemSwiped(long threadId, int unreadCount) { protected void onItemSwiped(long threadId, int unreadCount) {
new SnackbarAsyncTask<Long>(getView(), new SnackbarAsyncTask<Long>(getViewLifecycleOwner().getLifecycle(),
requireView(),
getResources().getQuantityString(R.plurals.ConversationListFragment_conversations_archived, 1, 1), getResources().getQuantityString(R.plurals.ConversationListFragment_conversations_archived, 1, 1),
getString(R.string.ConversationListFragment_undo), getString(R.string.ConversationListFragment_undo),
getResources().getColor(R.color.amber_500), getResources().getColor(R.color.amber_500),
Snackbar.LENGTH_LONG, false) Snackbar.LENGTH_LONG,
false)
{ {
@Override @Override
protected void executeAction(@Nullable Long parameter) { protected void executeAction(@Nullable Long parameter) {

View File

@ -3,15 +3,23 @@ package org.thoughtcrime.securesms.util.task;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.graphics.Color; import android.graphics.Color;
import android.os.AsyncTask; import android.os.AsyncTask;
import androidx.annotation.Nullable;
import com.google.android.material.snackbar.Snackbar;
import android.view.View; import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import com.google.android.material.snackbar.Snackbar;
import org.thoughtcrime.securesms.logging.Log;
public abstract class SnackbarAsyncTask<Params> public abstract class SnackbarAsyncTask<Params>
extends AsyncTask<Params, Void, Void> extends AsyncTask<Params, Void, Void>
implements View.OnClickListener implements View.OnClickListener
{ {
private static final String TAG = Log.tag(SnackbarAsyncTask.class);
private final Lifecycle lifecycle;
private final View view; private final View view;
private final String snackbarText; private final String snackbarText;
private final String snackbarActionText; private final String snackbarActionText;
@ -22,13 +30,15 @@ public abstract class SnackbarAsyncTask<Params>
private @Nullable Params reversibleParameter; private @Nullable Params reversibleParameter;
private @Nullable ProgressDialog progressDialog; private @Nullable ProgressDialog progressDialog;
public SnackbarAsyncTask(View view, public SnackbarAsyncTask(@NonNull Lifecycle lifecycle,
@NonNull View view,
String snackbarText, String snackbarText,
String snackbarActionText, String snackbarActionText,
int snackbarActionColor, int snackbarActionColor,
int snackbarDuration, int snackbarDuration,
boolean showProgress) boolean showProgress)
{ {
this.lifecycle = lifecycle;
this.view = view; this.view = view;
this.snackbarText = snackbarText; this.snackbarText = snackbarText;
this.snackbarActionText = snackbarActionText; this.snackbarActionText = snackbarActionText;
@ -58,6 +68,11 @@ public abstract class SnackbarAsyncTask<Params>
this.progressDialog = null; this.progressDialog = null;
} }
if (!lifecycle.getCurrentState().isAtLeast(Lifecycle.State.CREATED)) {
Log.w(TAG, "Not in at least created state. Refusing to show snack bar.");
return;
}
Snackbar.make(view, snackbarText, snackbarDuration) Snackbar.make(view, snackbarText, snackbarDuration)
.setAction(snackbarActionText, this) .setAction(snackbarActionText, this)
.setActionTextColor(snackbarActionColor) .setActionTextColor(snackbarActionColor)