From f420daf2f0eb2e645519442afadc82f7e37d5214 Mon Sep 17 00:00:00 2001 From: wchen342 Date: Mon, 10 Jan 2022 20:06:25 +0200 Subject: [PATCH] Make asset the default swap from token; disallow swap to and from the same token --- .../activities/AssetDetailActivity.java | 4 +- .../activities/BuySendSwapActivity.java | 57 ++++++++++++------- .../browser/crypto_wallet/util/Utils.java | 10 +++- 3 files changed, 48 insertions(+), 23 deletions(-) diff --git a/android/java/org/chromium/chrome/browser/crypto_wallet/activities/AssetDetailActivity.java b/android/java/org/chromium/chrome/browser/crypto_wallet/activities/AssetDetailActivity.java index 32ec5a5af8a..34c0be677f1 100644 --- a/android/java/org/chromium/chrome/browser/crypto_wallet/activities/AssetDetailActivity.java +++ b/android/java/org/chromium/chrome/browser/crypto_wallet/activities/AssetDetailActivity.java @@ -151,8 +151,8 @@ public class AssetDetailActivity extends AsyncInitializationActivity btnSwap.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - Utils.openBuySendSwapActivity( - AssetDetailActivity.this, BuySendSwapActivity.ActivityType.SWAP); + Utils.openBuySendSwapActivity(AssetDetailActivity.this, + BuySendSwapActivity.ActivityType.SWAP, mAssetSymbol); } }); diff --git a/android/java/org/chromium/chrome/browser/crypto_wallet/activities/BuySendSwapActivity.java b/android/java/org/chromium/chrome/browser/crypto_wallet/activities/BuySendSwapActivity.java index 7e745f316da..50738648a31 100644 --- a/android/java/org/chromium/chrome/browser/crypto_wallet/activities/BuySendSwapActivity.java +++ b/android/java/org/chromium/chrome/browser/crypto_wallet/activities/BuySendSwapActivity.java @@ -239,21 +239,18 @@ public class BuySendSwapActivity extends AsyncInitializationActivity TextView fromBalanceText = findViewById(R.id.from_balance_text); TextView fromAssetText = findViewById(R.id.from_asset_text); - fromAssetText.setText("ETH"); TextView toBalanceText = findViewById(R.id.to_balance_text); TextView toAssetText = findViewById(R.id.to_asset_text); - toAssetText.setText("BAT"); TextView marketPriceValueText = findViewById(R.id.market_price_value_text); mSlippageToleranceText = findViewById(R.id.slippage_tolerance_dropdown); onInitialLayoutInflationComplete(); + mInitialLayoutInflationComplete = true; adjustControls(); - - mInitialLayoutInflationComplete = true; } private class BuySendSwapUiInfo { @@ -496,6 +493,38 @@ public class BuySendSwapActivity extends AsyncInitializationActivity checkBalanceShowError(response, errorResponse); } + private void initSwapFromToAssets() { + final BlockchainToken eth = Utils.createEthereumBlockchainToken(); + if (mBlockchainRegistry != null && mCustomAccountAdapter != null + && mActivityType == ActivityType.SWAP && mInitialLayoutInflationComplete) { + String swapToAsset = "BAT"; + + // Swap from + String swapFromAssetSymbol = getIntent().getStringExtra("swapFromAssetSymbol"); + if (swapFromAssetSymbol == null + || swapFromAssetSymbol.equals(eth.symbol)) { // default swap from ETH + updateBuySendAsset(eth.symbol, eth); + } else { + mBlockchainRegistry.getTokenBySymbol(swapFromAssetSymbol, token -> { + if (token != null) { + updateBuySendAsset(token.symbol, token); + } + }); + } + + // Swap to + if (swapToAsset.equals(swapFromAssetSymbol)) { // swap from BAT + updateSwapToAsset(eth.symbol, eth); + } else { + mBlockchainRegistry.getTokenBySymbol(swapToAsset, token -> { + if (token != null) { + updateSwapToAsset(token.symbol, token); + } + }); + } + } + } + private void checkBalanceShowError(SwapResponse response, String errorResponse) { final Button btnBuySendSwap = findViewById(R.id.btn_buy_send_swap); EditText fromValueText = findViewById(R.id.from_value_text); @@ -909,13 +938,7 @@ public class BuySendSwapActivity extends AsyncInitializationActivity TabUtils.openUrlInNewTab(false, Utils.DEX_AGGREGATOR_URL); TabUtils.bringChromeTabbedActivityToTheTop(this); }); - if (mBlockchainRegistry != null && mCustomAccountAdapter != null) { - mBlockchainRegistry.getTokenBySymbol("BAT", token -> { - if (token != null) { - updateSwapToAsset(token.symbol, token); - } - }); - } + initSwapFromToAssets(); } btnBuySendSwap.setOnClickListener(v -> { @@ -1420,6 +1443,9 @@ public class BuySendSwapActivity extends AsyncInitializationActivity } public void updateSwapToAsset(String asset, BlockchainToken blockchainToken) { + if (mCurrentBlockchainToken != null + && mCurrentBlockchainToken.symbol.equals(blockchainToken.symbol)) + return; TextView assetToDropDown = findViewById(R.id.to_asset_text); assetToDropDown.setText(asset); mCurrentSwapToBlockchainToken = blockchainToken; @@ -1636,14 +1662,7 @@ public class BuySendSwapActivity extends AsyncInitializationActivity } // updateSwapToAsset needs mCustomAccountAdapter to be initialized - if (mBlockchainRegistry != null && mActivityType == ActivityType.SWAP - && mInitialLayoutInflationComplete) { - mBlockchainRegistry.getTokenBySymbol("BAT", token -> { - if (token != null) { - updateSwapToAsset(token.symbol, token); - } - }); - } + initSwapFromToAssets(); }); } } diff --git a/android/java/org/chromium/chrome/browser/crypto_wallet/util/Utils.java b/android/java/org/chromium/chrome/browser/crypto_wallet/util/Utils.java index d5f2f92edd6..dd6c6a2c9f4 100644 --- a/android/java/org/chromium/chrome/browser/crypto_wallet/util/Utils.java +++ b/android/java/org/chromium/chrome/browser/crypto_wallet/util/Utils.java @@ -192,14 +192,20 @@ public class Utils { if (focusedView != null) imm.hideSoftInputFromWindow(focusedView.getWindowToken(), 0); } - public static void openBuySendSwapActivity( - Activity activity, BuySendSwapActivity.ActivityType activityType) { + public static void openBuySendSwapActivity(Activity activity, + BuySendSwapActivity.ActivityType activityType, String swapFromAssetSymbol) { assert activity != null; Intent buySendSwapActivityIntent = new Intent(activity, BuySendSwapActivity.class); buySendSwapActivityIntent.putExtra("activityType", activityType.getValue()); + buySendSwapActivityIntent.putExtra("swapFromAssetSymbol", swapFromAssetSymbol); activity.startActivity(buySendSwapActivityIntent); } + public static void openBuySendSwapActivity( + Activity activity, BuySendSwapActivity.ActivityType activityType) { + openBuySendSwapActivity(activity, activityType, null); + } + public static void openAssetDetailsActivity(Activity activity, String chainId, String assetSymbol, String assetName, String contractAddress, String assetLogo, int assetDecimals) {