From 250e052b8f1317d600db1c61bb0829039df6da85 Mon Sep 17 00:00:00 2001 From: tom79 Date: Sat, 30 Mar 2019 18:48:38 +0100 Subject: [PATCH] Refresh statuses after an action --- .../activities/ShowConversationActivity.java | 5 ++-- .../fr/gouv/etalab/mastodon/client/API.java | 4 +-- .../gouv/etalab/mastodon/client/GNUAPI.java | 1 - .../mastodon/drawers/ArtListAdapter.java | 21 ++------------- .../drawers/NotificationsListAdapter.java | 27 +++---------------- .../mastodon/drawers/PixelfedListAdapter.java | 21 ++------------- .../mastodon/drawers/StatusListAdapter.java | 21 ++------------- .../DisplayNotificationsFragment.java | 10 ++----- .../fragments/DisplayStatusFragment.java | 7 +---- 9 files changed, 15 insertions(+), 102 deletions(-) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/ShowConversationActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/ShowConversationActivity.java index ecd81a740..54f80748c 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/ShowConversationActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/ShowConversationActivity.java @@ -129,9 +129,8 @@ public class ShowConversationActivity extends BaseActivity implements OnRetriev Bundle b = intent.getExtras(); assert b != null; Status status = b.getParcelable("status"); - API.StatusAction statusAction = (API.StatusAction) b.getSerializable("action"); - if( status != null) { - statusListAdapter.notifyStatusWithActionChanged(statusAction, status); + if( status != null && statusListAdapter != null) { + statusListAdapter.notifyStatusWithActionChanged(status); } } }; diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java index 4d8135842..8e6970977 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java @@ -2065,7 +2065,6 @@ public class API { try { Status status1 = parseStatuses(context, new JSONObject(resp)); b.putParcelable("status", status1); - b.putSerializable("action", statusAction); } catch (JSONException ignored) {} Intent intentBC = new Intent(Helper.RECEIVE_ACTION); intentBC.putExtras(b); @@ -2223,9 +2222,8 @@ public class API { String response = httpsConnection.get(getAbsoluteUrl(String.format("/polls/%s", status.getPoll().getId())), 60, null, prefKeyOauthTokenT); Poll poll = parsePoll(context, new JSONObject(response)); Bundle b = new Bundle(); - b.putParcelable("poll", poll); + status.setPoll(poll); b.putParcelable("status", status); - b.putSerializable("action", REFRESHPOLL); Intent intentBC = new Intent(Helper.RECEIVE_ACTION); intentBC.putExtras(b); LocalBroadcastManager.getInstance(context).sendBroadcast(intentBC); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/GNUAPI.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/GNUAPI.java index f61bd9eab..9fab9d736 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/GNUAPI.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/GNUAPI.java @@ -1389,7 +1389,6 @@ public class GNUAPI { try { Status status1 = parseStatuses(context, new JSONObject(resp)); b.putParcelable("status", status1); - b.putSerializable("action", statusAction); } catch (JSONException ignored) {} Intent intentBC = new Intent(Helper.RECEIVE_ACTION); intentBC.putExtras(b); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/ArtListAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/ArtListAdapter.java index 757f90cab..1c0e2578b 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/ArtListAdapter.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/ArtListAdapter.java @@ -316,29 +316,12 @@ public class ArtListAdapter extends RecyclerView.Adapter implements OnPostAction } } - public void notifyStatusWithActionChanged(API.StatusAction statusAction, Status status){ + public void notifyStatusWithActionChanged(Status status){ for (int i = 0; i < statusListAdapter.getItemCount(); i++) { //noinspection ConstantConditions if (statusListAdapter.getItemAt(i) != null && statusListAdapter.getItemAt(i).getId().equals(status.getId())) { try { - int favCount = statuses.get(i).getFavourites_count(); - int boostCount = statuses.get(i).getReblogs_count(); - if( statusAction == API.StatusAction.REBLOG) - boostCount++; - else if( statusAction == API.StatusAction.UNREBLOG) - boostCount--; - else if( statusAction == API.StatusAction.FAVOURITE) - favCount++; - else if( statusAction == API.StatusAction.UNFAVOURITE) - favCount--; - if( boostCount < 0 ) - boostCount = 0; - if( favCount < 0 ) - favCount = 0; - statuses.get(i).setFavourited(status.isFavourited()); - statuses.get(i).setFavourites_count(favCount); - statuses.get(i).setReblogged(status.isReblogged()); - statuses.get(i).setReblogs_count(boostCount); + statuses.set(i, status); statusListAdapter.notifyItemChanged(i); } catch (Exception ignored) { } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/NotificationsListAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/NotificationsListAdapter.java index f3493ed3f..89a4c90c4 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/NotificationsListAdapter.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/NotificationsListAdapter.java @@ -1081,33 +1081,12 @@ public class NotificationsListAdapter extends RecyclerView.Adapter implements On } - public void notifyNotificationWithActionChanged(API.StatusAction statusAction, Status status){ + public void notifyNotificationWithActionChanged(Status status){ for (int i = 0; i < notificationsListAdapter.getItemCount(); i++) { if (notificationsListAdapter.getItemAt(i) != null && notificationsListAdapter.getItemAt(i).getStatus() != null && notificationsListAdapter.getItemAt(i).getStatus().getId().equals(status.getId())) { try { if( notifications.get(i).getStatus() != null){ - int favCount = notifications.get(i).getStatus().getFavourites_count(); - int boostCount = notifications.get(i).getStatus().getReblogs_count(); - if( statusAction == API.StatusAction.REBLOG) - boostCount++; - else if( statusAction == API.StatusAction.UNREBLOG) - boostCount--; - else if( statusAction == API.StatusAction.FAVOURITE) - favCount++; - else if( statusAction == API.StatusAction.UNFAVOURITE) - favCount--; - else if( statusAction == API.StatusAction.REFRESHPOLL){ - if( status.getPoll() != null) - notifications.get(i).getStatus().setPoll(status.getPoll()); - } - if( boostCount < 0 ) - boostCount = 0; - if( favCount < 0 ) - favCount = 0; - notifications.get(i).getStatus().setFavourited(status.isFavourited()); - notifications.get(i).getStatus().setFavourites_count(favCount); - notifications.get(i).getStatus().setReblogged(status.isReblogged()); - notifications.get(i).getStatus().setReblogs_count(boostCount); + notifications.get(i).setStatus(status); notificationsListAdapter.notifyItemChanged(i); break; } @@ -1345,7 +1324,7 @@ public class NotificationsListAdapter extends RecyclerView.Adapter implements On @Override public void onPoll(Status status, Poll poll) { status.setPoll(poll); - notifyNotificationWithActionChanged(API.StatusAction.REFRESHPOLL, status); + notifyNotificationWithActionChanged(status); } class ViewHolder extends RecyclerView.ViewHolder { diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/PixelfedListAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/PixelfedListAdapter.java index 00ff45703..0f4a5842c 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/PixelfedListAdapter.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/PixelfedListAdapter.java @@ -460,29 +460,12 @@ public class PixelfedListAdapter extends RecyclerView.Adapter implements OnPostA } } - public void notifyStatusWithActionChanged(API.StatusAction statusAction, Status status){ + public void notifyStatusWithActionChanged(Status status){ for (int i = 0; i < pixelfedListAdapter.getItemCount(); i++) { //noinspection ConstantConditions if (pixelfedListAdapter.getItemAt(i) != null && pixelfedListAdapter.getItemAt(i).getId().equals(status.getId())) { try { - int favCount = statuses.get(i).getFavourites_count(); - int boostCount = statuses.get(i).getReblogs_count(); - if( statusAction == API.StatusAction.REBLOG) - boostCount++; - else if( statusAction == API.StatusAction.UNREBLOG) - boostCount--; - else if( statusAction == API.StatusAction.FAVOURITE) - favCount++; - else if( statusAction == API.StatusAction.UNFAVOURITE) - favCount--; - if( boostCount < 0 ) - boostCount = 0; - if( favCount < 0 ) - favCount = 0; - statuses.get(i).setFavourited(status.isFavourited()); - statuses.get(i).setFavourites_count(favCount); - statuses.get(i).setReblogged(status.isReblogged()); - statuses.get(i).setReblogs_count(boostCount); + statuses.set(i, status); pixelfedListAdapter.notifyItemChanged(i); } catch (Exception ignored) { } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java index 2e37c1186..8e9fe254c 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java @@ -3025,29 +3025,12 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct } } - public void notifyStatusWithActionChanged(API.StatusAction statusAction, Status status){ + public void notifyStatusWithActionChanged(Status status){ for (int i = 0; i < statusListAdapter.getItemCount(); i++) { //noinspection ConstantConditions if (statusListAdapter.getItemAt(i) != null && statusListAdapter.getItemAt(i).getId().equals(status.getId())) { try { - int favCount = statuses.get(i).getFavourites_count(); - int boostCount = statuses.get(i).getReblogs_count(); - if( statusAction == API.StatusAction.REBLOG) - boostCount++; - else if( statusAction == API.StatusAction.UNREBLOG) - boostCount--; - else if( statusAction == API.StatusAction.FAVOURITE) - favCount++; - else if( statusAction == API.StatusAction.UNFAVOURITE) - favCount--; - if( boostCount < 0 ) - boostCount = 0; - if( favCount < 0 ) - favCount = 0; - statuses.get(i).setFavourited(status.isFavourited()); - statuses.get(i).setFavourites_count(favCount); - statuses.get(i).setReblogged(status.isReblogged()); - statuses.get(i).setReblogs_count(boostCount); + statuses.set(i, status); statusListAdapter.notifyItemChanged(i); } catch (Exception ignored) { } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayNotificationsFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayNotificationsFragment.java index 2c542b420..388f1d3b4 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayNotificationsFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayNotificationsFragment.java @@ -157,14 +157,8 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve Bundle b = intent.getExtras(); assert b != null; Status status = b.getParcelable("status"); - Poll poll = b.getParcelable("poll"); - if (poll != null && status != null){ - status.setPoll(poll); - } - API.StatusAction statusAction = (API.StatusAction) b.getSerializable("action"); - if (status != null) { - notificationsListAdapter.notifyNotificationWithActionChanged(statusAction, status); - } + if( notificationsListAdapter != null && status != null) + notificationsListAdapter.notifyNotificationWithActionChanged(status); } }; LocalBroadcastManager.getInstance(context).registerReceiver(receive_action, new IntentFilter(Helper.RECEIVE_ACTION)); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java index fd4ad652b..015408a25 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java @@ -245,13 +245,8 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn Bundle b = intent.getExtras(); assert b != null; Status status = b.getParcelable("status"); - Poll poll = b.getParcelable("poll"); - if (poll != null && status != null){ - status.setPoll(poll); - } - API.StatusAction statusAction = (API.StatusAction) b.getSerializable("action"); if( status != null && statusListAdapter != null) { - statusListAdapter.notifyStatusWithActionChanged(statusAction, status); + statusListAdapter.notifyStatusWithActionChanged(status); } } };