close ViewThreadActivity if the main status got removed to avoid crashes on subsequent refreshes

This commit is contained in:
Conny Duck 2017-10-24 23:33:05 +02:00
parent b213941c5b
commit 00c5a975d6

View file

@ -20,6 +20,7 @@ import android.content.SharedPreferences;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
@ -146,7 +147,7 @@ public class ViewThreadFragment extends SFragment implements
final Status status = statuses.get(position); final Status status = statuses.get(position);
super.reblogWithCallback(statuses.get(position), reblog, new Callback<Status>() { super.reblogWithCallback(statuses.get(position), reblog, new Callback<Status>() {
@Override @Override
public void onResponse(Call<Status> call, Response<Status> response) { public void onResponse(@NonNull Call<Status> call, @NonNull Response<Status> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
status.reblogged = reblog; status.reblogged = reblog;
@ -161,7 +162,7 @@ public class ViewThreadFragment extends SFragment implements
} }
@Override @Override
public void onFailure(Call<Status> call, Throwable t) { public void onFailure(@NonNull Call<Status> call, @NonNull Throwable t) {
Log.d(getClass().getSimpleName(), "Failed to reblog status: " + status.id); Log.d(getClass().getSimpleName(), "Failed to reblog status: " + status.id);
t.printStackTrace(); t.printStackTrace();
} }
@ -173,7 +174,7 @@ public class ViewThreadFragment extends SFragment implements
final Status status = statuses.get(position); final Status status = statuses.get(position);
super.favouriteWithCallback(statuses.get(position), favourite, new Callback<Status>() { super.favouriteWithCallback(statuses.get(position), favourite, new Callback<Status>() {
@Override @Override
public void onResponse(Call<Status> call, Response<Status> response) { public void onResponse(@NonNull Call<Status> call, @NonNull Response<Status> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
status.favourited = favourite; status.favourited = favourite;
@ -187,7 +188,7 @@ public class ViewThreadFragment extends SFragment implements
} }
@Override @Override
public void onFailure(Call<Status> call, Throwable t) { public void onFailure(@NonNull Call<Status> call, @NonNull Throwable t) {
Log.d(getClass().getSimpleName(), "Failed to favourite status: " + status.id); Log.d(getClass().getSimpleName(), "Failed to favourite status: " + status.id);
t.printStackTrace(); t.printStackTrace();
} }
@ -251,6 +252,10 @@ public class ViewThreadFragment extends SFragment implements
@Override @Override
public void removeItem(int position) { public void removeItem(int position) {
if(position == statusIndex) {
//the status got removed, close the activity
getActivity().finish();
}
statuses.remove(position); statuses.remove(position);
adapter.setStatuses(statuses.getPairedCopy()); adapter.setStatuses(statuses.getPairedCopy());
} }
@ -270,6 +275,11 @@ public class ViewThreadFragment extends SFragment implements
} }
} }
statusIndex = statuses.indexOf(status); statusIndex = statuses.indexOf(status);
if(statusIndex == -1) {
//the status got removed, close the activity
getActivity().finish();
return;
}
adapter.setDetailedStatusPosition(statusIndex); adapter.setDetailedStatusPosition(statusIndex);
adapter.setStatuses(statuses.getPairedCopy()); adapter.setStatuses(statuses.getPairedCopy());
} }
@ -278,7 +288,7 @@ public class ViewThreadFragment extends SFragment implements
Call<Status> call = mastodonApi.status(id); Call<Status> call = mastodonApi.status(id);
call.enqueue(new Callback<Status>() { call.enqueue(new Callback<Status>() {
@Override @Override
public void onResponse(Call<Status> call, Response<Status> response) { public void onResponse(@NonNull Call<Status> call, @NonNull Response<Status> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
int position = setStatus(response.body()); int position = setStatus(response.body());
recyclerView.scrollToPosition(position); recyclerView.scrollToPosition(position);
@ -288,7 +298,7 @@ public class ViewThreadFragment extends SFragment implements
} }
@Override @Override
public void onFailure(Call<Status> call, Throwable t) { public void onFailure(@NonNull Call<Status> call, @NonNull Throwable t) {
onThreadRequestFailure(id); onThreadRequestFailure(id);
} }
}); });
@ -299,7 +309,7 @@ public class ViewThreadFragment extends SFragment implements
Call<StatusContext> call = mastodonApi.statusContext(id); Call<StatusContext> call = mastodonApi.statusContext(id);
call.enqueue(new Callback<StatusContext>() { call.enqueue(new Callback<StatusContext>() {
@Override @Override
public void onResponse(Call<StatusContext> call, Response<StatusContext> response) { public void onResponse(@NonNull Call<StatusContext> call, @NonNull Response<StatusContext> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
swipeRefreshLayout.setRefreshing(false); swipeRefreshLayout.setRefreshing(false);
StatusContext context = response.body(); StatusContext context = response.body();
@ -310,7 +320,7 @@ public class ViewThreadFragment extends SFragment implements
} }
@Override @Override
public void onFailure(Call<StatusContext> call, Throwable t) { public void onFailure(@NonNull Call<StatusContext> call, @NonNull Throwable t) {
onThreadRequestFailure(id); onThreadRequestFailure(id);
} }
}); });