From 9dd09d052276b1158fc54c836d5c5cc037bb65c9 Mon Sep 17 00:00:00 2001 From: Conny Duck Date: Fri, 27 Oct 2017 23:39:36 +0200 Subject: [PATCH] fix a bug where favs & reblogs where not shown correctly in NotificationsFragment --- .../tusky/fragment/NotificationsFragment.java | 67 ++++++++++++++++--- .../tusky/fragment/SFragment.java | 48 ------------- 2 files changed, 58 insertions(+), 57 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/fragment/NotificationsFragment.java b/app/src/main/java/com/keylesspalace/tusky/fragment/NotificationsFragment.java index bc9f1db1..5c3254c9 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/NotificationsFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/NotificationsFragment.java @@ -21,6 +21,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.FloatingActionButton; import android.support.design.widget.TabLayout; @@ -229,15 +230,63 @@ public class NotificationsFragment extends SFragment implements } @Override - public void onReblog(boolean reblog, int position) { - Notification notification = notifications.get(position); - super.reblog(notification.status, reblog, adapter, position); + public void onReblog(final boolean reblog, final int position) { + final Notification notification = notifications.get(position); + final Status status = notification.status; + reblogWithCallback(status, reblog, new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response 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 + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + Log.d(getClass().getSimpleName(), "Failed to reblog status: " + status.id); + t.printStackTrace(); + } + }); } + @Override - public void onFavourite(boolean favourite, int position) { - Notification notification = notifications.get(position); - super.favourite(notification.status, favourite, adapter, position); + 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() { + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response 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 call, @NonNull Throwable t) { + Log.d(getClass().getSimpleName(), "Failed to favourite status: " + status.id); + t.printStackTrace(); + } + }); } @Override @@ -373,8 +422,8 @@ public class NotificationsFragment extends SFragment implements call.enqueue(new Callback>() { @Override - public void onResponse(Call> call, - Response> response) { + public void onResponse(@NonNull Call> call, + @NonNull Response> response) { if (response.isSuccessful()) { String linkHeader = response.headers().get("Link"); onFetchNotificationsSuccess(response.body(), linkHeader, fetchEnd); @@ -384,7 +433,7 @@ public class NotificationsFragment extends SFragment implements } @Override - public void onFailure(Call> call, Throwable t) { + public void onFailure(@NonNull Call> call, @NonNull Throwable t) { onFetchNotificationsFailure((Exception) t, fetchEnd); } }); diff --git a/app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.java b/app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.java index 30830195..6b532ca0 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/SFragment.java @@ -111,30 +111,6 @@ public abstract class SFragment extends BaseFragment implements AdapterItemRemov 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() { - @Override - public void onResponse(Call call, retrofit2.Response response) { - if (response.isSuccessful()) { - status.reblogged = reblog; - - if (status.reblog != null) { - status.reblog.reblogged = reblog; - } - - adapter.notifyItemChanged(position); - } - } - - @Override - public void onFailure(Call call, Throwable t) { - Log.d(getClass().getSimpleName(), "Failed to reblog status: " + status.id); - t.printStackTrace(); - } - }); - } - protected void reblogWithCallback(final Status status, final boolean reblog, Callback callback) { String id = status.getActionableId(); @@ -148,30 +124,6 @@ public abstract class SFragment extends BaseFragment implements AdapterItemRemov call.enqueue(callback); } - protected void favourite(final Status status, final boolean favourite, - final RecyclerView.Adapter adapter, final int position) { - favouriteWithCallback(status, favourite, new Callback() { - @Override - public void onResponse(Call call, retrofit2.Response response) { - if (response.isSuccessful()) { - status.favourited = favourite; - - if (status.reblog != null) { - status.reblog.favourited = favourite; - } - - adapter.notifyItemChanged(position); - } - } - - @Override - public void onFailure(Call call, Throwable t) { - Log.d(getClass().getSimpleName(), "Failed to favourite status: " + status.id); - t.printStackTrace(); - } - }); - } - protected void favouriteWithCallback(final Status status, final boolean favourite, final Callback callback) { String id = status.getActionableId();