fix a bug where favs & reblogs where not shown correctly in NotificationsFragment

This commit is contained in:
Conny Duck 2017-10-27 23:39:36 +02:00
parent 5cbc7217ff
commit 9dd09d0522
2 changed files with 58 additions and 57 deletions

View file

@ -21,6 +21,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.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout; import android.support.design.widget.TabLayout;
@ -229,15 +230,63 @@ public class NotificationsFragment extends SFragment implements
} }
@Override @Override
public void onReblog(boolean reblog, int position) { public void onReblog(final boolean reblog, final int position) {
Notification notification = notifications.get(position); final Notification notification = notifications.get(position);
super.reblog(notification.status, reblog, adapter, position); final Status status = notification.status;
reblogWithCallback(status, reblog, new Callback<Status>() {
@Override
public void onResponse(@NonNull Call<Status> call, @NonNull retrofit2.Response<Status> response) {
if (response.isSuccessful()) {
status.reblogged = reblog;
if (status.reblog != null) {
status.reblog.reblogged = reblog;
}
notifications.set(position, notification);
adapter.updateItemWithNotify(position, notifications.getPairedItem(position), true);
adapter.notifyItemChanged(position);
}
} }
@Override @Override
public void onFavourite(boolean favourite, int position) { public void onFailure(@NonNull Call<Status> call, @NonNull Throwable t) {
Notification notification = notifications.get(position); Log.d(getClass().getSimpleName(), "Failed to reblog status: " + status.id);
super.favourite(notification.status, favourite, adapter, position); t.printStackTrace();
}
});
}
@Override
public void onFavourite(final boolean favourite, final int position) {
final Notification notification = notifications.get(position);
final Status status = notification.status;
favouriteWithCallback(status, favourite, new Callback<Status>() {
@Override
public void onResponse(@NonNull Call<Status> call, @NonNull retrofit2.Response<Status> response) {
if (response.isSuccessful()) {
status.favourited = favourite;
if (status.reblog != null) {
status.reblog.favourited = favourite;
}
notifications.set(position, notification);
adapter.updateItemWithNotify(position, notifications.getPairedItem(position), true);
adapter.notifyItemChanged(position);
}
}
@Override
public void onFailure(@NonNull Call<Status> call, @NonNull Throwable t) {
Log.d(getClass().getSimpleName(), "Failed to favourite status: " + status.id);
t.printStackTrace();
}
});
} }
@Override @Override
@ -373,8 +422,8 @@ public class NotificationsFragment extends SFragment implements
call.enqueue(new Callback<List<Notification>>() { call.enqueue(new Callback<List<Notification>>() {
@Override @Override
public void onResponse(Call<List<Notification>> call, public void onResponse(@NonNull Call<List<Notification>> call,
Response<List<Notification>> response) { @NonNull Response<List<Notification>> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
String linkHeader = response.headers().get("Link"); String linkHeader = response.headers().get("Link");
onFetchNotificationsSuccess(response.body(), linkHeader, fetchEnd); onFetchNotificationsSuccess(response.body(), linkHeader, fetchEnd);
@ -384,7 +433,7 @@ public class NotificationsFragment extends SFragment implements
} }
@Override @Override
public void onFailure(Call<List<Notification>> call, Throwable t) { public void onFailure(@NonNull Call<List<Notification>> call, @NonNull Throwable t) {
onFetchNotificationsFailure((Exception) t, fetchEnd); onFetchNotificationsFailure((Exception) t, fetchEnd);
} }
}); });

View file

@ -111,30 +111,6 @@ public abstract class SFragment extends BaseFragment implements AdapterItemRemov
startActivityForResult(intent, COMPOSE_RESULT); startActivityForResult(intent, COMPOSE_RESULT);
} }
protected void reblog(final Status status, final boolean reblog,
final RecyclerView.Adapter adapter, final int position) {
reblogWithCallback(status, reblog, new Callback<Status>() {
@Override
public void onResponse(Call<Status> call, retrofit2.Response<Status> response) {
if (response.isSuccessful()) {
status.reblogged = reblog;
if (status.reblog != null) {
status.reblog.reblogged = reblog;
}
adapter.notifyItemChanged(position);
}
}
@Override
public void onFailure(Call<Status> call, Throwable t) {
Log.d(getClass().getSimpleName(), "Failed to reblog status: " + status.id);
t.printStackTrace();
}
});
}
protected void reblogWithCallback(final Status status, final boolean reblog, protected void reblogWithCallback(final Status status, final boolean reblog,
Callback<Status> callback) { Callback<Status> callback) {
String id = status.getActionableId(); String id = status.getActionableId();
@ -148,30 +124,6 @@ public abstract class SFragment extends BaseFragment implements AdapterItemRemov
call.enqueue(callback); call.enqueue(callback);
} }
protected void favourite(final Status status, final boolean favourite,
final RecyclerView.Adapter adapter, final int position) {
favouriteWithCallback(status, favourite, new Callback<Status>() {
@Override
public void onResponse(Call<Status> call, retrofit2.Response<Status> response) {
if (response.isSuccessful()) {
status.favourited = favourite;
if (status.reblog != null) {
status.reblog.favourited = favourite;
}
adapter.notifyItemChanged(position);
}
}
@Override
public void onFailure(Call<Status> call, Throwable t) {
Log.d(getClass().getSimpleName(), "Failed to favourite status: " + status.id);
t.printStackTrace();
}
});
}
protected void favouriteWithCallback(final Status status, final boolean favourite, protected void favouriteWithCallback(final Status status, final boolean favourite,
final Callback<Status> callback) { final Callback<Status> callback) {
String id = status.getActionableId(); String id = status.getActionableId();