From 4af6d22aa785d85be83bce1e443e9c3ef426de58 Mon Sep 17 00:00:00 2001 From: Javi Rueda Date: Wed, 20 Oct 2021 21:59:20 +0200 Subject: [PATCH] Refresh faster user reaction UI status for content --- .../app/ui/findcontent/FileViewFragment.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/odysee/app/ui/findcontent/FileViewFragment.java b/app/src/main/java/com/odysee/app/ui/findcontent/FileViewFragment.java index 45782976..fc974326 100644 --- a/app/src/main/java/com/odysee/app/ui/findcontent/FileViewFragment.java +++ b/app/src/main/java/com/odysee/app/ui/findcontent/FileViewFragment.java @@ -194,8 +194,6 @@ import okhttp3.Request; import okhttp3.Response; -import static java.util.stream.Collectors.groupingBy; - public class FileViewFragment extends BaseFragment implements MainActivity.BackPressInterceptor, DownloadActionListener, @@ -839,6 +837,10 @@ public void onStop() { // Tasks on the scheduled executor needs to be really terminated to avoid // crashes if user presses back after going to a related content from here + purgeLoadingReactionsTask(); + } + + private void purgeLoadingReactionsTask() { if (scheduledExecutor != null && !scheduledExecutor.isShutdown() && futureReactions != null) { try { // .cancel() will not remove the task, so it is needed to .purge() @@ -3788,6 +3790,8 @@ public void run() { private void react(Claim claim, boolean like) { Runnable runnable = () -> { + purgeLoadingReactionsTask(); + Map options = new HashMap<>(); options.put("claim_ids", claim.getClaimId()); options.put("type", like ? "like" : "dislike"); @@ -3797,9 +3801,17 @@ private void react(Claim claim, boolean like) { options.put("remove", "true"); try { - Lbryio.call("reaction", "react", options, Helper.METHOD_POST, getContext()); - } catch (LbryioRequestException | LbryioResponseException e) { + JSONObject jsonResponse = (JSONObject) Lbryio.parseResponse(Lbryio.call("reaction", "react", options, Helper.METHOD_POST, getContext())); + + if (jsonResponse != null && jsonResponse.has(claim.getClaimId())) { + reactions.setLiked(jsonResponse.getJSONObject(claim.getClaimId()).has("like") && !reactions.isLiked()); + reactions.setDisliked(jsonResponse.getJSONObject(claim.getClaimId()).has("dislike") && !reactions.isDisliked()); + updateContentReactions(); + } + } catch (LbryioRequestException | LbryioResponseException | JSONException e) { e.printStackTrace(); + } finally { + loadReactions(claim); } };