use of com.odysee.app.supplier.FetchRewardsSupplier 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.supplier.FetchRewardsSupplier 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();
}
}
Aggregations