[Android] pass WalletModel to static util methods
This commit is contained in:
+57
-34
@@ -32,6 +32,8 @@ import org.chromium.brave_wallet.mojom.NetworkInfo;
|
||||
import org.chromium.brave_wallet.mojom.TransactionInfo;
|
||||
import org.chromium.brave_wallet.mojom.TxService;
|
||||
import org.chromium.chrome.R;
|
||||
import org.chromium.chrome.browser.app.BraveActivity;
|
||||
import org.chromium.chrome.browser.app.domain.WalletModel;
|
||||
import org.chromium.chrome.browser.crypto_wallet.BlockchainRegistryFactory;
|
||||
import org.chromium.chrome.browser.crypto_wallet.activities.AddAccountActivity;
|
||||
import org.chromium.chrome.browser.crypto_wallet.adapters.WalletCoinAdapter;
|
||||
@@ -43,6 +45,7 @@ import org.chromium.chrome.browser.crypto_wallet.util.AssetUtils;
|
||||
import org.chromium.chrome.browser.crypto_wallet.util.PortfolioHelper;
|
||||
import org.chromium.chrome.browser.crypto_wallet.util.Utils;
|
||||
import org.chromium.chrome.browser.init.AsyncInitializationActivity;
|
||||
import org.chromium.chrome.browser.util.LiveDataUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -61,6 +64,7 @@ public class AccountDetailActivity
|
||||
private ExecutorService mExecutor;
|
||||
private Handler mHandler;
|
||||
private WalletCoinAdapter mWalletTxCoinAdapter;
|
||||
private WalletModel mWalletModel;
|
||||
|
||||
@Override
|
||||
protected void triggerLayoutInflation() {
|
||||
@@ -134,52 +138,67 @@ public class AccountDetailActivity
|
||||
|
||||
private void setUpAssetList(NetworkInfo selectedNetwork) {
|
||||
AccountInfo[] accountInfos = new AccountInfo[] {getThisAccountInfo()};
|
||||
PortfolioHelper portfolioHelper = new PortfolioHelper(this, accountInfos);
|
||||
portfolioHelper.setSelectedNetwork(selectedNetwork);
|
||||
portfolioHelper.calculateBalances(() -> {
|
||||
RecyclerView rvAssets = findViewById(R.id.rv_assets);
|
||||
if (mWalletModel == null) return;
|
||||
LiveDataUtil.observeOnce(
|
||||
mWalletModel.getCryptoModel().getNetworkModel().mCryptoNetworks, allNetworks -> {
|
||||
PortfolioHelper portfolioHelper =
|
||||
new PortfolioHelper(this, allNetworks, accountInfos);
|
||||
portfolioHelper.setSelectedNetwork(selectedNetwork);
|
||||
portfolioHelper.calculateBalances(() -> {
|
||||
RecyclerView rvAssets = findViewById(R.id.rv_assets);
|
||||
|
||||
BlockchainToken[] userAssets = portfolioHelper.getUserAssets();
|
||||
HashMap<String, Double> perTokenCryptoSum = portfolioHelper.getPerTokenCryptoSum();
|
||||
HashMap<String, Double> perTokenFiatSum = portfolioHelper.getPerTokenFiatSum();
|
||||
BlockchainToken[] userAssets = portfolioHelper.getUserAssets();
|
||||
HashMap<String, Double> perTokenCryptoSum =
|
||||
portfolioHelper.getPerTokenCryptoSum();
|
||||
HashMap<String, Double> perTokenFiatSum =
|
||||
portfolioHelper.getPerTokenFiatSum();
|
||||
|
||||
String tokensPath = BlockchainRegistryFactory.getInstance().getTokensIconsLocation();
|
||||
String tokensPath =
|
||||
BlockchainRegistryFactory.getInstance().getTokensIconsLocation();
|
||||
|
||||
WalletCoinAdapter walletCoinAdapter = Utils.setupVisibleAssetList(
|
||||
userAssets, perTokenCryptoSum, perTokenFiatSum, tokensPath);
|
||||
walletCoinAdapter.setOnWalletListItemClick(AccountDetailActivity.this);
|
||||
rvAssets.setAdapter(walletCoinAdapter);
|
||||
rvAssets.setLayoutManager(new LinearLayoutManager(this));
|
||||
});
|
||||
WalletCoinAdapter walletCoinAdapter = Utils.setupVisibleAssetList(
|
||||
userAssets, perTokenCryptoSum, perTokenFiatSum, tokensPath);
|
||||
walletCoinAdapter.setOnWalletListItemClick(AccountDetailActivity.this);
|
||||
rvAssets.setAdapter(walletCoinAdapter);
|
||||
rvAssets.setLayoutManager(new LinearLayoutManager(this));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private void fetchAccountInfo(NetworkInfo selectedNetwork) {
|
||||
assert mKeyringService != null;
|
||||
mKeyringService.getKeyringInfo(AssetUtils.getKeyringForCoinType(mCoinType), keyringInfo -> {
|
||||
if (keyringInfo == null) {
|
||||
if (keyringInfo == null || mWalletModel == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
AccountInfo[] accounts = keyringInfo.accountInfos;
|
||||
Utils.getTxExtraInfo(this, selectedNetwork, accounts, null, false,
|
||||
(assetPrices, fullTokenList, nativeAssetsBalances,
|
||||
blockchainTokensBalances) -> {
|
||||
for (AccountInfo accountInfo : accounts) {
|
||||
if (accountInfo.address.equals(mAddress)
|
||||
&& accountInfo.name.equals(mName)) {
|
||||
AccountInfo[] accountInfos = new AccountInfo[1];
|
||||
accountInfos[0] = accountInfo;
|
||||
WalletListItemModel thisAccountItemModel =
|
||||
new WalletListItemModel(Utils.getCoinIcon(mCoinType), mName,
|
||||
mAddress, null, null, mIsImported);
|
||||
Utils.setUpTransactionList(this, accountInfos, thisAccountItemModel,
|
||||
assetPrices, fullTokenList, nativeAssetsBalances,
|
||||
blockchainTokensBalances,
|
||||
findViewById(R.id.rv_transactions), this,
|
||||
mWalletTxCoinAdapter);
|
||||
break;
|
||||
}
|
||||
}
|
||||
LiveDataUtil.observeOnce(
|
||||
mWalletModel.getCryptoModel().getNetworkModel().mCryptoNetworks,
|
||||
allNetworks -> {
|
||||
Utils.getTxExtraInfo(this, allNetworks, selectedNetwork, accounts, null,
|
||||
false,
|
||||
(assetPrices, fullTokenList, nativeAssetsBalances,
|
||||
blockchainTokensBalances) -> {
|
||||
for (AccountInfo accountInfo : accounts) {
|
||||
if (accountInfo.address.equals(mAddress)
|
||||
&& accountInfo.name.equals(mName)) {
|
||||
AccountInfo[] accountInfos = new AccountInfo[1];
|
||||
accountInfos[0] = accountInfo;
|
||||
WalletListItemModel thisAccountItemModel =
|
||||
new WalletListItemModel(
|
||||
Utils.getCoinIcon(mCoinType), mName,
|
||||
mAddress, null, null, mIsImported);
|
||||
Utils.setUpTransactionList(this, accountInfos,
|
||||
thisAccountItemModel, assetPrices,
|
||||
fullTokenList, nativeAssetsBalances,
|
||||
blockchainTokensBalances,
|
||||
findViewById(R.id.rv_transactions), this,
|
||||
mWalletTxCoinAdapter);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -201,6 +220,10 @@ public class AccountDetailActivity
|
||||
super.finishNativeInitialization();
|
||||
|
||||
initState();
|
||||
BraveActivity activity = BraveActivity.getBraveActivity();
|
||||
if (activity != null) {
|
||||
mWalletModel = activity.getWalletModel();
|
||||
}
|
||||
assert mJsonRpcService != null;
|
||||
mJsonRpcService.getNetwork(mCoinType, selectedNetwork -> {
|
||||
setUpAssetList(selectedNetwork);
|
||||
|
||||
+89
-57
@@ -39,6 +39,8 @@ import org.chromium.brave_wallet.mojom.JsonRpcService;
|
||||
import org.chromium.brave_wallet.mojom.KeyringService;
|
||||
import org.chromium.brave_wallet.mojom.TransactionInfo;
|
||||
import org.chromium.chrome.R;
|
||||
import org.chromium.chrome.browser.app.BraveActivity;
|
||||
import org.chromium.chrome.browser.app.domain.WalletModel;
|
||||
import org.chromium.chrome.browser.crypto_wallet.BlockchainRegistryFactory;
|
||||
import org.chromium.chrome.browser.crypto_wallet.adapters.WalletCoinAdapter;
|
||||
import org.chromium.chrome.browser.crypto_wallet.listeners.OnWalletListItemClick;
|
||||
@@ -50,6 +52,7 @@ import org.chromium.chrome.browser.crypto_wallet.util.TokenUtils;
|
||||
import org.chromium.chrome.browser.crypto_wallet.util.Utils;
|
||||
import org.chromium.chrome.browser.crypto_wallet.util.WalletConstants;
|
||||
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;
|
||||
import org.chromium.chrome.browser.util.LiveDataUtil;
|
||||
import org.chromium.chrome.browser.util.TabUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -82,6 +85,7 @@ public class AssetDetailActivity
|
||||
private boolean mHasNewTx;
|
||||
private boolean mNativeInitialized;
|
||||
private boolean mShouldShowDialog;
|
||||
private WalletModel mWalletModel;
|
||||
|
||||
@Override
|
||||
protected void triggerLayoutInflation() {
|
||||
@@ -301,7 +305,7 @@ public class AssetDetailActivity
|
||||
new WalletCoinAdapter(WalletCoinAdapter.AdapterType.VISIBLE_ASSETS_LIST);
|
||||
KeyringService keyringService = getKeyringService();
|
||||
JsonRpcService jsonRpcService = getJsonRpcService();
|
||||
if (keyringService != null && jsonRpcService != null) {
|
||||
if (keyringService != null && jsonRpcService != null && mWalletModel != null) {
|
||||
keyringService.getKeyringInfo(
|
||||
AssetUtils.getKeyringForCoinType(mCoinType), keyringInfo -> {
|
||||
if (keyringInfo == null) return;
|
||||
@@ -310,66 +314,90 @@ public class AssetDetailActivity
|
||||
WalletListItemModel thisAssetItemModel =
|
||||
new WalletListItemModel(R.drawable.ic_eth, mAsset.name,
|
||||
mAsset.symbol, mAsset.tokenId, "", "");
|
||||
Utils.getTxExtraInfo(this, selectedNetwork, accountInfos,
|
||||
new BlockchainToken[] {mAsset}, false,
|
||||
(assetPrices, fullTokenList, nativeAssetsBalances,
|
||||
blockchainTokensBalances) -> {
|
||||
thisAssetItemModel.setBlockchainToken(mAsset);
|
||||
Utils.setUpTransactionList(this, accountInfos,
|
||||
thisAssetItemModel, assetPrices, fullTokenList,
|
||||
nativeAssetsBalances, blockchainTokensBalances,
|
||||
findViewById(R.id.rv_transactions), this,
|
||||
mWalletTxCoinAdapter);
|
||||
LiveDataUtil.observeOnce(
|
||||
mWalletModel.getCryptoModel().getNetworkModel().mCryptoNetworks,
|
||||
allNetworks -> {
|
||||
Utils.getTxExtraInfo(this, allNetworks, selectedNetwork,
|
||||
accountInfos, new BlockchainToken[] {mAsset}, false,
|
||||
(assetPrices, fullTokenList, nativeAssetsBalances,
|
||||
blockchainTokensBalances) -> {
|
||||
thisAssetItemModel.setBlockchainToken(mAsset);
|
||||
Utils.setUpTransactionList(this, accountInfos,
|
||||
thisAssetItemModel, assetPrices,
|
||||
fullTokenList, nativeAssetsBalances,
|
||||
blockchainTokensBalances,
|
||||
findViewById(R.id.rv_transactions),
|
||||
this, mWalletTxCoinAdapter);
|
||||
|
||||
double thisPrice = Utils.getOrDefault(assetPrices,
|
||||
mAsset.symbol.toLowerCase(Locale.getDefault()),
|
||||
0.0d);
|
||||
List<WalletListItemModel> walletListItemModelList =
|
||||
new ArrayList<>();
|
||||
for (AccountInfo accountInfo : accountInfos) {
|
||||
final String accountAddressLower =
|
||||
accountInfo.address.toLowerCase(
|
||||
Locale.getDefault());
|
||||
double thisAccountBalance =
|
||||
Utils.isNativeToken(selectedNetwork, mAsset)
|
||||
? Utils.getOrDefault(nativeAssetsBalances,
|
||||
accountAddressLower, 0.0d)
|
||||
: Utils.getOrDefault(
|
||||
Utils.getOrDefault(
|
||||
blockchainTokensBalances,
|
||||
accountAddressLower,
|
||||
new HashMap<String, Double>()),
|
||||
Utils.tokenToString(mAsset), 0.0d);
|
||||
final String fiatBalanceString =
|
||||
String.format(Locale.getDefault(), "$%,.2f",
|
||||
thisPrice * thisAccountBalance);
|
||||
final String cryptoBalanceString =
|
||||
String.format(Locale.getDefault(), "%.4f %s",
|
||||
thisAccountBalance, mAsset.symbol);
|
||||
double thisPrice =
|
||||
Utils.getOrDefault(assetPrices,
|
||||
mAsset.symbol.toLowerCase(
|
||||
Locale.getDefault()),
|
||||
0.0d);
|
||||
List<WalletListItemModel>
|
||||
walletListItemModelList =
|
||||
new ArrayList<>();
|
||||
for (AccountInfo accountInfo : accountInfos) {
|
||||
final String accountAddressLower =
|
||||
accountInfo.address.toLowerCase(
|
||||
Locale.getDefault());
|
||||
double thisAccountBalance =
|
||||
Utils.isNativeToken(
|
||||
selectedNetwork, mAsset)
|
||||
? Utils.getOrDefault(
|
||||
nativeAssetsBalances,
|
||||
accountAddressLower, 0.0d)
|
||||
: Utils.getOrDefault(
|
||||
Utils.getOrDefault(
|
||||
blockchainTokensBalances,
|
||||
accountAddressLower,
|
||||
new HashMap<String,
|
||||
Double>()),
|
||||
Utils.tokenToString(mAsset),
|
||||
0.0d);
|
||||
final String fiatBalanceString =
|
||||
String.format(Locale.getDefault(),
|
||||
"$%,.2f",
|
||||
thisPrice
|
||||
* thisAccountBalance);
|
||||
final String cryptoBalanceString =
|
||||
String.format(Locale.getDefault(),
|
||||
"%.4f %s",
|
||||
thisAccountBalance,
|
||||
mAsset.symbol);
|
||||
|
||||
// if NFT, only show the account that owns it (i.e.
|
||||
// balance = 1)
|
||||
if (mAsset.isNft && thisAccountBalance != 1.) continue;
|
||||
// if NFT, only show the account that owns
|
||||
// it (i.e. balance = 1)
|
||||
if (mAsset.isNft
|
||||
&& thisAccountBalance != 1.)
|
||||
continue;
|
||||
|
||||
WalletListItemModel model = new WalletListItemModel(
|
||||
R.drawable.ic_eth, accountInfo.name,
|
||||
accountInfo.address, fiatBalanceString,
|
||||
cryptoBalanceString, accountInfo.isImported);
|
||||
model.setAccountInfo(accountInfo);
|
||||
walletListItemModelList.add(model);
|
||||
}
|
||||
WalletListItemModel model =
|
||||
new WalletListItemModel(
|
||||
R.drawable.ic_eth,
|
||||
accountInfo.name,
|
||||
accountInfo.address,
|
||||
fiatBalanceString,
|
||||
cryptoBalanceString,
|
||||
accountInfo.isImported);
|
||||
model.setAccountInfo(accountInfo);
|
||||
walletListItemModelList.add(model);
|
||||
}
|
||||
|
||||
if (walletCoinAdapter != null) {
|
||||
walletCoinAdapter.setWalletListItemModelList(
|
||||
walletListItemModelList);
|
||||
walletCoinAdapter.setOnWalletListItemClick(
|
||||
AssetDetailActivity.this);
|
||||
walletCoinAdapter.setWalletListItemType(
|
||||
Utils.ACCOUNT_ITEM);
|
||||
rvAccounts.setAdapter(walletCoinAdapter);
|
||||
rvAccounts.setLayoutManager(new LinearLayoutManager(
|
||||
AssetDetailActivity.this));
|
||||
}
|
||||
if (walletCoinAdapter != null) {
|
||||
walletCoinAdapter
|
||||
.setWalletListItemModelList(
|
||||
walletListItemModelList);
|
||||
walletCoinAdapter.setOnWalletListItemClick(
|
||||
AssetDetailActivity.this);
|
||||
walletCoinAdapter.setWalletListItemType(
|
||||
Utils.ACCOUNT_ITEM);
|
||||
rvAccounts.setAdapter(walletCoinAdapter);
|
||||
rvAccounts.setLayoutManager(
|
||||
new LinearLayoutManager(
|
||||
AssetDetailActivity.this));
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -404,6 +432,10 @@ public class AssetDetailActivity
|
||||
getPriceHistory(mAssetSymbol, "usd", AssetPriceTimeframe.ONE_DAY);
|
||||
getPrice(mAssetSymbol, "btc", AssetPriceTimeframe.LIVE);
|
||||
getBlockchainToken(() -> setUpAccountList());
|
||||
BraveActivity activity = BraveActivity.getBraveActivity();
|
||||
if (activity != null) {
|
||||
mWalletModel = activity.getWalletModel();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+35
-11
@@ -9,6 +9,7 @@ import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.app.Dialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.text.Spanned;
|
||||
@@ -49,6 +50,8 @@ import org.chromium.brave_wallet.mojom.TransactionInfo;
|
||||
import org.chromium.brave_wallet.mojom.TransactionType;
|
||||
import org.chromium.brave_wallet.mojom.TxService;
|
||||
import org.chromium.chrome.R;
|
||||
import org.chromium.chrome.browser.app.BraveActivity;
|
||||
import org.chromium.chrome.browser.app.domain.WalletModel;
|
||||
import org.chromium.chrome.browser.crypto_wallet.activities.BraveWalletBaseActivity;
|
||||
import org.chromium.chrome.browser.crypto_wallet.adapters.ApproveTxFragmentPageAdapter;
|
||||
import org.chromium.chrome.browser.crypto_wallet.listeners.TransactionConfirmationListener;
|
||||
@@ -61,6 +64,7 @@ import org.chromium.chrome.browser.crypto_wallet.util.TokenUtils;
|
||||
import org.chromium.chrome.browser.crypto_wallet.util.TransactionUtils;
|
||||
import org.chromium.chrome.browser.crypto_wallet.util.Utils;
|
||||
import org.chromium.chrome.browser.crypto_wallet.util.WalletConstants;
|
||||
import org.chromium.chrome.browser.util.LiveDataUtil;
|
||||
import org.chromium.chrome.browser.util.TabUtils;
|
||||
import org.chromium.url.GURL;
|
||||
|
||||
@@ -87,6 +91,7 @@ public class ApproveTxBottomSheetDialogFragment extends BottomSheetDialogFragmen
|
||||
private Button mRejectAllTx;
|
||||
private int mCoinType;
|
||||
private long mSolanaEstimatedTxFee;
|
||||
private WalletModel mWalletModel;
|
||||
|
||||
public static ApproveTxBottomSheetDialogFragment newInstance(
|
||||
List<TransactionInfo> transactionInfos, TransactionInfo txInfo, String accountName,
|
||||
@@ -202,6 +207,16 @@ public class ApproveTxBottomSheetDialogFragment extends BottomSheetDialogFragmen
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
Dialog dialog = super.onCreateDialog(savedInstanceState);
|
||||
BraveActivity activity = BraveActivity.getBraveActivity();
|
||||
if (activity != null) {
|
||||
mWalletModel = activity.getWalletModel();
|
||||
}
|
||||
return dialog;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDismiss(DialogInterface dialog) {
|
||||
super.onDismiss(dialog);
|
||||
@@ -288,17 +303,26 @@ public class ApproveTxBottomSheetDialogFragment extends BottomSheetDialogFragmen
|
||||
BlockchainToken[] filterByTokens =
|
||||
tokens.toArray(new BlockchainToken[0]);
|
||||
|
||||
Utils.getTxExtraInfo(
|
||||
(BraveWalletBaseActivity) getActivity(),
|
||||
selectedNetwork, accounts, filterByTokens, false,
|
||||
(assetPrices, fullTokenList, nativeAssetsBalances,
|
||||
blockchainTokensBalances) -> {
|
||||
if (!canUpdateUi()) return;
|
||||
fillAssetDependentControls(view,
|
||||
selectedNetwork, accounts, assetPrices,
|
||||
fullTokenList, nativeAssetsBalances,
|
||||
blockchainTokensBalances,
|
||||
mSolanaEstimatedTxFee);
|
||||
if (mWalletModel == null) return;
|
||||
LiveDataUtil.observeOnce(mWalletModel.getCryptoModel()
|
||||
.getNetworkModel()
|
||||
.mCryptoNetworks,
|
||||
allNetworks -> {
|
||||
Utils.getTxExtraInfo(
|
||||
(BraveWalletBaseActivity) getActivity(),
|
||||
allNetworks, selectedNetwork, accounts,
|
||||
filterByTokens, false,
|
||||
(assetPrices, fullTokenList,
|
||||
nativeAssetsBalances,
|
||||
blockchainTokensBalances) -> {
|
||||
if (!canUpdateUi()) return;
|
||||
fillAssetDependentControls(view,
|
||||
selectedNetwork, accounts,
|
||||
assetPrices, fullTokenList,
|
||||
nativeAssetsBalances,
|
||||
blockchainTokensBalances,
|
||||
mSolanaEstimatedTxFee);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+41
-24
@@ -54,6 +54,7 @@ import org.chromium.chrome.browser.crypto_wallet.util.SmoothLineChartEquallySpac
|
||||
import org.chromium.chrome.browser.crypto_wallet.util.TransactionUtils;
|
||||
import org.chromium.chrome.browser.crypto_wallet.util.Utils;
|
||||
import org.chromium.chrome.browser.crypto_wallet.util.WalletUtils;
|
||||
import org.chromium.chrome.browser.util.LiveDataUtil;
|
||||
import org.chromium.content_public.browser.UiThreadTaskTraits;
|
||||
|
||||
import java.util.HashMap;
|
||||
@@ -144,8 +145,11 @@ public class PortfolioFragment
|
||||
mBtnChangeNetwork = view.findViewById(R.id.fragment_portfolio_btn_change_networks);
|
||||
mBtnChangeNetwork.setOnClickListener(v -> { openNetworkSelection(); });
|
||||
mBtnChangeNetwork.setOnLongClickListener(v -> {
|
||||
NetworkInfo networkInfo =
|
||||
mWalletModel.getCryptoModel().getNetworkModel().mDefaultNetwork.getValue();
|
||||
NetworkInfo networkInfo = null;
|
||||
if (mWalletModel != null) {
|
||||
networkInfo =
|
||||
mWalletModel.getCryptoModel().getNetworkModel().mDefaultNetwork.getValue();
|
||||
}
|
||||
if (networkInfo != null) {
|
||||
Toast.makeText(requireContext(), networkInfo.chainName, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
@@ -159,7 +163,7 @@ public class PortfolioFragment
|
||||
updatePortfolioGetPendingTx();
|
||||
}
|
||||
});
|
||||
setUpObservers();
|
||||
if (mWalletModel != null) setUpObservers();
|
||||
return view;
|
||||
}
|
||||
|
||||
@@ -241,8 +245,11 @@ public class PortfolioFragment
|
||||
editVisibleAssets.setOnClickListener(v -> {
|
||||
JsonRpcService jsonRpcService = getJsonRpcService();
|
||||
assert jsonRpcService != null;
|
||||
NetworkInfo selectedNetwork =
|
||||
mWalletModel.getCryptoModel().getNetworkModel().mDefaultNetwork.getValue();
|
||||
NetworkInfo selectedNetwork = null;
|
||||
if (mWalletModel != null) {
|
||||
selectedNetwork =
|
||||
mWalletModel.getCryptoModel().getNetworkModel().mDefaultNetwork.getValue();
|
||||
}
|
||||
if (selectedNetwork == null) {
|
||||
return;
|
||||
}
|
||||
@@ -297,8 +304,11 @@ public class PortfolioFragment
|
||||
|
||||
@Override
|
||||
public void onAssetClick(BlockchainToken asset) {
|
||||
NetworkInfo selectedNetwork =
|
||||
mWalletModel.getCryptoModel().getNetworkModel().mDefaultNetwork.getValue();
|
||||
NetworkInfo selectedNetwork = null;
|
||||
if (mWalletModel != null) {
|
||||
selectedNetwork =
|
||||
mWalletModel.getCryptoModel().getNetworkModel().mDefaultNetwork.getValue();
|
||||
}
|
||||
if (selectedNetwork == null) {
|
||||
return;
|
||||
}
|
||||
@@ -399,6 +409,7 @@ public class PortfolioFragment
|
||||
private void updatePortfolioGetPendingTx() {
|
||||
KeyringService keyringService = getKeyringService();
|
||||
assert keyringService != null;
|
||||
if (mWalletModel == null) return;
|
||||
|
||||
final NetworkInfo selectedNetwork =
|
||||
mWalletModel.getCryptoModel().getNetworkModel().mDefaultNetwork.getValue();
|
||||
@@ -412,26 +423,32 @@ public class PortfolioFragment
|
||||
accountInfos = keyringInfo.accountInfos;
|
||||
}
|
||||
Activity activity = getActivity();
|
||||
final AccountInfo[] accountInfosFinal = accountInfos;
|
||||
if (!(activity instanceof BraveWalletActivity)) return;
|
||||
mPortfolioHelper =
|
||||
new PortfolioHelper((BraveWalletActivity) activity, accountInfos);
|
||||
LiveDataUtil.observeOnce(
|
||||
mWalletModel.getCryptoModel().getNetworkModel().mCryptoNetworks,
|
||||
allNetworks -> {
|
||||
mPortfolioHelper =
|
||||
new PortfolioHelper((BraveWalletActivity) activity,
|
||||
allNetworks, accountInfosFinal);
|
||||
|
||||
mPortfolioHelper.setSelectedNetwork(selectedNetwork);
|
||||
mPortfolioHelper.calculateBalances(() -> {
|
||||
final String fiatSumString = String.format(
|
||||
Locale.getDefault(), "$%,.2f", mPortfolioHelper.getTotalFiatSum());
|
||||
PostTask.runOrPostTask(UiThreadTaskTraits.DEFAULT, () -> {
|
||||
mFiatSumString = fiatSumString;
|
||||
mBalance.setText(mFiatSumString);
|
||||
mBalance.invalidate();
|
||||
mPortfolioHelper.setSelectedNetwork(selectedNetwork);
|
||||
mPortfolioHelper.calculateBalances(() -> {
|
||||
final String fiatSumString = String.format(Locale.getDefault(),
|
||||
"$%,.2f", mPortfolioHelper.getTotalFiatSum());
|
||||
PostTask.runOrPostTask(UiThreadTaskTraits.DEFAULT, () -> {
|
||||
mFiatSumString = fiatSumString;
|
||||
mBalance.setText(mFiatSumString);
|
||||
mBalance.invalidate();
|
||||
|
||||
setUpCoinList(mPortfolioHelper.getUserAssets(),
|
||||
mPortfolioHelper.getPerTokenCryptoSum(),
|
||||
mPortfolioHelper.getPerTokenFiatSum());
|
||||
setUpCoinList(mPortfolioHelper.getUserAssets(),
|
||||
mPortfolioHelper.getPerTokenCryptoSum(),
|
||||
mPortfolioHelper.getPerTokenFiatSum());
|
||||
|
||||
updatePortfolioGraph();
|
||||
});
|
||||
});
|
||||
updatePortfolioGraph();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -448,7 +465,7 @@ public class PortfolioFragment
|
||||
}
|
||||
|
||||
public void callAnotherApproveDialog() {
|
||||
if (!hasPendingTx()) {
|
||||
if (!hasPendingTx() || mWalletModel == null) {
|
||||
return;
|
||||
}
|
||||
ApproveTxBottomSheetDialogFragment approveTxBottomSheetDialogFragment =
|
||||
|
||||
@@ -181,7 +181,8 @@ public class BalanceHelper {
|
||||
});
|
||||
}
|
||||
|
||||
public static void getP3ABalances(BraveWalletBaseActivity activity, NetworkInfo selectedNetwork,
|
||||
public static void getP3ABalances(BraveWalletBaseActivity activity, NetworkInfo[] allNetworks,
|
||||
NetworkInfo selectedNetwork,
|
||||
Callbacks.Callback1<HashMap<Integer, HashSet<String>>> callback) {
|
||||
KeyringService keyringService = activity.getKeyringService();
|
||||
BraveWalletService braveWalletService = activity.getBraveWalletService();
|
||||
@@ -201,7 +202,7 @@ public class BalanceHelper {
|
||||
callback.call(activeAddresses);
|
||||
return;
|
||||
} else {
|
||||
Utils.getP3ANetworks(relevantNetworks -> {
|
||||
Utils.getP3ANetworks(allNetworks, relevantNetworks -> {
|
||||
// Exclude selectedNetwork if in relevantNetworks, also sort by CoinType
|
||||
HashMap<Integer, ArrayList<NetworkInfo>> sortedNetworks =
|
||||
filterAndSortNetworksP3A(relevantNetworks, selectedNetwork);
|
||||
|
||||
@@ -43,9 +43,12 @@ public class PortfolioHelper {
|
||||
private HashMap<String, Double> mPerTokenCryptoSum;
|
||||
private AssetTimePrice[] mFiatHistory;
|
||||
private int mFiatHistoryTimeframe;
|
||||
private NetworkInfo[] mCryptoNetworks;
|
||||
|
||||
public PortfolioHelper(BraveWalletBaseActivity activity, AccountInfo[] accountInfos) {
|
||||
public PortfolioHelper(BraveWalletBaseActivity activity, NetworkInfo[] cryptoNetworks,
|
||||
AccountInfo[] accountInfos) {
|
||||
mActivity = new WeakReference<BraveWalletBaseActivity>(activity);
|
||||
mCryptoNetworks = cryptoNetworks;
|
||||
mAccountInfos = accountInfos;
|
||||
}
|
||||
|
||||
@@ -103,7 +106,8 @@ public class PortfolioHelper {
|
||||
resetResultData();
|
||||
if (mActivity == null) return;
|
||||
|
||||
Utils.getTxExtraInfo(mActivity.get(), mSelectedNetwork, mAccountInfos, null, true,
|
||||
Utils.getTxExtraInfo(mActivity.get(), mCryptoNetworks, mSelectedNetwork, mAccountInfos,
|
||||
null, true,
|
||||
(assetPrices, userAssetsList, nativeAssetsBalances, blockchainTokensBalances) -> {
|
||||
mUserAssets = userAssetsList;
|
||||
|
||||
|
||||
@@ -94,7 +94,6 @@ import org.chromium.chrome.browser.crypto_wallet.model.WalletListItemModel;
|
||||
import org.chromium.chrome.browser.crypto_wallet.observers.ApprovedTxObserver;
|
||||
import org.chromium.chrome.browser.crypto_wallet.util.WalletConstants;
|
||||
import org.chromium.chrome.browser.profiles.Profile;
|
||||
import org.chromium.chrome.browser.util.LiveDataUtil;
|
||||
import org.chromium.chrome.browser.util.TabUtils;
|
||||
import org.chromium.mojo.bindings.Callbacks;
|
||||
import org.chromium.ui.text.NoUnderlineClickableSpan;
|
||||
@@ -1472,8 +1471,11 @@ public class Utils {
|
||||
: symbolLowerCase;
|
||||
}
|
||||
|
||||
public static void getTxExtraInfo(BraveWalletBaseActivity activity, NetworkInfo selectedNetwork,
|
||||
AccountInfo[] accountInfos, BlockchainToken[] filterByTokens, boolean userAssetsOnly,
|
||||
// Please only use this function when you need all the info (tokens, prices and balances) at the
|
||||
// same time!
|
||||
public static void getTxExtraInfo(BraveWalletBaseActivity activity, NetworkInfo[] allNetworks,
|
||||
NetworkInfo selectedNetwork, AccountInfo[] accountInfos,
|
||||
BlockchainToken[] filterByTokens, boolean userAssetsOnly,
|
||||
Callbacks.Callback4<HashMap<String, Double>, BlockchainToken[], HashMap<String, Double>,
|
||||
HashMap<String, HashMap<String, Double>>> callback) {
|
||||
BraveWalletService braveWalletService = activity.getBraveWalletService();
|
||||
@@ -1519,7 +1521,8 @@ public class Utils {
|
||||
AsyncUtils.GetP3ABalancesContext getP3ABalancesContext =
|
||||
new AsyncUtils.GetP3ABalancesContext(
|
||||
multiResponse.singleResponseComplete);
|
||||
BalanceHelper.getP3ABalances(activity, selectedNetwork, getP3ABalancesContext);
|
||||
BalanceHelper.getP3ABalances(
|
||||
activity, allNetworks, selectedNetwork, getP3ABalancesContext);
|
||||
|
||||
multiResponse.setWhenAllCompletedAction(() -> {
|
||||
// P3A active accounts
|
||||
@@ -1543,30 +1546,22 @@ public class Utils {
|
||||
});
|
||||
}
|
||||
|
||||
public static void getP3ANetworks(Callbacks.Callback1<NetworkInfo[]> callback) {
|
||||
BraveActivity activity = BraveActivity.getBraveActivity();
|
||||
if (activity == null) {
|
||||
callback.call(new NetworkInfo[0]);
|
||||
}
|
||||
public static void getP3ANetworks(
|
||||
NetworkInfo[] allNetworks, Callbacks.Callback1<NetworkInfo[]> callback) {
|
||||
ArrayList<NetworkInfo> relevantNetworks = new ArrayList<NetworkInfo>();
|
||||
boolean countTestNetworks = CommandLine.getInstance().hasSwitch(
|
||||
BraveWalletConstants.P3A_COUNT_TEST_NETWORKS_SWITCH);
|
||||
LiveDataUtil.observeOnce(
|
||||
activity.getWalletModel().getCryptoModel().getNetworkModel().mCryptoNetworks,
|
||||
allNetworks -> {
|
||||
for (NetworkInfo network : allNetworks) {
|
||||
// Exclude testnet chain data by default.
|
||||
// Testnet chain counting can be enabled via the
|
||||
// --p3a-count-wallet-test-networks switch
|
||||
if (countTestNetworks
|
||||
|| !WalletConstants.KNOWN_TEST_CHAIN_IDS.contains(
|
||||
network.chainId)) {
|
||||
relevantNetworks.add(network);
|
||||
}
|
||||
}
|
||||
for (NetworkInfo network : allNetworks) {
|
||||
// Exclude testnet chain data by default.
|
||||
// Testnet chain counting can be enabled via the
|
||||
// --p3a-count-wallet-test-networks switch
|
||||
if (countTestNetworks
|
||||
|| !WalletConstants.KNOWN_TEST_CHAIN_IDS.contains(network.chainId)) {
|
||||
relevantNetworks.add(network);
|
||||
}
|
||||
}
|
||||
|
||||
callback.call(relevantNetworks.toArray(new NetworkInfo[0]));
|
||||
});
|
||||
callback.call(relevantNetworks.toArray(new NetworkInfo[0]));
|
||||
}
|
||||
|
||||
public static boolean isNativeToken(NetworkInfo selectedNetwork, BlockchainToken token) {
|
||||
|
||||
Reference in New Issue
Block a user