use of com.odysee.app.callable.BuildCommentReactOptions in project odysee-android by OdyseeTeam.
the class FileViewFragment method react.
private void react(Comment comment, boolean like) {
JSONObject options = new JSONObject();
try {
options.put("comment_ids", comment.getId());
options.put("type", like ? "like" : "dislike");
options.put("clear_types", like ? "dislike" : "like");
/**
* This covers the case of a fresh comment being made and added to the list, and then liked by
* the commenter themself, but the {@link Reactions} field is not instantiated in this case. It
* would perhaps be better to fix this upstream and make this situation impossible, but even then
* this last line of defense doesn't hurt.
*/
if (comment.getReactions() == null) {
comment.setReactions(Reactions.newInstanceWithNoLikesOrDislikes());
}
if ((like && comment.getReactions().isLiked()) || (!like && comment.getReactions().isDisliked())) {
options.put("remove", true);
}
AccountManager am = AccountManager.get(getContext());
Account odyseeAccount = Helper.getOdyseeAccount(am.getAccounts());
if (odyseeAccount != null) {
options.put("auth_token", am.peekAuthToken(odyseeAccount, "auth_token_type"));
}
} catch (JSONException e) {
e.printStackTrace();
}
ExecutorService executor = Executors.newSingleThreadExecutor();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
// This makes a network connection, so it needs to be executed on a different thread than main.
if (Lbry.ownChannels.size() > 0) {
try {
// This makes a network connection, so it needs to be executed on a different thread than main.
Callable<JSONObject> optionsCallable = new BuildCommentReactOptions(options);
Future<JSONObject> optionsFuture = executor.submit(optionsCallable);
JSONObject opt = optionsFuture.get();
Future<?> futureReactions = executor.submit(new ReactToComment(opt));
futureReactions.get();
if (!executor.isShutdown()) {
executor.shutdown();
}
refreshCommentAfterReacting(comment);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (!executor.isShutdown()) {
executor.shutdown();
}
}
}
}
});
thread.start();
}
Aggregations