From 00c5a975d6d0e1306685814fad721f360085b94a Mon Sep 17 00:00:00 2001 From: Conny Duck Date: Tue, 24 Oct 2017 23:33:05 +0200 Subject: [PATCH] close ViewThreadActivity if the main status got removed to avoid crashes on subsequent refreshes --- .../tusky/fragment/ViewThreadFragment.java | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/fragment/ViewThreadFragment.java b/app/src/main/java/com/keylesspalace/tusky/fragment/ViewThreadFragment.java index 5501c882..43b4423a 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/ViewThreadFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/ViewThreadFragment.java @@ -20,6 +20,7 @@ import android.content.SharedPreferences; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.preference.PreferenceManager; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.design.widget.Snackbar; import android.support.v4.content.ContextCompat; @@ -146,7 +147,7 @@ public class ViewThreadFragment extends SFragment implements final Status status = statuses.get(position); super.reblogWithCallback(statuses.get(position), reblog, new Callback() { @Override - public void onResponse(Call call, Response response) { + public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { status.reblogged = reblog; @@ -161,7 +162,7 @@ public class ViewThreadFragment extends SFragment implements } @Override - public void onFailure(Call call, Throwable t) { + public void onFailure(@NonNull Call call, @NonNull Throwable t) { Log.d(getClass().getSimpleName(), "Failed to reblog status: " + status.id); t.printStackTrace(); } @@ -173,7 +174,7 @@ public class ViewThreadFragment extends SFragment implements final Status status = statuses.get(position); super.favouriteWithCallback(statuses.get(position), favourite, new Callback() { @Override - public void onResponse(Call call, Response response) { + public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { status.favourited = favourite; @@ -187,7 +188,7 @@ public class ViewThreadFragment extends SFragment implements } @Override - public void onFailure(Call call, Throwable t) { + public void onFailure(@NonNull Call call, @NonNull Throwable t) { Log.d(getClass().getSimpleName(), "Failed to favourite status: " + status.id); t.printStackTrace(); } @@ -251,6 +252,10 @@ public class ViewThreadFragment extends SFragment implements @Override public void removeItem(int position) { + if(position == statusIndex) { + //the status got removed, close the activity + getActivity().finish(); + } statuses.remove(position); adapter.setStatuses(statuses.getPairedCopy()); } @@ -270,6 +275,11 @@ public class ViewThreadFragment extends SFragment implements } } statusIndex = statuses.indexOf(status); + if(statusIndex == -1) { + //the status got removed, close the activity + getActivity().finish(); + return; + } adapter.setDetailedStatusPosition(statusIndex); adapter.setStatuses(statuses.getPairedCopy()); } @@ -278,7 +288,7 @@ public class ViewThreadFragment extends SFragment implements Call call = mastodonApi.status(id); call.enqueue(new Callback() { @Override - public void onResponse(Call call, Response response) { + public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { int position = setStatus(response.body()); recyclerView.scrollToPosition(position); @@ -288,7 +298,7 @@ public class ViewThreadFragment extends SFragment implements } @Override - public void onFailure(Call call, Throwable t) { + public void onFailure(@NonNull Call call, @NonNull Throwable t) { onThreadRequestFailure(id); } }); @@ -299,7 +309,7 @@ public class ViewThreadFragment extends SFragment implements Call call = mastodonApi.statusContext(id); call.enqueue(new Callback() { @Override - public void onResponse(Call call, Response response) { + public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { swipeRefreshLayout.setRefreshing(false); StatusContext context = response.body(); @@ -310,7 +320,7 @@ public class ViewThreadFragment extends SFragment implements } @Override - public void onFailure(Call call, Throwable t) { + public void onFailure(@NonNull Call call, @NonNull Throwable t) { onThreadRequestFailure(id); } });