From c95d7045fa7a654f52789b5aa677faeb296e27e5 Mon Sep 17 00:00:00 2001 From: stom79 Date: Thu, 10 Jan 2019 12:00:14 +0100 Subject: [PATCH] Fix issue with counters --- .../fr/gouv/etalab/mastodon/client/API.java | 1 + .../mastodon/drawers/StatusListAdapter.java | 24 +++++++++++++------ .../fragments/DisplayStatusFragment.java | 11 +++------ 3 files changed, 21 insertions(+), 15 deletions(-) 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 fd02698a2..8435f8e8c 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 @@ -1750,6 +1750,7 @@ public class API { try { Status status1 = parseStatuses(context, new JSONObject(resp)); b.putParcelable("status", status1); + b.putSerializable("action", statusAction); } catch (JSONException ignored) {} b.putSerializable("action", statusAction); Intent intentBC = new Intent(Helper.RECEIVE_ACTION); 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 354b6c3b8..5bf97a920 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 @@ -2700,17 +2700,27 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct } } - public void notifyStatusWithActionChanged(Status status){ + public void notifyStatusWithActionChanged(API.StatusAction statusAction, 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 countFav = statuses.get(i).getFavourites_count() -1; - if( countFav < 0) - countFav = 0; - int countReblog = statuses.get(i).getReblogs_count() -1; - if( countReblog < 0) - countReblog = 0; + int j; + int countFav = statuses.get(i).getFavourites_count(); + int countReblog = statuses.get(i).getReblogs_count(); + if( statusAction == API.StatusAction.UNFAVOURITE){ + countFav--; + if( countFav < 0) + countFav = 0; + }else if( statusAction == API.StatusAction.FAVOURITE){ + countFav++; + }else if( statusAction == API.StatusAction.UNREBLOG){ + countReblog--; + if( countReblog < 0) + countReblog = 0; + }else if( statusAction == API.StatusAction.REBLOG){ + countReblog++; + } statuses.get(i).setFavourited(status.isFavourited()); statuses.get(i).setFavourites_count(countFav); statuses.get(i).setReblogged(status.isReblogged()); 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 e93fac3a5..6aceff51d 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 @@ -51,6 +51,7 @@ import fr.gouv.etalab.mastodon.asynctasks.RetrieveFeedsAsyncTask; import fr.gouv.etalab.mastodon.asynctasks.RetrieveMissingFeedsAsyncTask; import fr.gouv.etalab.mastodon.asynctasks.RetrievePeertubeSearchAsyncTask; import fr.gouv.etalab.mastodon.asynctasks.UpdateAccountInfoAsyncTask; +import fr.gouv.etalab.mastodon.client.API; import fr.gouv.etalab.mastodon.client.APIResponse; import fr.gouv.etalab.mastodon.client.Entities.Account; import fr.gouv.etalab.mastodon.client.Entities.Conversation; @@ -216,8 +217,9 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn Bundle b = intent.getExtras(); assert b != null; Status status = b.getParcelable("status"); + API.StatusAction statusAction = (API.StatusAction)b.getSerializable("action"); if( status != null) { - applyAction(status); + statusListAdapter.notifyStatusWithActionChanged(statusAction, status); } } }; @@ -426,7 +428,6 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn super.onDestroy(); if(asyncTask != null && asyncTask.getStatus() == AsyncTask.Status.RUNNING) asyncTask.cancel(true); - Log.v(Helper.TAG,type + " - destroy: " + receive_action); if( receive_action != null) LocalBroadcastManager.getInstance(context).unregisterReceiver(receive_action); } @@ -938,10 +939,4 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn } } - - public void applyAction(Status status){ - if( statusListAdapter != null && this.statuses != null && this.statuses.contains(status)){ - statusListAdapter.notifyStatusWithActionChanged(status); - } - } }