From 5d8dbd06640259ff2b295bb14b9d18e7fb8b8057 Mon Sep 17 00:00:00 2001 From: Severiano Jaramillo Date: Mon, 11 Feb 2019 20:38:46 -0600 Subject: [PATCH] Correctly handle the scenario when the user scans a QR that requests an asset that he does not own, when that happens just parse the To and Memo fields and show a snackbar with a proper message, do not fill the amount field. --- .../fragments/SendTransactionFragment.kt | 22 ++++++++++++++----- .../res/layout/fragment_send_transaction.xml | 1 + app/src/main/res/values-es/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/SendTransactionFragment.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/SendTransactionFragment.kt index c1f66f0..14f0f8c 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/SendTransactionFragment.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/SendTransactionFragment.kt @@ -17,9 +17,11 @@ import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.navArgs import com.afollestad.materialdialogs.MaterialDialog import com.afollestad.materialdialogs.customview.customView +import com.google.android.material.snackbar.Snackbar import com.google.common.primitives.UnsignedLong import com.google.zxing.BarcodeFormat import com.google.zxing.Result +import com.jakewharton.rxbinding3.material.dismisses import com.jakewharton.rxbinding3.widget.textChanges import cy.agorise.bitsybitshareswallet.R import cy.agorise.bitsybitshareswallet.adapters.BalancesDetailsAdapter @@ -372,6 +374,12 @@ class SendTransactionFragment : ConnectedFragment(), ZXingScannerView.ResultHand tietTo.setText(invoice.to) + if (invoice.memo != null) { + tietMemo.setText(invoice.memo) + if (invoice.memo.startsWith("PP")) + tietMemo.isEnabled = false + } + var balanceDetail: BalanceDetail? = null // Try to select the invoice's Asset in the Assets spinner @@ -386,18 +394,20 @@ class SendTransactionFragment : ConnectedFragment(), ZXingScannerView.ResultHand } } - if (invoice.memo != null) { - tietMemo.setText(invoice.memo) - if (invoice.memo.startsWith("PP")) - tietMemo.isEnabled = false + // If the user does not own any of the requested asset then show a SnackBar to explain the issue and + // return early to avoid filling the asset field + if (balanceDetail == null) { + Snackbar.make(rootView, getString(R.string.error__you_dont_own_asset, invoice.currency.toUpperCase()), + Snackbar.LENGTH_INDEFINITE).setAction(android.R.string.ok) { }.show() + return } var amount = 0.0 for (nextItem in invoice.lineItems) { amount += nextItem.quantity * nextItem.price } - // TODO Improve pattern to account for different asset precisions - val df = DecimalFormat("####." + "#".repeat(balanceDetail?.precision ?: 5)) + + val df = DecimalFormat("####." + "#".repeat(balanceDetail.precision)) df.roundingMode = RoundingMode.CEILING df.decimalFormatSymbols = DecimalFormatSymbols(Locale.getDefault()) tietAmount.setText(df.format(amount)) diff --git a/app/src/main/res/layout/fragment_send_transaction.xml b/app/src/main/res/layout/fragment_send_transaction.xml index 141c97c..452396c 100644 --- a/app/src/main/res/layout/fragment_send_transaction.xml +++ b/app/src/main/res/layout/fragment_send_transaction.xml @@ -8,6 +8,7 @@ tools:context=".fragments.SendTransactionFragment"> El permiso de cámara es necesario para leer códigos QR. ¡Transacción enviada! No se pudo enviar la transacción + No tienes nada de %1$s Escribe la cuenta BitShares de la persona a la que le deseas enviar fondos.\nPor ejemplo: agorise-faucet diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b86f7a1..be17c4c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -99,6 +99,7 @@ Camera permission is necessary to read QR codes. Transaction sent! Unable to send transaction + You don\'t own any %1$s Type in the Bitshares account name of the person you want to send funds to.\nFor example: agorise-faucet