use of com.odysee.app.adapter.RewardListAdapter 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.adapter.RewardListAdapter in project odysee-android by OdyseeTeam.
the class RewardsFragment method onCreateView.
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_rewards, container, false);
linkFilterUnclaimed = root.findViewById(R.id.rewards_filter_link_unclaimed);
linkFilterAll = root.findViewById(R.id.rewards_filter_link_all);
rewardList = root.findViewById(R.id.rewards_list);
rewardsLoading = root.findViewById(R.id.rewards_list_loading);
Context context = getContext();
LinearLayoutManager llm = new LinearLayoutManager(context);
rewardList.setLayoutManager(llm);
adapter = new RewardListAdapter(Lbryio.allRewards, context);
adapter.setClickListener(this);
adapter.setDisplayMode(RewardListAdapter.DISPLAY_MODE_UNCLAIMED);
rewardList.setAdapter(adapter);
initUi();
return root;
}
Aggregations