use of com.odysee.app.model.lbryinc.Reward in project odysee-android by OdyseeTeam.
the class RewardsFragment method fetchRewards.
private void fetchRewards() {
Helper.setViewVisibility(rewardList, View.INVISIBLE);
rewardsLoading.setVisibility(View.VISIBLE);
Activity activity = getActivity();
String authToken;
AccountManager am = AccountManager.get(getContext());
Account odyseeAccount = Helper.getOdyseeAccount(am.getAccounts());
if (odyseeAccount != null) {
authToken = am.peekAuthToken(odyseeAccount, "auth_token_type");
} else {
authToken = "";
}
Map<String, String> options = new HashMap<>();
options.put("multiple_rewards_per_type", "true");
if (odyseeAccount != null && authToken != null) {
options.put("auth_token", authToken);
}
ExecutorService executorService = Executors.newSingleThreadExecutor();
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
Supplier<List<Reward>> supplier = new FetchRewardsSupplier(options);
CompletableFuture<List<Reward>> cf = CompletableFuture.supplyAsync(supplier, executorService);
cf.exceptionally(e -> {
if (activity != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
((MainActivity) activity).showError(e.getMessage());
}
});
}
return null;
}).thenAccept(rewards -> {
Lbryio.updateRewardsLists(rewards);
if (activity != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
rewardsLoading.setVisibility(View.GONE);
if (adapter == null) {
adapter = new RewardListAdapter(rewards, getContext());
adapter.setClickListener(RewardsFragment.this);
adapter.setDisplayMode(RewardListAdapter.DISPLAY_MODE_UNCLAIMED);
rewardList.setAdapter(adapter);
} else {
adapter.setRewards(rewards);
}
Helper.setViewVisibility(rewardList, View.VISIBLE);
}
});
}
});
} else {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
Callable<List<Reward>> callable = new Callable<List<Reward>>() {
@Override
public List<Reward> call() {
List<Reward> rewards = null;
try {
JSONArray results = (JSONArray) Lbryio.parseResponse(Lbryio.call("reward", "list", options, null));
rewards = new ArrayList<>();
if (results != null) {
for (int i = 0; i < results.length(); i++) {
rewards.add(Reward.fromJSONObject(results.getJSONObject(i)));
}
}
} catch (ClassCastException | LbryioRequestException | LbryioResponseException | JSONException ex) {
if (activity != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
((MainActivity) activity).showError(ex.getMessage());
}
});
}
}
return rewards;
}
};
Future<List<Reward>> future = executorService.submit(callable);
try {
List<Reward> rewards = future.get();
if (rewards != null) {
Lbryio.updateRewardsLists(rewards);
if (activity != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
rewardsLoading.setVisibility(View.GONE);
if (adapter == null) {
adapter = new RewardListAdapter(rewards, getContext());
adapter.setClickListener(RewardsFragment.this);
adapter.setDisplayMode(RewardListAdapter.DISPLAY_MODE_UNCLAIMED);
rewardList.setAdapter(adapter);
} else {
adapter.setRewards(rewards);
}
Helper.setViewVisibility(rewardList, View.VISIBLE);
}
});
}
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
});
t.start();
}
}
use of com.odysee.app.model.lbryinc.Reward in project odysee-android by OdyseeTeam.
the class MainActivity method fetchRewards.
private void fetchRewards() {
String authToken;
AccountManager am = AccountManager.get(this);
Account odyseeAccount = Helper.getOdyseeAccount(am.getAccounts());
if (odyseeAccount != null) {
authToken = am.peekAuthToken(odyseeAccount, "auth_token_type");
} else {
authToken = "";
}
Map<String, String> options = new HashMap<>();
options.put("multiple_rewards_per_type", "true");
if (odyseeAccount != null && authToken != null) {
options.put("auth_token", authToken);
}
ExecutorService executorService = Executors.newSingleThreadExecutor();
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
Supplier<List<Reward>> supplier = new FetchRewardsSupplier(options);
CompletableFuture<List<Reward>> cf = CompletableFuture.supplyAsync(supplier, executorService);
cf.exceptionally(e -> {
runOnUiThread(new Runnable() {
@Override
public void run() {
showError(e.getMessage());
}
});
return null;
}).thenAccept(rewards -> {
Lbryio.updateRewardsLists(rewards);
if (Lbryio.totalUnclaimedRewardAmount > 0) {
updateRewardsUsdValue();
}
});
} else {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
Callable<List<Reward>> callable = new Callable<List<Reward>>() {
@Override
public List<Reward> call() {
List<Reward> rewards = null;
try {
JSONArray results = (JSONArray) Lbryio.parseResponse(Lbryio.call("reward", "list", options, null));
rewards = new ArrayList<>();
if (results != null) {
for (int i = 0; i < results.length(); i++) {
rewards.add(Reward.fromJSONObject(results.getJSONObject(i)));
}
}
} catch (ClassCastException | LbryioRequestException | LbryioResponseException | JSONException ex) {
runOnUiThread(new Runnable() {
@Override
public void run() {
showError(ex.getMessage());
}
});
}
return rewards;
}
};
Future<List<Reward>> future = executorService.submit(callable);
try {
List<Reward> rewards = future.get();
if (rewards != null) {
Lbryio.updateRewardsLists(rewards);
if (Lbryio.totalUnclaimedRewardAmount > 0) {
updateRewardsUsdValue();
}
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
});
t.start();
}
}
use of com.odysee.app.model.lbryinc.Reward in project odysee-android by OdyseeTeam.
the class FetchRewardsSupplier method get.
@Override
public List<Reward> get() {
List<Reward> rewards = new ArrayList<>();
try {
JSONArray results = (JSONArray) Lbryio.parseResponse(Lbryio.call("reward", "list", options, null));
rewards = new ArrayList<>();
if (results != null) {
for (int i = 0; i < results.length(); i++) {
rewards.add(Reward.fromJSONObject(results.getJSONObject(i)));
}
}
} catch (LbryioResponseException | JSONException | LbryioRequestException e) {
e.printStackTrace();
}
return rewards;
}
use of com.odysee.app.model.lbryinc.Reward in project odysee-android by OdyseeTeam.
the class RewardListAdapter method onBindViewHolder.
@Override
public void onBindViewHolder(RewardListAdapter.ViewHolder vh, int position) {
Reward reward = items.get(position);
String displayAmount = reward.getDisplayAmount();
double rewardAmount = 0;
if (!"?".equals(displayAmount)) {
rewardAmount = Double.valueOf(displayAmount);
}
boolean hasTransaction = !Helper.isNullOrEmpty(reward.getTransactionId()) && reward.getTransactionId().length() > 7;
vh.iconClaimed.setVisibility(reward.isClaimed() ? View.VISIBLE : View.INVISIBLE);
vh.inputCustomCode.setVisibility(reward.isCustom() ? View.VISIBLE : View.GONE);
vh.buttonClaimCustom.setVisibility(reward.isCustom() ? View.VISIBLE : View.GONE);
vh.textTitle.setText(reward.getRewardTitle());
vh.textDescription.setText(reward.getRewardDescription());
vh.upTo.setVisibility(reward.shouldDisplayRange() ? View.VISIBLE : View.GONE);
vh.lbcValue.setText(reward.isCustom() ? "?" : Helper.LBC_CURRENCY_FORMAT.format(Helper.parseDouble(reward.getDisplayAmount(), 0)));
vh.textLinkTransaction.setVisibility(hasTransaction ? View.VISIBLE : View.GONE);
vh.textLinkTransaction.setText(hasTransaction ? reward.getTransactionId().substring(0, 7) : null);
vh.textLinkTransaction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (context != null) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(String.format("%s/%s", Helper.EXPLORER_TX_PREFIX, reward.getTransactionId())));
context.startActivity(intent);
}
}
});
vh.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (reward.isClaimed()) {
return;
}
if (vh.inputCustomCode != null && !vh.inputCustomCode.hasFocus()) {
vh.inputCustomCode.requestFocus();
}
if (clickListener != null) {
clickListener.onRewardClicked(reward, vh.loading);
}
}
});
vh.inputCustomCode.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String value = charSequence.toString().trim();
vh.buttonClaimCustom.setEnabled(value.length() > 0);
}
@Override
public void afterTextChanged(Editable editable) {
}
});
vh.buttonClaimCustom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String claimCode = Helper.getValue(vh.inputCustomCode.getText());
if (Helper.isNullOrEmpty(claimCode)) {
Snackbar.make(view, R.string.please_enter_claim_code, Snackbar.LENGTH_LONG).setBackgroundTint(Color.RED).setTextColor(Color.WHITE).show();
return;
}
if (clickListener != null) {
clickListener.onCustomClaimButtonClicked(claimCode, vh.inputCustomCode, vh.buttonClaimCustom, vh.loading);
}
}
});
}
use of com.odysee.app.model.lbryinc.Reward in project odysee-android by OdyseeTeam.
the class Lbryio method updateRewardsLists.
public static void updateRewardsLists(List<Reward> rewards) {
synchronized (lock) {
allRewards.clear();
unclaimedRewards.clear();
totalUnclaimedRewardAmount = 0;
for (int i = 0; i < rewards.size(); i++) {
Reward reward = rewards.get(i);
allRewards.add(reward);
if (!reward.isClaimed()) {
unclaimedRewards.add(reward);
totalUnclaimedRewardAmount += reward.getRewardAmount();
}
}
}
}
Aggregations