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);
}
}
Aggregations