use of com.odysee.app.MainActivity 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.MainActivity in project odysee-android by OdyseeTeam.
the class WalletFragment method onStop.
public void onStop() {
Context context = getContext();
if (context instanceof MainActivity) {
MainActivity activity = (MainActivity) context;
activity.removeWalletBalanceListener(this);
}
super.onStop();
}
use of com.odysee.app.MainActivity in project odysee-android by OdyseeTeam.
the class WalletFragment method onResume.
public void onResume() {
super.onResume();
Context context = getContext();
// Helper.setWunderbarValue(null, context);
if (context instanceof MainActivity) {
MainActivity activity = (MainActivity) context;
if (!activity.isSignedIn()) {
return;
}
activity.syncWalletAndLoadPreferences();
LbryAnalytics.setCurrentScreen(activity, "Wallet", "Wallet");
}
checkReceiveAddress();
checkRewardsDriver();
fetchRecentTransactions();
}
use of com.odysee.app.MainActivity in project odysee-android by OdyseeTeam.
the class WalletFragment method fetchRecentTransactions.
private void fetchRecentTransactions(boolean forceFetch) {
if (!Helper.isSignedIn(getContext())) {
return;
}
if (hasFetchedRecentTransactions && !forceFetch) {
return;
}
Helper.setViewVisibility(textNoRecentTransactions, View.GONE);
AccountManager am = AccountManager.get(getContext());
Account[] accounts = am.getAccounts();
TransactionListTask task = new TransactionListTask(1, 5, am.peekAuthToken(Helper.getOdyseeAccount(accounts), "auth_token_type"), loadingRecentContainer, new TransactionListTask.TransactionListHandler() {
@Override
public void onSuccess(List<Transaction> transactions, boolean hasReachedEnd) {
hasFetchedRecentTransactions = true;
recentTransactionsAdapter = new TransactionListAdapter(transactions, getContext());
recentTransactionsAdapter.setListener(new TransactionListAdapter.TransactionClickListener() {
@Override
public void onTransactionClicked(Transaction transaction) {
}
@Override
public void onClaimUrlClicked(LbryUri uri) {
Context context = getContext();
if (uri != null && context instanceof MainActivity) {
MainActivity activity = (MainActivity) context;
if (uri.isChannel()) {
activity.openChannelUrl(uri.toString());
} else {
activity.openFileUrl(uri.toString());
}
}
}
});
recentTransactionsList.setAdapter(recentTransactionsAdapter);
displayNoRecentTransactions();
}
@Override
public void onError(Exception error) {
hasFetchedRecentTransactions = true;
displayNoRecentTransactions();
}
});
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
use of com.odysee.app.MainActivity in project odysee-android by OdyseeTeam.
the class WalletFragment method onWalletBalanceUpdated.
public void onWalletBalanceUpdated(WalletBalance walletBalance) {
if (walletBalance == null) {
return;
}
double totalBalance = walletBalance.getTotal().doubleValue();
double spendableBalance = walletBalance.getAvailable().doubleValue();
double supportingBalance = walletBalance.getClaims().doubleValue() + walletBalance.getTips().doubleValue() + walletBalance.getSupports().doubleValue();
double usdBalance = totalBalance * Lbryio.LBCUSDRate;
double tipsBalance = walletBalance.getTips().doubleValue();
if (detailRows == null)
detailRows = new ArrayList<>(3);
if (detailAdapter == null) {
detailAdapter = new WalletDetailAdapter(getContext(), detailRows);
detailListView.setAdapter(detailAdapter);
}
Activity activity = (Activity) getContext();
boolean tipsBeenUnlocked;
if (activity instanceof MainActivity)
tipsBeenUnlocked = ((MainActivity) activity).isUnlockingTips();
else
tipsBeenUnlocked = false;
WalletDetailItem earnedBalance = new WalletDetailItem(getResources().getString(R.string.earned_from_others), getResources().getString(R.string.unlock_to_spend), Helper.SIMPLE_CURRENCY_FORMAT.format(tipsBalance), tipsBalance != 0, tipsBeenUnlocked);
WalletDetailItem initialPublishes = new WalletDetailItem(getResources().getString(R.string.on_initial_publishes), getResources().getString(R.string.delete_or_edit_past_content), Helper.SIMPLE_CURRENCY_FORMAT.format(walletBalance.getClaims().doubleValue()), false, false);
WalletDetailItem supportingContent = new WalletDetailItem(getResources().getString(R.string.supporting_content), getResources().getString(R.string.delete_supports_to_spend), Helper.SIMPLE_CURRENCY_FORMAT.format(walletBalance.getSupports().doubleValue()), false, false);
if (detailRows.size() == 0) {
detailRows.add(0, earnedBalance);
detailRows.add(1, initialPublishes);
detailRows.add(2, supportingContent);
} else {
if (!detailRows.get(0).detailAmount.equals(earnedBalance.detailAmount) || detailRows.get(0).isInProgress != earnedBalance.isInProgress || detailRows.get(0).isUnlockable != earnedBalance.isUnlockable) {
detailRows.set(0, earnedBalance);
}
if (!detailRows.get(1).detailAmount.equals(initialPublishes.detailAmount)) {
detailRows.set(1, initialPublishes);
}
if (!detailRows.get(2).detailAmount.equals(supportingContent.detailAmount)) {
detailRows.set(2, supportingContent);
}
}
detailListView.setAdapter(detailAdapter);
String formattedTotalBalance = Helper.REDUCED_LBC_CURRENCY_FORMAT.format(totalBalance);
String formattedSpendableBalance = Helper.SIMPLE_CURRENCY_FORMAT.format(spendableBalance);
String formattedSupportingBalance = Helper.SIMPLE_CURRENCY_FORMAT.format(supportingBalance);
Helper.setViewText(walletTotalBalanceView, totalBalance > 0 && formattedTotalBalance.equals("0") ? Helper.FULL_LBC_CURRENCY_FORMAT.format(totalBalance) : formattedTotalBalance);
Helper.setViewText(walletSpendableBalanceView, spendableBalance > 0 && formattedSpendableBalance.equals("0") ? Helper.FULL_LBC_CURRENCY_FORMAT.format(spendableBalance) : formattedSpendableBalance);
Helper.setViewText(walletSupportingBalanceView, supportingBalance > 0 && formattedSupportingBalance.equals("0") ? Helper.FULL_LBC_CURRENCY_FORMAT.format(supportingBalance) : formattedSupportingBalance);
Helper.setViewText(textWalletInlineBalance, Helper.shortCurrencyFormat(spendableBalance));
if (Lbryio.LBCUSDRate > 0) {
// only update display usd values if the rate is loaded
Helper.setViewText(textWalletBalanceUSD, String.format("≈$%s", Helper.SIMPLE_CURRENCY_FORMAT.format(usdBalance)));
}
textWalletBalanceDesc.setText(spendableBalance == totalBalance ? getResources().getString(R.string.your_total_balance) : getResources().getString(R.string.all_of_this_is_yours));
fetchRecentTransactions(true);
checkRewardsDriver();
}
Aggregations