Search in sources :

Example 1 with ClaimRewardTask

use of com.odysee.app.tasks.lbryinc.ClaimRewardTask 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)

Example 2 with ClaimRewardTask

use of com.odysee.app.tasks.lbryinc.ClaimRewardTask in project odysee-android by OdyseeTeam.

the class MainActivity method checkAndClaimNewAndroidReward.

public void checkAndClaimNewAndroidReward() {
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
    boolean rewardClaimed = sp.getBoolean(PREFERENCE_KEY_INTERNAL_NEW_ANDROID_REWARD_CLAIMED, false);
    if (!rewardClaimed) {
        ClaimRewardTask task = new ClaimRewardTask(Reward.TYPE_NEW_ANDROID, null, null, new ClaimRewardTask.ClaimRewardHandler() {

            @Override
            public void onSuccess(double amountClaimed, String message) {
                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));
                }
                Snackbar.make(findViewById(R.id.content_main), message, Snackbar.LENGTH_LONG).show();
                sp.edit().putBoolean(PREFERENCE_KEY_INTERNAL_NEW_ANDROID_REWARD_CLAIMED, true).apply();
            }

            @Override
            public void onError(Exception error) {
                // pass. fail silently
                sp.edit().putBoolean(PREFERENCE_KEY_INTERNAL_NEW_ANDROID_REWARD_CLAIMED, true).apply();
            }
        });
        task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) DecimalFormat(java.text.DecimalFormat) SpannableString(android.text.SpannableString) JSONException(org.json.JSONException) LbryUriException(com.odysee.app.exceptions.LbryUriException) ExecutionException(java.util.concurrent.ExecutionException) SQLiteException(android.database.sqlite.SQLiteException) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException) ApiCallException(com.odysee.app.exceptions.ApiCallException) AuthTokenInvalidatedException(com.odysee.app.exceptions.AuthTokenInvalidatedException) ParseException(java.text.ParseException) ClaimRewardTask(com.odysee.app.tasks.lbryinc.ClaimRewardTask)

Example 3 with ClaimRewardTask

use of com.odysee.app.tasks.lbryinc.ClaimRewardTask in project odysee-android by OdyseeTeam.

the class FileViewFragment method claimEligibleRewards.

private void claimEligibleRewards() {
    // attempt to claim eligible rewards after viewing or playing a file (fail silently)
    final String authToken = Lbryio.AUTH_TOKEN;
    ClaimRewardTask firstStreamTask = new ClaimRewardTask(Reward.TYPE_FIRST_STREAM, null, authToken, eligibleRewardHandler);
    ClaimRewardTask dailyViewTask = new ClaimRewardTask(Reward.TYPE_DAILY_VIEW, null, authToken, eligibleRewardHandler);
    firstStreamTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    dailyViewTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Also used : ClaimRewardTask(com.odysee.app.tasks.lbryinc.ClaimRewardTask)

Aggregations

ClaimRewardTask (com.odysee.app.tasks.lbryinc.ClaimRewardTask)3 LbryioRequestException (com.odysee.app.exceptions.LbryioRequestException)2 LbryioResponseException (com.odysee.app.exceptions.LbryioResponseException)2 DecimalFormat (java.text.DecimalFormat)2 ExecutionException (java.util.concurrent.ExecutionException)2 JSONException (org.json.JSONException)2 AccountManager (android.accounts.AccountManager)1 Activity (android.app.Activity)1 SharedPreferences (android.content.SharedPreferences)1 SQLiteException (android.database.sqlite.SQLiteException)1 SpannableString (android.text.SpannableString)1 View (android.view.View)1 TextView (android.widget.TextView)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 MainActivity (com.odysee.app.MainActivity)1 ApiCallException (com.odysee.app.exceptions.ApiCallException)1 AuthTokenInvalidatedException (com.odysee.app.exceptions.AuthTokenInvalidatedException)1 LbryUriException (com.odysee.app.exceptions.LbryUriException)1 ClaimRewardSupplier (com.odysee.app.supplier.ClaimRewardSupplier)1 ParseException (java.text.ParseException)1