Search in sources :

Example 1 with MainActivity

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();
    }
}
Also used : Typeface(android.graphics.Typeface) Context(android.content.Context) Bundle(android.os.Bundle) ProgressBar(android.widget.ProgressBar) NonNull(androidx.annotation.NonNull) ClaimRewardTask(com.odysee.app.tasks.lbryinc.ClaimRewardTask) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) CompletableFuture(java.util.concurrent.CompletableFuture) ClaimRewardSupplier(com.odysee.app.supplier.ClaimRewardSupplier) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) MaterialButton(com.google.android.material.button.MaterialButton) MainActivity(com.odysee.app.MainActivity) Map(java.util.Map) View(android.view.View) LbryioRequestException(com.odysee.app.exceptions.LbryioRequestException) RecyclerView(androidx.recyclerview.widget.RecyclerView) Build(android.os.Build) RewardListAdapter(com.odysee.app.adapter.RewardListAdapter) ExecutorService(java.util.concurrent.ExecutorService) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException) AccountManager(android.accounts.AccountManager) AsyncTask(android.os.AsyncTask) Account(android.accounts.Account) LayoutInflater(android.view.LayoutInflater) DecimalFormat(java.text.DecimalFormat) Helper(com.odysee.app.utils.Helper) FetchRewardsSupplier(com.odysee.app.supplier.FetchRewardsSupplier) ViewGroup(android.view.ViewGroup) Executors(java.util.concurrent.Executors) Color(android.graphics.Color) ExecutionException(java.util.concurrent.ExecutionException) Lbryio(com.odysee.app.utils.Lbryio) List(java.util.List) TextView(android.widget.TextView) Reward(com.odysee.app.model.lbryinc.Reward) BaseFragment(com.odysee.app.ui.BaseFragment) LbryAnalytics(com.odysee.app.utils.LbryAnalytics) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) Activity(android.app.Activity) Snackbar(com.google.android.material.snackbar.Snackbar) EditText(android.widget.EditText) R(com.odysee.app.R) JSONArray(org.json.JSONArray) Account(android.accounts.Account) HashMap(java.util.HashMap) RewardListAdapter(com.odysee.app.adapter.RewardListAdapter) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) MainActivity(com.odysee.app.MainActivity) Activity(android.app.Activity) MainActivity(com.odysee.app.MainActivity) Callable(java.util.concurrent.Callable) FetchRewardsSupplier(com.odysee.app.supplier.FetchRewardsSupplier) ExecutorService(java.util.concurrent.ExecutorService) CompletableFuture(java.util.concurrent.CompletableFuture) Future(java.util.concurrent.Future) AccountManager(android.accounts.AccountManager) ArrayList(java.util.ArrayList) List(java.util.List) Reward(com.odysee.app.model.lbryinc.Reward)

Example 2 with MainActivity

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();
}
Also used : Context(android.content.Context) MainActivity(com.odysee.app.MainActivity)

Example 3 with MainActivity

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();
}
Also used : Context(android.content.Context) MainActivity(com.odysee.app.MainActivity)

Example 4 with MainActivity

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);
}
Also used : Context(android.content.Context) Account(android.accounts.Account) TransactionListTask(com.odysee.app.tasks.wallet.TransactionListTask) MainActivity(com.odysee.app.MainActivity) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ApiCallException(com.odysee.app.exceptions.ApiCallException) ExecutionException(java.util.concurrent.ExecutionException) Transaction(com.odysee.app.model.Transaction) AccountManager(android.accounts.AccountManager) LbryUri(com.odysee.app.utils.LbryUri) TransactionListAdapter(com.odysee.app.adapter.TransactionListAdapter)

Example 5 with MainActivity

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();
}
Also used : WalletDetailItem(com.odysee.app.model.WalletDetailItem) ArrayList(java.util.ArrayList) MainActivity(com.odysee.app.MainActivity) Activity(android.app.Activity) WalletDetailAdapter(com.odysee.app.adapter.WalletDetailAdapter) MainActivity(com.odysee.app.MainActivity)

Aggregations

MainActivity (com.odysee.app.MainActivity)138 Context (android.content.Context)119 Claim (com.odysee.app.model.Claim)39 View (android.view.View)31 TextView (android.widget.TextView)30 RecyclerView (androidx.recyclerview.widget.RecyclerView)26 AttributeProviderContext (org.commonmark.renderer.html.AttributeProviderContext)25 ArrayList (java.util.ArrayList)21 TrackSelectionOverride (com.google.android.exoplayer2.trackselection.TrackSelectionOverrides.TrackSelectionOverride)19 ImageView (android.widget.ImageView)18 SuppressLint (android.annotation.SuppressLint)15 AdapterView (android.widget.AdapterView)14 NestedScrollView (androidx.core.widget.NestedScrollView)14 JSONException (org.json.JSONException)13 JSONObject (org.json.JSONObject)13 SolidIconView (com.odysee.app.ui.controls.SolidIconView)12 HashMap (java.util.HashMap)12 ClaimListAdapter (com.odysee.app.adapter.ClaimListAdapter)11 ApiCallException (com.odysee.app.exceptions.ApiCallException)11 WebView (android.webkit.WebView)10