From 04b509651b671822408ec63c9fababf156c4a37e Mon Sep 17 00:00:00 2001 From: Severiano Jaramillo Date: Fri, 7 Dec 2018 15:18:23 -0600 Subject: [PATCH] - Adjusted how the Android virtual keyboard appears on the screen so that it does not distort the QR code image. - Created a method to update the QR code when the user updates the Amount text field information, using RxJava debounce to avoid calls while the user is still typing. - Created a method to remove the QR when there is no Asset selected. --- app/src/main/AndroidManifest.xml | 4 +- .../fragments/ReceiveTransactionFragment.kt | 95 ++++++++++++------- .../fragments/SendTransactionFragment.kt | 1 + .../layout/fragment_receive_transaction.xml | 4 +- app/src/main/res/values/strings.xml | 2 + 5 files changed, 67 insertions(+), 39 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ca59e1b..74048ff 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -26,7 +26,9 @@ - + diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/ReceiveTransactionFragment.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/ReceiveTransactionFragment.kt index f2ff4be..c812567 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/ReceiveTransactionFragment.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/ReceiveTransactionFragment.kt @@ -24,8 +24,13 @@ import cy.agorise.bitsybitshareswallet.utils.Constants import cy.agorise.bitsybitshareswallet.viewmodels.AssetViewModel import cy.agorise.bitsybitshareswallet.viewmodels.UserAccountViewModel import cy.agorise.graphenej.* +import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.CompositeDisposable import kotlinx.android.synthetic.main.fragment_receive_transaction.* +import java.lang.Exception +import java.math.RoundingMode +import java.text.DecimalFormat +import java.text.DecimalFormatSymbols import java.util.* import java.util.concurrent.TimeUnit @@ -75,33 +80,53 @@ class ReceiveTransactionFragment : Fragment() { updateQR() } + // Use RxJava Debounce to create QR code only after the user stopped typing an amount + mDisposables.add( + RxTextView.textChanges(tietAmount) + .debounce(1000, TimeUnit.MILLISECONDS) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe { updateQR() } + ) + // Use RxJava Debounce to avoid making calls to the NetworkService on every text change event -// mDisposables.add( -// RxTextView.textChanges(tietAmount) -// .debounce(500, TimeUnit.MILLISECONDS) -// .map { it.toString().trim() } -// .filter { it.length > 1 } -// .subscribe { -// -// } -// ) + mDisposables.add( + RxTextView.textChanges(actvAsset) + .debounce(500, TimeUnit.MILLISECONDS) + .filter { it.toString().length < 3 } + .observeOn(AndroidSchedulers.mainThread()) + .subscribe { + mAsset = null + updateQR() + } + ) } private fun updateQR() { - val amount: Long = 10 + if (mAsset == null) { + ivQR.setImageDrawable(null) + return + } - val total = Util.fromBase(AssetAmount(UnsignedLong.valueOf(amount), mAsset!!)) - val items = arrayOf(LineItem("transfer", 1, total)) + // Try to obtain the amount from the Amount Text Field or make it zero otherwise + val amount: Long = try { + val tmpAmount = tietAmount.text.toString().toDouble() + (tmpAmount * Math.pow(10.0, mAsset!!.precision.toDouble())).toLong() + }catch (e: Exception) { + 0 + } + + val total = AssetAmount(UnsignedLong.valueOf(amount), mAsset!!) + val totalInDouble = Util.fromBase(total) + val items = arrayOf(LineItem("transfer", 1, totalInDouble)) val invoice = Invoice(mUserAccount!!.name, "", "#bitsy", mAsset!!.symbol, items, "", "") Log.d(TAG, "invoice: " + invoice.toJsonString()) try { val bitmap = encodeAsBitmap(Invoice.toQrCode(invoice), "#139657") // PalmPay green ivQR.setImageBitmap(bitmap) -// updateAmountAddressUI(total, mUserAccount!!.getName()) + updateAmountAddressUI(total, mUserAccount!!.name) } catch (e: WriterException) { Log.e(TAG, "WriterException. Msg: " + e.message) } - } /** @@ -149,26 +174,24 @@ class ReceiveTransactionFragment : Fragment() { return bitmap } -// /** -// * Updates the UI to show amount and address to send the payment -// * -// * @param amount Amount in crypto to be paid -// * @param address Address to pay amount -// */ -// private fun updateAmountAddressUI(amount: Double, account: String) { -// // Trick to format correctly really small floating point numbers -// val df = DecimalFormat("0", DecimalFormatSymbols.getInstance(Locale.ENGLISH)) -// df.maximumFractionDigits = 340 -// -// val cryptoAmount = Helper.getCryptoAmountLocaleFormatted( -// locale, amount, -// inputCoinType.toLowerCase(), this -// ) -// -// val txtAmount = getString(R.string.please_pay_s_s, cryptoAmount, inputCoinType.toUpperCase()) -// val txtAddress = getString(R.string.to_s, account) -// -// tvTotalCryptoAmount.setText(txtAmount) -// tvReceivingAddress.setText(txtAddress) -// } + /** + * Updates the UI to show amount and account to send the payment + * + * @param total Total Amount in crypto to be paid + * @param account Account to pay total + */ + private fun updateAmountAddressUI(total: AssetAmount, account: String) { + val df = DecimalFormat("####."+("#".repeat(total.asset.precision))) + df.roundingMode = RoundingMode.CEILING + df.decimalFormatSymbols = DecimalFormatSymbols(Locale.getDefault()) + + val amount = total.amount.toDouble() / Math.pow(10.toDouble(), total.asset.precision.toDouble()) + val strAmount = df.format(amount) + + val txtAmount = getString(R.string.template__please_pay, strAmount, total.asset.symbol) + val txtAccount = getString(R.string.template__to, account) + + tvPleasePay.text = txtAmount + tvTo.text = txtAccount + } } \ No newline at end of file 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 84aa3ce..6ac81b9 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/SendTransactionFragment.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/SendTransactionFragment.kt @@ -335,6 +335,7 @@ class SendTransactionFragment : Fragment(), ZXingScannerView.ResultHandler, Serv for (nextItem in invoice.lineItems) { amount += nextItem.quantity * nextItem.price } + // TODO Improve pattern to account for different asset precisions val df = DecimalFormat("####.#####") df.roundingMode = RoundingMode.CEILING df.decimalFormatSymbols = DecimalFormatSymbols(Locale.getDefault()) diff --git a/app/src/main/res/layout/fragment_receive_transaction.xml b/app/src/main/res/layout/fragment_receive_transaction.xml index 61bb58d..4bca35b 100644 --- a/app/src/main/res/layout/fragment_receive_transaction.xml +++ b/app/src/main/res/layout/fragment_receive_transaction.xml @@ -79,7 +79,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLines="1" - android:text="Please Pay: 12.25316 BTS" + tools:text="Please Pay: 12.25316 BTS" android:textAppearance="@style/TextAppearance.Bitsy.Body1" android:textAlignment="center" app:layout_constraintBottom_toTopOf="@id/tvTo"/> @@ -89,7 +89,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLines="1" - android:text="To: seventest-3" + tools:text="To: seventest-3" android:textAppearance="@style/TextAppearance.Bitsy.Body1" android:textAlignment="center" app:layout_constraintBottom_toBottomOf="parent"/> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e3082b9..ee74fea 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -47,6 +47,8 @@ Memo Scan QR Asset + Please pay: %1$s %2$s + To: %1$s Settings