Skip to content

Commit

Permalink
Uplift of #20361 (squashed) to beta
Browse files Browse the repository at this point in the history
  • Loading branch information
brave-builds committed Oct 6, 2023
1 parent 23c0a4d commit 8e3ede8
Show file tree
Hide file tree
Showing 13 changed files with 237 additions and 217 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public void init() {
// Selected local network can be "All networks"
allNetworkList.add(0, allNetwork);
}
updateLocalNetwork(allNetworkList, mSelectedChainId);
updateLocalNetwork(allNetworkList, mSelectedChainId,
mNetworkModel.getNetwork(mSelectedChainId).coin);
return networkListsCopy;
});
}
Expand Down Expand Up @@ -146,8 +147,8 @@ public Mode getMode() {
public SelectionMode getSelectionType() {
return mSelectionMode;
}
private void updateLocalNetwork(List<NetworkInfo> networkInfos, String chainId) {
NetworkInfo networkInfo = NetworkUtils.findNetwork(networkInfos, chainId);
private void updateLocalNetwork(List<NetworkInfo> networkInfos, String chainId, int coin) {
NetworkInfo networkInfo = NetworkUtils.findNetwork(networkInfos, chainId, coin);
if (networkInfo != null) {
_mSelectedNetwork.postValue(networkInfo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,17 @@ private void fetchNftMetadata(List<BlockchainToken> nftList, List<NetworkInfo> a
} else {
// Other NFTs.
nftDataModels.add(new NftDataModel(userAsset,
NetworkUtils.findNetwork(allNetworkList, userAsset.chainId), null));
NetworkUtils.findNetwork(
allNetworkList, userAsset.chainId, userAsset.coin),
null));
}
}
}
nftMetaDataHandler.setWhenAllCompletedAction(() -> {
for (AsyncUtils.BaseGetNftMetadataContext metadata : nftMetadataList) {
nftDataModels.add(new NftDataModel(metadata.asset,
NetworkUtils.findNetwork(allNetworkList, metadata.asset.chainId),
NetworkUtils.findNetwork(
allNetworkList, metadata.asset.chainId, metadata.asset.coin),
new NftMetadata(metadata.tokenMetadata, metadata.errorCode,
metadata.errorMessage)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void update(WeakReference<BraveWalletBaseActivity> activityRef) {
== txNetworks.size()) {
parseTransactions(mActivityRef,
assetAccountsNetworkBalances,
filteredTransactions);
filteredTransactions, networkInfo.coin);
}
});
}
Expand All @@ -165,7 +165,7 @@ public void update(WeakReference<BraveWalletBaseActivity> activityRef) {

private void parseTransactions(WeakReference<BraveWalletBaseActivity> activityRef,
List<AssetAccountsNetworkBalance> assetAccountsNetworkBalances,
TransactionInfo[] transactionInfoArr) {
TransactionInfo[] transactionInfoArr, int coin) {
// Received balances of all network, can now fetch transaction
var allAccountsArray = mAllAccountInfoList.toArray(new AccountInfo[0]);
SolanaTransactionsGasHelper solanaTransactionsGasHelper =
Expand All @@ -183,7 +183,7 @@ private void parseTransactions(WeakReference<BraveWalletBaseActivity> activityRe
if (perTxSolanaFee.get(txInfo.id) != null) {
solanaEstimatedTxFee = perTxSolanaFee.get(txInfo.id);
}
var txNetwork = NetworkUtils.findNetwork(mAllNetworkInfoList, txInfo.chainId);
var txNetwork = NetworkUtils.findNetwork(mAllNetworkInfoList, txInfo.chainId, coin);
var txExtraData =
assetAccountsNetworkBalances.stream()
.filter(data -> data.networkInfo.chainId.equals(txInfo.chainId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,20 @@
import org.chromium.chrome.browser.crypto_wallet.util.WalletConstants;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;

public class UserAssetModel {
private final Object mLock = new Object();
private BraveWalletService mBraveWalletService;
private JsonRpcService mJsonRpcService;
private BlockchainRegistry mBlockchainRegistry;
private final BraveWalletService mBraveWalletService;
private final JsonRpcService mJsonRpcService;
private final BlockchainRegistry mBlockchainRegistry;
private final WalletCoinAdapter.AdapterType mType;
private NetworkInfo mSelectedNetwork;
private List<NetworkInfo> mCryptoNetworks;
private CryptoSharedData mSharedData;
private MutableLiveData<AssetsResult> _mAssetsResult;
private final CryptoSharedData mSharedData;
private final MutableLiveData<AssetsResult> _mAssetsResult;
public LiveData<AssetsResult> mAssetsResult;

public UserAssetModel(BraveWalletService braveWalletService, JsonRpcService jsonRpcService,
Expand All @@ -63,34 +62,20 @@ public void fetchAssets(boolean nftsOnly, NetworkInfo selectedNetwork) {
if (NetworkUtils.isAllNetwork(mSelectedNetwork)) {
fetchAllNetworksAssets(nftsOnly);
} else {
TokenUtils.getUserAssetsFiltered(mBraveWalletService,
TokenUtils.getVisibleUserAssetsFiltered(mBraveWalletService,
mSelectedNetwork, mSelectedNetwork.coin,
TokenUtils.TokenType.ALL, userAssets -> {
TokenUtils.getAllTokensFiltered(mBraveWalletService,
mBlockchainRegistry, mSelectedNetwork,
mSelectedNetwork.coin,
nftsOnly ? TokenUtils.TokenType.NFTS
: TokenUtils.TokenType.NON_NFTS,
tokens -> {
_mAssetsResult.postValue(new AssetsResult(
Arrays.asList(tokens),
Arrays.asList(userAssets)));
});
});
TokenUtils.TokenType.ALL,
userAssets
-> TokenUtils.getAllTokensFiltered(mBraveWalletService,
mBlockchainRegistry, mSelectedNetwork,
nftsOnly ? TokenUtils.TokenType.NFTS
: TokenUtils.TokenType.NON_NFTS,
tokens -> {
_mAssetsResult.postValue(
new AssetsResult(Arrays.asList(tokens),
Arrays.asList(userAssets)));
}));
}
} else if (mType == WalletCoinAdapter.AdapterType.SEND_ASSETS_LIST) {
assert mSelectedNetwork != null;
TokenUtils.getUserAssetsFiltered(mBraveWalletService, mSelectedNetwork,
mSelectedNetwork.coin, TokenUtils.TokenType.ALL, tokens -> {
_mAssetsResult.postValue(new AssetsResult(
Arrays.asList(tokens), Collections.emptyList()));
});
} else if (mType == WalletCoinAdapter.AdapterType.BUY_ASSETS_LIST) {
TokenUtils.getBuyTokensFiltered(mBlockchainRegistry, mSelectedNetwork,
TokenUtils.TokenType.ALL, tokens -> {
_mAssetsResult.postValue(new AssetsResult(
Arrays.asList(tokens), Collections.emptyList()));
});
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,18 @@ public enum AdapterType {
VISIBLE_ASSETS_LIST,
EDIT_VISIBLE_ASSETS_LIST,
ACCOUNTS_LIST,
SELECT_ACCOUNTS_LIST,
BUY_ASSETS_LIST,
SEND_ASSETS_LIST;
SELECT_ACCOUNTS_LIST
}

private Context context;
private List<WalletListItemModel> walletListItemModelList = new ArrayList<>();
private List<WalletListItemModel> walletListItemModelListCopy = new ArrayList<>();
private List<Integer> mCheckedPositions = new ArrayList<>();
private final List<WalletListItemModel> walletListItemModelListCopy = new ArrayList<>();
private final List<Integer> mCheckedPositions = new ArrayList<>();
private OnWalletListItemClick onWalletListItemClick;
private int walletListItemType;
private AdapterType mType;
private ExecutorService mExecutor;
private Handler mHandler;
private final ExecutorService mExecutor;
private final Handler mHandler;
private int previousSelectedPos;

public WalletCoinAdapter(AdapterType type) {
Expand All @@ -78,8 +76,8 @@ public WalletCoinAdapter(AdapterType type) {
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
WalletListItemModel walletListItemModel = walletListItemModelList.get(position);
// When ViewHolder is re-used, it has the obeservers which are fired when
// we modifying checkbox. This may cause unwanted modifying of the model
// When ViewHolder is re-used, it has the observers which are fired when
// we modifying checkbox. This may cause unwanted modifying of the model.
holder.resetObservers();

holder.titleText.setText(walletListItemModel.getTitle());
Expand All @@ -101,16 +99,6 @@ public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
if (walletListItemType == Utils.TRANSACTION_ITEM) {
onWalletListItemClick.onTransactionClick(walletListItemModel.getTransactionInfo());
} else if (walletListItemType == Utils.ASSET_ITEM) {
if (mType == AdapterType.BUY_ASSETS_LIST || mType == AdapterType.SEND_ASSETS_LIST) {
for (int i = 0; i < walletListItemModelListCopy.size(); i++) {
WalletListItemModel item = walletListItemModelListCopy.get(i);
if (item.getTitle().equals(holder.titleText.getText())
|| item.getSubTitle().equals(holder.subTitleText.getText())) {
mCheckedPositions.add((Integer) i);
break;
}
}
}
if (mType != AdapterType.EDIT_VISIBLE_ASSETS_LIST) {
onWalletListItemClick.onAssetClick(walletListItemModel.getBlockchainToken());
}
Expand Down Expand Up @@ -161,7 +149,7 @@ public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.text2Text.setVisibility(View.GONE);
if (mType == AdapterType.EDIT_VISIBLE_ASSETS_LIST) {
holder.assetCheck.setVisibility(View.VISIBLE);
holder.assetCheck.setChecked(walletListItemModel.getIsUserSelected());
holder.assetCheck.setChecked(walletListItemModel.isVisible());
holder.assetCheck.setOnCheckedChangeListener(
new CompoundButton.OnCheckedChangeListener() {
@Override
Expand Down Expand Up @@ -218,11 +206,10 @@ public void onCheckedChanged(
onWalletListItemClick.onMaybeShowTrashButton(
walletListItemModel, holder.iconTrash);
holder.iconTrash.setOnClickListener(
v -> { onWalletListItemClick.onTrashIconClick(walletListItemModel); });
v -> onWalletListItemClick.onTrashIconClick(walletListItemModel));
}
}
} else if (mType == AdapterType.ACCOUNTS_LIST
|| mType == AdapterType.SELECT_ACCOUNTS_LIST) {
} else {
holder.iconImg.setImageResource(android.R.color.transparent);
if (walletListItemModel.getAccountInfo() != null) {
Utils.setBlockiesBitmapResourceFromAccount(mExecutor, mHandler, holder.iconImg,
Expand All @@ -236,14 +223,13 @@ public void onCheckedChanged(
});
if (mType == AdapterType.SELECT_ACCOUNTS_LIST) {
holder.ivSelected.setVisibility(
walletListItemModel.getIsUserSelected() ? View.VISIBLE : View.INVISIBLE);
walletListItemModel.isVisible() ? View.VISIBLE : View.INVISIBLE);
}
}
}

private boolean isAssetSelectionType() {
return mType == AdapterType.EDIT_VISIBLE_ASSETS_LIST || mType == AdapterType.BUY_ASSETS_LIST
|| mType == AdapterType.SEND_ASSETS_LIST;
return mType == AdapterType.EDIT_VISIBLE_ASSETS_LIST;
}

@Override
Expand All @@ -263,7 +249,7 @@ public void setWalletListItemModelList(List<WalletListItemModel> walletListItemM
mCheckedPositions.clear();
}
for (int i = 0; i < walletListItemModelListCopy.size(); i++) {
if (walletListItemModelListCopy.get(i).getIsUserSelected()) {
if (walletListItemModelListCopy.get(i).isVisible()) {
mCheckedPositions.add((Integer) i);
}
}
Expand Down Expand Up @@ -320,10 +306,10 @@ public void updateSelectedNetwork(String title, String subTitle) {
}

private void updateSelectedNetwork(int selectedAccountPosition) {
walletListItemModelList.get(previousSelectedPos).setIsUserSelected(false);
walletListItemModelList.get(previousSelectedPos).isVisible(false);
notifyItemChanged(previousSelectedPos);

walletListItemModelList.get(selectedAccountPosition).setIsUserSelected(true);
walletListItemModelList.get(selectedAccountPosition).isVisible(true);
previousSelectedPos = selectedAccountPosition;
notifyItemChanged(selectedAccountPosition);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ public void setupDialog(@NonNull Dialog dialog, int style) {

// First fill in data that does not require remote queries
TokenUtils.getAllTokensFiltered(getBraveWalletService(),
getBlockchainRegistry(), mTxNetwork, mTxNetwork.coin,
TokenUtils.TokenType.ALL, tokenList -> {
getBlockchainRegistry(), mTxNetwork, TokenUtils.TokenType.ALL,
tokenList -> {
SolanaTransactionsGasHelper solanaTransactionsGasHelper =
new SolanaTransactionsGasHelper(
(BraveWalletBaseActivity) getActivity(),
Expand Down
Loading

0 comments on commit 8e3ede8

Please sign in to comment.