Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes a crash on Android on clicking swap from an asset screen for a custom asset #13020

Merged
merged 1 commit into from
Apr 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
import org.chromium.chrome.browser.crypto_wallet.fragments.EditVisibleAssetsBottomSheetDialogFragment;
import org.chromium.chrome.browser.crypto_wallet.observers.ApprovedTxObserver;
import org.chromium.chrome.browser.crypto_wallet.observers.KeyringServiceObserver;
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.Validations;
import org.chromium.chrome.browser.init.AsyncInitializationActivity;
Expand Down Expand Up @@ -456,35 +457,53 @@ private void resetSwapFromToAssets() {
&& mInitialLayoutInflationComplete) {
final BlockchainToken eth =
Utils.createEthereumBlockchainToken(BraveWalletConstants.MAINNET_CHAIN_ID);
String swapToAsset = "BAT";

// Swap from
String swapFromAssetSymbol = getIntent().getStringExtra("swapFromAssetSymbol");
if (swapFromAssetSymbol == null
|| swapFromAssetSymbol.equals(eth.symbol)) { // default swap from ETH
updateBuySendSwapAsset(eth.symbol, eth, true);
resetSwapToAsset(eth, swapFromAssetSymbol);
} else {
mBlockchainRegistry.getTokenBySymbol(BraveWalletConstants.MAINNET_CHAIN_ID,
CoinType.ETH, swapFromAssetSymbol, token -> {
if (token != null) {
updateBuySendSwapAsset(token.symbol, token, true);
resetSwapToAsset(eth, swapFromAssetSymbol);

return;
}
// We most likely have a custom token
TokenUtils.getAllTokensFiltered(mBraveWalletService,
mBlockchainRegistry, mCurrentChainId, tokens -> {
for (BlockchainToken filteredToken : tokens) {
if (swapFromAssetSymbol.equals(filteredToken.symbol)) {
updateBuySendSwapAsset(
filteredToken.symbol, filteredToken, true);
resetSwapToAsset(eth, swapFromAssetSymbol);
break;
}
}
});
});
}
}
}

if (mActivityType == ActivityType.SWAP) {
// Swap to
if (swapToAsset.equals(swapFromAssetSymbol)) { // swap from BAT
updateBuySendSwapAsset(eth.symbol, eth, false);
} else {
mBlockchainRegistry.getTokenBySymbol(BraveWalletConstants.MAINNET_CHAIN_ID,
CoinType.ETH, swapToAsset, token -> {
if (token != null) {
updateBuySendSwapAsset(token.symbol, token, false);
}
});
}
}
private void resetSwapToAsset(BlockchainToken eth, String swapFromAssetSymbol) {
if (mActivityType != ActivityType.SWAP) {
return;
}
String swapToAsset = "BAT";
// Swap to
if (swapToAsset.equals(swapFromAssetSymbol)) { // swap from BAT
updateBuySendSwapAsset(eth.symbol, eth, false);
} else {
mBlockchainRegistry.getTokenBySymbol(
BraveWalletConstants.MAINNET_CHAIN_ID, CoinType.ETH, swapToAsset, token -> {
if (token != null) {
updateBuySendSwapAsset(token.symbol, token, false);
}
});
}
}

Expand Down Expand Up @@ -848,7 +867,6 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
}
return false;
});
resetSwapFromToAssets();
}

mBtnBuySendSwap.setOnClickListener(v -> {
Expand Down Expand Up @@ -1390,6 +1408,7 @@ private void enableDisableSwapButton() {
boolean tokenNullOrEmpty = mCurrentBlockchainToken == null
|| mCurrentBlockchainToken.contractAddress.isEmpty();
boolean disable = swapToTokenNullOrEmpty && tokenNullOrEmpty
|| (mCurrentSwapToBlockchainToken == null) || (mCurrentBlockchainToken == null)
|| mCurrentSwapToBlockchainToken.contractAddress.equals(
mCurrentBlockchainToken.contractAddress);

Expand Down Expand Up @@ -1454,28 +1473,30 @@ chain.symbol, getResources().getDisplayMetrics().density,
}
}
});
});
}
if (mKeyringService != null) {
mKeyringService.getKeyringInfo(BraveWalletConstants.DEFAULT_KEYRING_ID, keyring -> {
String[] accountNames = new String[keyring.accountInfos.length];
String[] accountTitles = new String[keyring.accountInfos.length];
int currentPos = 0;
for (AccountInfo info : keyring.accountInfos) {
accountNames[currentPos] = info.name;
accountTitles[currentPos] = info.address;
currentPos++;
}
mCustomAccountAdapter = new AccountSpinnerAdapter(
getApplicationContext(), accountNames, accountTitles);
mAccountSpinner.setAdapter(mCustomAccountAdapter);
mAccountSpinner.setOnItemSelectedListener(this);
if (accountTitles.length > 0) {
updateBalanceMaybeSwap(accountTitles[0]);
}
if (mKeyringService != null) {
mKeyringService.getKeyringInfo(
BraveWalletConstants.DEFAULT_KEYRING_ID, keyring -> {
String[] accountNames = new String[keyring.accountInfos.length];
String[] accountTitles = new String[keyring.accountInfos.length];
int currentPos = 0;
for (AccountInfo info : keyring.accountInfos) {
accountNames[currentPos] = info.name;
accountTitles[currentPos] = info.address;
currentPos++;
}
mCustomAccountAdapter = new AccountSpinnerAdapter(
getApplicationContext(), accountNames, accountTitles);
mAccountSpinner.setAdapter(mCustomAccountAdapter);
mAccountSpinner.setOnItemSelectedListener(this);
if (accountTitles.length > 0) {
updateBalanceMaybeSwap(accountTitles[0]);
}

// updateBuySendSwapAsset needs mCustomAccountAdapter to be initialized
resetSwapFromToAssets();
// updateBuySendSwapAsset needs mCustomAccountAdapter to be
// initialized
resetSwapFromToAssets();
});
}
});
}
}
Expand Down