Search in sources :

Example 1 with ReactToComment

use of com.odysee.app.runnable.ReactToComment 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();
}
Also used : Account(android.accounts.Account) JSONException(org.json.JSONException) Callable(java.util.concurrent.Callable) LbryRequestException(com.odysee.app.exceptions.LbryRequestException) JSONException(org.json.JSONException) LbryUriException(com.odysee.app.exceptions.LbryUriException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) LbryResponseException(com.odysee.app.exceptions.LbryResponseException) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException) ApiCallException(com.odysee.app.exceptions.ApiCallException) AnyThread(androidx.annotation.AnyThread) BuildCommentReactOptions(com.odysee.app.callable.BuildCommentReactOptions) JSONObject(org.json.JSONObject) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) CompletableFuture(java.util.concurrent.CompletableFuture) ScheduledFuture(java.util.concurrent.ScheduledFuture) AccountManager(android.accounts.AccountManager) ReactToComment(com.odysee.app.runnable.ReactToComment) TrackSelectionOverride(com.google.android.exoplayer2.trackselection.TrackSelectionOverrides.TrackSelectionOverride)

Aggregations

Account (android.accounts.Account)1 AccountManager (android.accounts.AccountManager)1 AnyThread (androidx.annotation.AnyThread)1 TrackSelectionOverride (com.google.android.exoplayer2.trackselection.TrackSelectionOverrides.TrackSelectionOverride)1 BuildCommentReactOptions (com.odysee.app.callable.BuildCommentReactOptions)1 ApiCallException (com.odysee.app.exceptions.ApiCallException)1 LbryRequestException (com.odysee.app.exceptions.LbryRequestException)1 LbryResponseException (com.odysee.app.exceptions.LbryResponseException)1 LbryUriException (com.odysee.app.exceptions.LbryUriException)1 LbryioRequestException (com.odysee.app.exceptions.LbryioRequestException)1 LbryioResponseException (com.odysee.app.exceptions.LbryioResponseException)1 ReactToComment (com.odysee.app.runnable.ReactToComment)1 IOException (java.io.IOException)1 Callable (java.util.concurrent.Callable)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1