Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refreshing faster user reaction UI status for content #42

Merged
merged 1 commit into from
Oct 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -3788,6 +3790,8 @@ public void run() {

private void react(Claim claim, boolean like) {
Runnable runnable = () -> {
purgeLoadingReactionsTask();

Map<String, String> options = new HashMap<>();
options.put("claim_ids", claim.getClaimId());
options.put("type", like ? "like" : "dislike");
Expand All @@ -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);
}
};

Expand Down