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.

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

View File

@ -1,15 +1,30 @@
package cy.agorise.bitsybitshareswallet.utils
import android.content.Context
import android.content.res.ColorStateList
import android.widget.Toast
import androidx.core.content.ContextCompat
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) {
this.isEnabled = true
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) {
this.isEnabled = false
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_export">Export</string>
<!-- Send & Receive Transaction -->
<!-- Send Transaction -->
<string name="text__to">To</string>
<string name="text__amount">Amount</string>
<string name="text__memo">Memo</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__other">Other…</string>
<string name="template__please_send">Please Send: %1$s %2$s</string>
@ -83,5 +90,4 @@
</string>
<string name="title__bitshares_nodes_dialog">Block: %1$s</string>
</resources>