Review suggestions

This commit is contained in:
Serg
2021-10-22 11:40:19 -04:00
parent 090bb81093
commit e02800f96e
2 changed files with 47 additions and 76 deletions
@@ -153,6 +153,7 @@ public class BuySendSwapActivity extends AsyncInitializationActivity
private Handler mHandler;
private String mCurrentChainId;
private String mAllowanceTarget;
private Spinner mAccountSpinner;
@Override
protected void onDestroy() {
@@ -225,11 +226,11 @@ public class BuySendSwapActivity extends AsyncInitializationActivity
accountTitles[currentPos] = info.address;
currentPos++;
}
Spinner accountSpinner = findViewById(R.id.accounts_spinner);
mAccountSpinner = findViewById(R.id.accounts_spinner);
mCustomAccountAdapter = new AccountSpinnerAdapter(
getApplicationContext(), pictures, accountNames, accountTitles);
accountSpinner.setAdapter(mCustomAccountAdapter);
accountSpinner.setOnItemSelectedListener(this);
mAccountSpinner.setAdapter(mCustomAccountAdapter);
mAccountSpinner.setOnItemSelectedListener(this);
if (accountTitles.length > 0) {
updateBalance(accountTitles[0], true);
if (mActivityType == ActivityType.SWAP) {
@@ -253,13 +254,12 @@ public class BuySendSwapActivity extends AsyncInitializationActivity
mCurrentChainId = chainId;
});
}
Spinner accountSpinner = findViewById(R.id.accounts_spinner);
updateBalance(mCustomAccountAdapter.getTitleAtPosition(
accountSpinner.getSelectedItemPosition()),
mAccountSpinner.getSelectedItemPosition()),
true);
if (mActivityType == ActivityType.SWAP) {
updateBalance(mCustomAccountAdapter.getTitleAtPosition(
accountSpinner.getSelectedItemPosition()),
mAccountSpinner.getSelectedItemPosition()),
false);
}
} else if (parent.getId() == R.id.accounts_spinner) {
@@ -276,9 +276,8 @@ public class BuySendSwapActivity extends AsyncInitializationActivity
public void onNothingSelected(AdapterView<?> arg0) {}
private void getSendSwapQuota(boolean calculatePerSellAsset, boolean sendTx) {
Spinner accountSpinner = findViewById(R.id.accounts_spinner);
String from =
mCustomAccountAdapter.getTitleAtPosition(accountSpinner.getSelectedItemPosition());
mCustomAccountAdapter.getTitleAtPosition(mAccountSpinner.getSelectedItemPosition());
EditText fromValueText = findViewById(R.id.from_value_text);
String value = fromValueText.getText().toString();
EditText toValueText = findViewById(R.id.to_value_text);
@@ -416,9 +415,8 @@ public class BuySendSwapActivity extends AsyncInitializationActivity
final double fee = gasLimit * Utils.fromWei(response.gasPrice, 18);
final double fromValue = valueFrom;
assert mEthJsonRpcController != null;
Spinner accountSpinner = findViewById(R.id.accounts_spinner);
mEthJsonRpcController.getBalance(
mCustomAccountAdapter.getTitleAtPosition(accountSpinner.getSelectedItemPosition()),
mCustomAccountAdapter.getTitleAtPosition(mAccountSpinner.getSelectedItemPosition()),
(success, balance) -> {
if (success) {
double currentBalance = Utils.fromHexWei(balance, 18);
@@ -468,9 +466,8 @@ public class BuySendSwapActivity extends AsyncInitializationActivity
private void checkAllowance(String contract, String spenderAddress, double amountToSend) {
assert mEthJsonRpcController != null;
assert mCurrentErcToken != null;
Spinner accountSpinner = findViewById(R.id.accounts_spinner);
String ownerAddress =
mCustomAccountAdapter.getTitleAtPosition(accountSpinner.getSelectedItemPosition());
mCustomAccountAdapter.getTitleAtPosition(mAccountSpinner.getSelectedItemPosition());
mEthJsonRpcController.getErc20TokenAllowance(
contract, ownerAddress, spenderAddress, (success, allowance) -> {
if (!success
@@ -667,9 +664,8 @@ public class BuySendSwapActivity extends AsyncInitializationActivity
}
btnBuySendSwap.setOnClickListener(v -> {
Spinner accountSpinner = findViewById(R.id.accounts_spinner);
String from = mCustomAccountAdapter.getTitleAtPosition(
accountSpinner.getSelectedItemPosition());
mAccountSpinner.getSelectedItemPosition());
EditText fromValueText = findViewById(R.id.from_value_text);
// TODO(sergz): Some kind of validation that we have enough balance
String value = fromValueText.getText().toString();
@@ -780,9 +776,8 @@ public class BuySendSwapActivity extends AsyncInitializationActivity
}
TxData txData = Utils.getTxData(
"0x1", "", "", mCurrentErcToken.contractAddress, "0x0", data);
Spinner accountSpinner = findViewById(R.id.accounts_spinner);
String from = mCustomAccountAdapter.getTitleAtPosition(
accountSpinner.getSelectedItemPosition());
mAccountSpinner.getSelectedItemPosition());
addUnapprovedTransaction(txData, from);
});
}
@@ -823,11 +818,10 @@ public class BuySendSwapActivity extends AsyncInitializationActivity
}
mEthJsonRpcController.getChainId(chainId -> {
String chainName = Utils.getNetworkText(this, chainId).toString();
Spinner accountSpinner = findViewById(R.id.accounts_spinner);
String accountName = mCustomAccountAdapter.getNameAtPosition(
accountSpinner.getSelectedItemPosition());
mAccountSpinner.getSelectedItemPosition());
int accountPic = mCustomAccountAdapter.getPictureAtPosition(
accountSpinner.getSelectedItemPosition());
mAccountSpinner.getSelectedItemPosition());
String txType = getText(R.string.send).toString();
if (mActivityType == ActivityType.SWAP) {
txType = getText(R.string.swap).toString();
@@ -874,9 +868,8 @@ public class BuySendSwapActivity extends AsyncInitializationActivity
ercToken.logo.isEmpty() ? null : ("file://" + tokensPath + "/" + ercToken.logo);
Utils.setBitmapResource(
mExecutor, mHandler, this, iconPath, R.drawable.ic_eth_24, null, assetFromDropDown);
Spinner accountSpinner = findViewById(R.id.accounts_spinner);
updateBalance(
mCustomAccountAdapter.getTitleAtPosition(accountSpinner.getSelectedItemPosition()),
mCustomAccountAdapter.getTitleAtPosition(mAccountSpinner.getSelectedItemPosition()),
true);
if (mActivityType == ActivityType.SWAP) {
enableDisableSwapButton();
@@ -903,9 +896,8 @@ public class BuySendSwapActivity extends AsyncInitializationActivity
ercToken.logo.isEmpty() ? null : ("file://" + tokensPath + "/" + ercToken.logo);
Utils.setBitmapResource(
mExecutor, mHandler, this, iconPath, R.drawable.ic_eth_24, null, assetToDropDown);
Spinner accountSpinner = findViewById(R.id.accounts_spinner);
updateBalance(
mCustomAccountAdapter.getTitleAtPosition(accountSpinner.getSelectedItemPosition()),
mCustomAccountAdapter.getTitleAtPosition(mAccountSpinner.getSelectedItemPosition()),
false);
enableDisableSwapButton();
getSendSwapQuota(true, false);
@@ -325,6 +325,15 @@ public class Utils {
return networkConst;
}
private static String getDecimalsDepNumber(int decimals) {
String strDecimals = "1";
for (int i = 0; i < decimals; i++) {
strDecimals += "0";
}
return strDecimals;
}
public static double fromHexWei(String number, int decimals) {
if (number.equals("0x0")) {
return 0;
@@ -336,11 +345,7 @@ public class Utils {
return 0;
}
BigInteger bigNumber = new BigInteger(number, 16);
String strDecimals = "1";
for (int i = 0; i < decimals; i++) {
strDecimals += "0";
}
BigInteger divider = new BigInteger(strDecimals);
BigInteger divider = new BigInteger(getDecimalsDepNumber(decimals));
BigDecimal bDecimal = new BigDecimal(bigNumber);
BigDecimal bDecimalRes = bDecimal.divide(new BigDecimal(divider), MathContext.DECIMAL32);
String resStr = bDecimalRes.toPlainString();
@@ -374,11 +379,7 @@ public class Utils {
return "0";
}
int dotPosition = number.indexOf(".");
String strDecimals = "1";
for (int i = 0; i < decimals; i++) {
strDecimals += "0";
}
String multiplier = strDecimals;
String multiplier = getDecimalsDepNumber(decimals);
if (dotPosition != -1) {
int zeroToRemove = number.length() - dotPosition - 1;
multiplier = multiplier.substring(0, multiplier.length() - zeroToRemove);
@@ -400,11 +401,7 @@ public class Utils {
return 0;
}
BigInteger bigNumber = new BigInteger(number);
String strDecimals = "1";
for (int i = 0; i < decimals; i++) {
strDecimals += "0";
}
BigInteger divider = new BigInteger(strDecimals);
BigInteger divider = new BigInteger(getDecimalsDepNumber(decimals));
BigDecimal bDecimal = new BigDecimal(bigNumber);
BigDecimal bDecimalRes = bDecimal.divide(new BigDecimal(divider), MathContext.DECIMAL32);
String resStr = bDecimalRes.toPlainString();
@@ -417,11 +414,7 @@ public class Utils {
return "0x0";
}
int dotPosition = number.indexOf(".");
String strDecimals = "1";
for (int i = 0; i < decimals; i++) {
strDecimals += "0";
}
String multiplier = strDecimals;
String multiplier = getDecimalsDepNumber(decimals);
if (dotPosition != -1) {
int zeroToRemove = number.length() - dotPosition - 1;
multiplier = multiplier.substring(0, multiplier.length() - zeroToRemove);
@@ -551,32 +544,26 @@ public class Utils {
public static void setBitmapResource(ExecutorService executor, Handler handler, Context context,
String iconPath, int iconId, ImageView iconImg, TextView textView) {
if (iconPath == null) {
if (iconImg != null) {
iconImg.setImageResource(iconId);
} else if (textView != null) {
textView.setCompoundDrawablesRelativeWithIntrinsicBounds(
iconId, 0, R.drawable.ic_carat_down, 0);
}
return;
}
executor.execute(() -> {
InputStream inputStream = null;
try {
Bitmap logoBitmap = null;
if (iconPath == null) {
handler.post(() -> {
if (iconImg != null) {
iconImg.setImageResource(iconId);
} else if (textView != null) {
textView.setCompoundDrawablesRelativeWithIntrinsicBounds(
iconId, 0, R.drawable.ic_carat_down, 0);
}
});
return;
} else {
Uri logoFileUri = Uri.parse(iconPath);
inputStream = context.getContentResolver().openInputStream(logoFileUri);
int resizeFactor = 110;
if (textView != null) {
resizeFactor = 70;
}
logoBitmap = Utils.resizeBitmap(
BitmapFactory.decodeStream(inputStream), resizeFactor);
inputStream.close();
}
final Bitmap bitmap = logoBitmap;
Uri logoFileUri = Uri.parse(iconPath);
int resizeFactor = 110;
if (textView != null) {
resizeFactor = 70;
}
try (InputStream inputStream =
context.getContentResolver().openInputStream(logoFileUri)) {
final Bitmap bitmap =
Utils.resizeBitmap(BitmapFactory.decodeStream(inputStream), resizeFactor);
handler.post(() -> {
if (iconImg != null) {
iconImg.setImageBitmap(bitmap);
@@ -590,14 +577,6 @@ public class Utils {
org.chromium.base.Log.e("Utils", exc.getMessage());
} catch (IllegalArgumentException exc) {
org.chromium.base.Log.e("Utils", exc.getMessage());
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException exception) {
org.chromium.base.Log.e("Utils", exception.getMessage());
}
}
});
}