Search in sources :

Example 1 with ClaimRewardSupplier

use of com.odysee.app.supplier.ClaimRewardSupplier in project odysee-android by OdyseeTeam.

the class RewardsFragment method claimReward.

private void claimReward(String type, String code, EditText inputClaimCode, MaterialButton buttonClaim, View loadingView) {
    rewardClaimInProgress = true;
    Helper.setViewEnabled(buttonClaim, false);
    Helper.setViewEnabled(inputClaimCode, false);
    Activity activity = getActivity();
    if (activity != null) {
        activity.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                loadingView.setVisibility(View.VISIBLE);
            }
        });
    }
    final AccountManager am = AccountManager.get(getContext());
    final String authToken = am.peekAuthToken(Helper.getOdyseeAccount(am.getAccounts()), "auth_token_type");
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
        Supplier<JSONObject> s = new ClaimRewardSupplier(type, code, authToken);
        CompletableFuture<JSONObject> cf = CompletableFuture.supplyAsync(s);
        cf.whenComplete((result, e) -> {
            afterClaimingReward(inputClaimCode, buttonClaim, loadingView, activity, result, e);
        });
    } else {
        ClaimRewardTask task = new ClaimRewardTask(type, code, authToken, new ClaimRewardTask.ClaimRewardHandler() {

            @Override
            public void onSuccess(double amountClaimed, String message) {
                loadingView.setVisibility(View.INVISIBLE);
                if (Helper.isNullOrEmpty(message)) {
                    message = getResources().getQuantityString(R.plurals.claim_reward_message, amountClaimed == 1 ? 1 : 2, new DecimalFormat(Helper.LBC_CURRENCY_FORMAT_PATTERN).format(amountClaimed));
                }
                View view = getView();
                if (view != null) {
                    Snackbar.make(view, message, Snackbar.LENGTH_LONG).show();
                }
                Helper.setViewEnabled(buttonClaim, true);
                Helper.setViewEnabled(inputClaimCode, true);
                rewardClaimInProgress = false;
                fetchRewards();
            }

            @Override
            public void onError(Exception error) {
                loadingView.setVisibility(View.INVISIBLE);
                View view = getView();
                if (view != null && error != null && !Helper.isNullOrEmpty(error.getMessage())) {
                    Snackbar.make(view, error.getMessage(), Snackbar.LENGTH_LONG).setBackgroundTint(Color.RED).setTextColor(Color.WHITE).show();
                }
                Helper.setViewEnabled(buttonClaim, true);
                Helper.setViewEnabled(inputClaimCode, true);
                rewardClaimInProgress = false;
            }
        });
        task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }
}
Also used : DecimalFormat(java.text.DecimalFormat) MainActivity(com.odysee.app.MainActivity) Activity(android.app.Activity) ClaimRewardSupplier(com.odysee.app.supplier.ClaimRewardSupplier) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) TextView(android.widget.TextView) JSONException(org.json.JSONException) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException) ExecutionException(java.util.concurrent.ExecutionException) ClaimRewardTask(com.odysee.app.tasks.lbryinc.ClaimRewardTask) JSONObject(org.json.JSONObject) AccountManager(android.accounts.AccountManager)

Aggregations

AccountManager (android.accounts.AccountManager)1 Activity (android.app.Activity)1 View (android.view.View)1 TextView (android.widget.TextView)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 MainActivity (com.odysee.app.MainActivity)1 LbryioRequestException (com.odysee.app.exceptions.LbryioRequestException)1 LbryioResponseException (com.odysee.app.exceptions.LbryioResponseException)1 ClaimRewardSupplier (com.odysee.app.supplier.ClaimRewardSupplier)1 ClaimRewardTask (com.odysee.app.tasks.lbryinc.ClaimRewardTask)1 DecimalFormat (java.text.DecimalFormat)1 ExecutionException (java.util.concurrent.ExecutionException)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1