Add lifecycle check in SnackbarAsyncTask.

master
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")
@Override
protected void onItemSwiped(long threadId, int unreadCount) {
new SnackbarAsyncTask<Long>(getView(),
getResources().getQuantityString(R.plurals.ConversationListFragment_moved_conversations_to_inbox, 1, 1),
getString(R.string.ConversationListFragment_undo),
getResources().getColor(R.color.amber_500),
Snackbar.LENGTH_LONG, false)
new SnackbarAsyncTask<Long>(getViewLifecycleOwner().getLifecycle(),
requireView(),
getResources().getQuantityString(R.plurals.ConversationListFragment_moved_conversations_to_inbox, 1, 1),
getString(R.string.ConversationListFragment_undo),
getResources().getColor(R.color.amber_500),
Snackbar.LENGTH_LONG,
false)
{
@Override
protected void executeAction(@Nullable Long parameter) {

View File

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

View File

@ -3,32 +3,42 @@ package org.thoughtcrime.securesms.util.task;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.AsyncTask;
import androidx.annotation.Nullable;
import com.google.android.material.snackbar.Snackbar;
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>
extends AsyncTask<Params, Void, Void>
implements View.OnClickListener
{
private static final String TAG = Log.tag(SnackbarAsyncTask.class);
private final View view;
private final String snackbarText;
private final String snackbarActionText;
private final int snackbarActionColor;
private final int snackbarDuration;
private final boolean showProgress;
private final Lifecycle lifecycle;
private final View view;
private final String snackbarText;
private final String snackbarActionText;
private final int snackbarActionColor;
private final int snackbarDuration;
private final boolean showProgress;
private @Nullable Params reversibleParameter;
private @Nullable ProgressDialog progressDialog;
public SnackbarAsyncTask(View view,
public SnackbarAsyncTask(@NonNull Lifecycle lifecycle,
@NonNull View view,
String snackbarText,
String snackbarActionText,
int snackbarActionColor,
int snackbarDuration,
boolean showProgress)
{
this.lifecycle = lifecycle;
this.view = view;
this.snackbarText = snackbarText;
this.snackbarActionText = snackbarActionText;
@ -58,6 +68,11 @@ public abstract class SnackbarAsyncTask<Params>
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)
.setAction(snackbarActionText, this)
.setActionTextColor(snackbarActionColor)