Create a toast extension function to reduce the boilerplate code required to create Toasts. Extracted string resources fron SendTransactionFragment and used the new toast extension funcion.

This commit is contained in:
Severiano Jaramillo 2019-01-03 09:45:47 -06:00
parent 8eed991e0e
commit c068efa84c
3 changed files with 33 additions and 17 deletions

View file

@ -27,10 +27,7 @@ import cy.agorise.bitsybitshareswallet.R
import cy.agorise.bitsybitshareswallet.adapters.BalancesDetailsAdapter import cy.agorise.bitsybitshareswallet.adapters.BalancesDetailsAdapter
import cy.agorise.bitsybitshareswallet.database.joins.BalanceDetail import cy.agorise.bitsybitshareswallet.database.joins.BalanceDetail
import cy.agorise.bitsybitshareswallet.repositories.AuthorityRepository import cy.agorise.bitsybitshareswallet.repositories.AuthorityRepository
import cy.agorise.bitsybitshareswallet.utils.Constants import cy.agorise.bitsybitshareswallet.utils.*
import cy.agorise.bitsybitshareswallet.utils.CryptoUtils
import cy.agorise.bitsybitshareswallet.utils.disable
import cy.agorise.bitsybitshareswallet.utils.enable
import cy.agorise.bitsybitshareswallet.viewmodels.BalanceDetailViewModel import cy.agorise.bitsybitshareswallet.viewmodels.BalanceDetailViewModel
import cy.agorise.graphenej.* import cy.agorise.graphenej.*
import cy.agorise.graphenej.api.ConnectionStatusUpdate import cy.agorise.graphenej.api.ConnectionStatusUpdate
@ -241,7 +238,7 @@ class SendTransactionFragment : Fragment(), ZXingScannerView.ResultHandler, Serv
} else { } else {
mSelectedUserAccount = null mSelectedUserAccount = null
destinationPublicKey = null destinationPublicKey = null
tilTo.error = "Invalid account" tilTo.error = getString(R.string.error__invalid_account)
isToAccountCorrect = false isToAccountCorrect = false
} }
@ -261,7 +258,7 @@ class SendTransactionFragment : Fragment(), ZXingScannerView.ResultHandler, Serv
val id = mNetworkService!!.sendMessage(GetRequiredFees(transaction!!, asset), GetRequiredFees.REQUIRED_API) val id = mNetworkService!!.sendMessage(GetRequiredFees(transaction!!, asset), GetRequiredFees.REQUIRED_API)
responseMap[id] = RESPONSE_GET_REQUIRED_FEES responseMap[id] = RESPONSE_GET_REQUIRED_FEES
} else { } else {
// TODO unableToSendTransactionError() context?.toast(getString(R.string.msg__transaction_not_sent))
} }
} }
@ -273,14 +270,13 @@ class SendTransactionFragment : Fragment(), ZXingScannerView.ResultHandler, Serv
val id = mNetworkService!!.sendMessage(BroadcastTransaction(transaction), BroadcastTransaction.REQUIRED_API) val id = mNetworkService!!.sendMessage(BroadcastTransaction(transaction), BroadcastTransaction.REQUIRED_API)
responseMap[id] = RESPONSE_BROADCAST_TRANSACTION responseMap[id] = RESPONSE_BROADCAST_TRANSACTION
} else { } else {
// TODO unableToSendTransactionError() context?.toast(getString(R.string.msg__transaction_not_sent))
} }
} }
private fun handleBroadcastTransaction(message: JsonRpcResponse<*>) { private fun handleBroadcastTransaction(message: JsonRpcResponse<*>) {
if (message.result == null && message.error == null) { if (message.result == null && message.error == null) {
// TODO extract string resources context?.toast(getString(R.string.text__transaction_sent))
Toast.makeText(context!!, "Transaction sent!", Toast.LENGTH_SHORT).show()
// Remove information from the text fields and disable send button // Remove information from the text fields and disable send button
tietTo.setText("") tietTo.setText("")
@ -289,9 +285,10 @@ class SendTransactionFragment : Fragment(), ZXingScannerView.ResultHandler, Serv
isToAccountCorrect = false isToAccountCorrect = false
isAmountCorrect = false isAmountCorrect = false
enableDisableSendFAB() enableDisableSendFAB()
// TODO return to Main fragment ??
} else { } else {
// TODO extract error messages to show a better explanation to the user context?.toast(message.error.message, Toast.LENGTH_LONG)
Toast.makeText(context!!, message.error.message, Toast.LENGTH_LONG).show()
} }
} }
@ -313,8 +310,7 @@ class SendTransactionFragment : Fragment(), ZXingScannerView.ResultHandler, Serv
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) { if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
startCameraPreview() startCameraPreview()
} else { } else {
// TODO extract string resource context?.toast(getString(R.string.msg__camera_permission_necessary))
Toast.makeText(context!!, "Camera permission is necessary to read QR codes.", Toast.LENGTH_SHORT).show()
} }
return return
} }
@ -380,8 +376,7 @@ class SendTransactionFragment : Fragment(), ZXingScannerView.ResultHandler, Serv
val currentAmount = balance.amount.toDouble() / Math.pow(10.0, balance.precision.toDouble()) val currentAmount = balance.amount.toDouble() / Math.pow(10.0, balance.precision.toDouble())
if (currentAmount < amount) { if (currentAmount < amount) {
// TODO extract string resource tilAmount.error = getString(R.string.error__not_enough_funds)
tilAmount.error = "Not enough funds"
isAmountCorrect = false isAmountCorrect = false
} else { } else {
tilAmount.isErrorEnabled = false tilAmount.isErrorEnabled = false

View file

@ -1,15 +1,30 @@
package cy.agorise.bitsybitshareswallet.utils package cy.agorise.bitsybitshareswallet.utils
import android.content.Context
import android.content.res.ColorStateList import android.content.res.ColorStateList
import android.widget.Toast
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import com.google.android.material.floatingactionbutton.FloatingActionButton import com.google.android.material.floatingactionbutton.FloatingActionButton
/**
* Creates an enabled state, by enabling the button and using the given [colorResource] to color it.
*/
fun FloatingActionButton.enable(colorResource: Int) { fun FloatingActionButton.enable(colorResource: Int) {
this.isEnabled = true this.isEnabled = true
this.backgroundTintList = ColorStateList.valueOf(ContextCompat.getColor(this.context, colorResource)) this.backgroundTintList = ColorStateList.valueOf(ContextCompat.getColor(this.context, colorResource))
} }
/**
* Creates a disabled state, by disabling the button and using the given [colorResource] to color it.
*/
fun FloatingActionButton.disable(colorResource: Int) { fun FloatingActionButton.disable(colorResource: Int) {
this.isEnabled = false this.isEnabled = false
this.backgroundTintList = ColorStateList.valueOf(ContextCompat.getColor(this.context, colorResource)) this.backgroundTintList = ColorStateList.valueOf(ContextCompat.getColor(this.context, colorResource))
} }
/**
* Easily create a toast message with less boilerplate code
*/
fun Context.toast(message: CharSequence, duration: Int = Toast.LENGTH_LONG) {
Toast.makeText(this, message, duration).show()
}

View file

@ -49,11 +49,18 @@
<string name="title_filter">Filter</string> <string name="title_filter">Filter</string>
<string name="title_export">Export</string> <string name="title_export">Export</string>
<!-- Send & Receive Transaction --> <!-- Send Transaction -->
<string name="text__to">To</string> <string name="text__to">To</string>
<string name="text__amount">Amount</string> <string name="text__amount">Amount</string>
<string name="text__memo">Memo</string> <string name="text__memo">Memo</string>
<string name="text__scan_qr">Scan QR</string> <string name="text__scan_qr">Scan QR</string>
<string name="error__invalid_account">Invalid account</string>
<string name="error__not_enough_funds">Not enough funds</string>
<string name="msg__camera_permission_necessary">Camera permission is necessary to read QR codes.</string>
<string name="text__transaction_sent">Transaction sent!</string>
<string name="msg__transaction_not_sent">Unable to send transaction</string>
<!-- Receive Transaction -->
<string name="text__asset">Asset</string> <string name="text__asset">Asset</string>
<string name="text__other">Other…</string> <string name="text__other">Other…</string>
<string name="template__please_send">Please Send: %1$s %2$s</string> <string name="template__please_send">Please Send: %1$s %2$s</string>
@ -83,5 +90,4 @@
</string> </string>
<string name="title__bitshares_nodes_dialog">Block: %1$s</string> <string name="title__bitshares_nodes_dialog">Block: %1$s</string>
</resources> </resources>