- Add a amount verification to the SendTransactionFragment to ensure the user has at least that amount of the selected asset in his account.
- Show the balance of the current selected asset below the asset Spinner. - Enable the send transaction button only after verifying that both the to account and the amount are both valid.
This commit is contained in:
parent
4739e41fce
commit
53479f8837
3 changed files with 60 additions and 11 deletions
|
@ -55,6 +55,8 @@ class SendTransactionFragment : Fragment(), ZXingScannerView.ResultHandler, Serv
|
|||
private val RESPONSE_GET_ACCOUNT_BY_NAME = 1
|
||||
|
||||
private var isCameraPreviewVisible = false
|
||||
private var isToAccountCorrect = false
|
||||
private var isAmountCorrect = false
|
||||
|
||||
private var mBalancesDetails: List<BalanceDetail>? = null
|
||||
|
||||
|
@ -119,7 +121,13 @@ class SendTransactionFragment : Fragment(), ZXingScannerView.ResultHandler, Serv
|
|||
override fun onNothingSelected(parent: AdapterView<*>?) { }
|
||||
|
||||
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
|
||||
selectedAssetSymbol = mAssetsAdapter!!.getItem(position)!!.symbol
|
||||
val balance = mAssetsAdapter!!.getItem(position)!!
|
||||
selectedAssetSymbol = balance.symbol
|
||||
|
||||
val amount = balance.amount.toDouble() / Math.pow(10.0, balance.precision.toDouble())
|
||||
|
||||
tvAvailableAssetAmount.text =
|
||||
String.format("%." + Math.min(balance.precision, 8) + "f %s", amount, balance.symbol)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -128,15 +136,23 @@ class SendTransactionFragment : Fragment(), ZXingScannerView.ResultHandler, Serv
|
|||
|
||||
// Use RxJava Debounce to avoid making calls to the NetworkService on every text change event
|
||||
mDisposables.add(RxTextView.textChanges(tietTo)
|
||||
.debounce(500, TimeUnit.MILLISECONDS)
|
||||
.map { it.toString().trim() }
|
||||
.filter { it.length > 1 }
|
||||
.debounce(500, TimeUnit.MILLISECONDS)
|
||||
.subscribe {
|
||||
val id = mNetworkService!!.sendMessage(GetAccountByName(it!!), GetAccountByName.REQUIRED_API)
|
||||
responseMap[id] = RESPONSE_GET_ACCOUNT_BY_NAME
|
||||
}
|
||||
)
|
||||
|
||||
mDisposables.add(RxTextView.textChanges(tietAmount)
|
||||
.debounce(500, TimeUnit.MILLISECONDS)
|
||||
.filter { it.isNotEmpty() }
|
||||
.map { it.toString().trim().toDouble() }
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe { validateAmount(it!!) }
|
||||
)
|
||||
|
||||
mDisposables.add(RxBus.getBusInstance()
|
||||
.asFlowable()
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
|
@ -164,12 +180,14 @@ class SendTransactionFragment : Fragment(), ZXingScannerView.ResultHandler, Serv
|
|||
if (result is AccountProperties) {
|
||||
mSelectedUserAccount = UserAccount(result.id, result.name)
|
||||
tilTo.isErrorEnabled = false
|
||||
fabSendTransaction.show()
|
||||
isToAccountCorrect = true
|
||||
} else {
|
||||
mSelectedUserAccount = null
|
||||
tilTo.error = "Invalid account"
|
||||
fabSendTransaction.hide()
|
||||
isToAccountCorrect = false
|
||||
}
|
||||
|
||||
enableDisableSendFAB()
|
||||
}
|
||||
|
||||
private fun verifyCameraPermission() {
|
||||
|
@ -250,13 +268,30 @@ class SendTransactionFragment : Fragment(), ZXingScannerView.ResultHandler, Serv
|
|||
}
|
||||
}
|
||||
|
||||
private fun validateAmount(amount: Double) {
|
||||
val balance = mAssetsAdapter!!.getItem(spAsset.selectedItemPosition)!!
|
||||
val currentAmount = balance.amount.toDouble() / Math.pow(10.0, balance.precision.toDouble())
|
||||
|
||||
if (currentAmount < amount) {
|
||||
tilAmount.error = "Not enough funds"
|
||||
isAmountCorrect = false
|
||||
} else {
|
||||
tilAmount.isErrorEnabled = false
|
||||
isAmountCorrect = true
|
||||
}
|
||||
|
||||
enableDisableSendFAB()
|
||||
}
|
||||
|
||||
private fun enableDisableSendFAB() {
|
||||
if (isToAccountCorrect && isAmountCorrect)
|
||||
fabSendTransaction.show()
|
||||
else
|
||||
fabSendTransaction.hide()
|
||||
}
|
||||
|
||||
private fun validateFields() {
|
||||
tilAmount.isErrorEnabled = false
|
||||
|
||||
// val selectedAsset = mAssetsAdapter!!.getItem(spAsset.selectedItemPosition)
|
||||
// val selectedAmount = tietAmount.getTex
|
||||
|
||||
// Create TransferOperation
|
||||
// Create TransferOperation
|
||||
val builder = TransferOperationBuilder()
|
||||
builder.setSource(mUserAccount)
|
||||
}
|
||||
|
|
|
@ -43,7 +43,9 @@
|
|||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:digits="abcdefghijklmnopqrstuvwxyz-0123456789"
|
||||
android:maxLines="1"/>
|
||||
android:maxLines="1"
|
||||
android:lines="1"
|
||||
android:imeOptions="actionNext"/>
|
||||
|
||||
</com.google.android.material.textfield.TextInputLayout>
|
||||
|
||||
|
@ -90,6 +92,17 @@
|
|||
app:layout_constraintEnd_toEndOf="@+id/spAsset"
|
||||
app:layout_constraintStart_toStartOf="@+id/spAsset" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvAvailableAssetAmount"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:textAppearance="@style/TextAppearance.Bitsy.Caption"
|
||||
tools:text="12.01253 BTS"
|
||||
android:textAlignment="center"
|
||||
app:layout_constraintTop_toBottomOf="@id/vSpinner"
|
||||
app:layout_constraintStart_toStartOf="@id/vSpinner"
|
||||
app:layout_constraintEnd_toEndOf="@id/vSpinner"/>
|
||||
|
||||
<com.google.android.material.textfield.TextInputLayout
|
||||
android:id="@+id/tilMemo"
|
||||
style="@style/Widget.Bitsy.TextInputLayout"
|
||||
|
|
|
@ -47,4 +47,5 @@
|
|||
<style name="TextAppearance.Bitsy.Subtitle1" parent="TextAppearance.MaterialComponents.Subtitle1" />
|
||||
<style name="TextAppearance.Bitsy.Body1" parent="TextAppearance.MaterialComponents.Body1" />
|
||||
<style name="TextAppearance.Bitsy.Body2" parent="TextAppearance.MaterialComponents.Body2" />
|
||||
<style name="TextAppearance.Bitsy.Caption" parent="TextAppearance.MaterialComponents.Caption"/>
|
||||
</resources>
|
||||
|
|
Loading…
Reference in a new issue