Skip to content

Commit

Permalink
Send.kt cleanups:
Browse files Browse the repository at this point in the history
* Make unbroadcasted flag an instance variable rather than a global.
* Fix duplicate code in `if` statements.
  • Loading branch information
mhsmith committed Sep 26, 2020
1 parent a54fcbd commit 6ee6ceb
Showing 1 changed file with 22 additions and 29 deletions.
51 changes: 22 additions & 29 deletions android/app/src/main/java/org/electroncash/electroncash3/Send.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import kotlin.properties.Delegates.notNull
val libPaymentRequest by lazy { libMod("paymentrequest") }

val MIN_FEE = 1 // sat/byte
var DONT_SEND = false


class SendDialog : AlertDialogFragment() {
Expand All @@ -33,6 +32,10 @@ class SendDialog : AlertDialogFragment() {
}
val model: Model by viewModels()

val unbroadcasted by lazy {
arguments?.getBoolean("unbroadcasted", false) ?: false
}

init {
if (daemonModel.wallet!!.callAttr("is_watching_only").toBoolean()) {
throw ToastException(R.string.this_wallet_is)
Expand All @@ -45,35 +48,29 @@ class SendDialog : AlertDialogFragment() {
}

override fun onBuildDialog(builder: AlertDialog.Builder) {
DONT_SEND = if (arguments != null) {
arguments!!.getBoolean("unbroadcasted", false)
} else {
false
}
if (!DONT_SEND) {
if (!unbroadcasted) {
builder.setTitle(R.string.send)
.setView(R.layout.send)
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(android.R.string.ok, null)
.setNeutralButton(R.string.qr_code, null)
.setPositiveButton(R.string.send, null)
} else {
builder.setTitle(R.string.sign_transaction)
.setView(R.layout.send)
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(R.string.sign, null)
.setNeutralButton(R.string.qr_code, null)
.setPositiveButton(R.string.sign, null)
}
builder.setView(R.layout.send)
.setNegativeButton(android.R.string.cancel, null)
.setNeutralButton(R.string.qr_code, null)
}

override fun onShowDialog() {
override fun onFirstShowDialog() {
if (arguments != null) {
val address = arguments!!.getString("address")
if (address != null) {
etAddress.setText(address)
etAmount.requestFocus()
}
arguments = null
}
}

override fun onShowDialog() {
setPaymentRequest(model.paymentRequest)

etAmount.addTextChangedListener(object : TextWatcher {
Expand Down Expand Up @@ -299,8 +296,8 @@ class SendPasswordDialog : PasswordDialog<Unit>() {

override fun onPassword(password: String) {
val wallet = daemonModel.wallet!!
if (!DONT_SEND) {
wallet.callAttr("sign_transaction", model.tx, password)
wallet.callAttr("sign_transaction", model.tx, password)
if (!sendDialog.unbroadcasted) {
if (!daemonModel.isConnected()) {
throw ToastException(R.string.not_connected)
}
Expand All @@ -314,20 +311,16 @@ class SendPasswordDialog : PasswordDialog<Unit>() {
}
checkBroadcastResult(result)
setDescription(model.tx.callAttr("txid").toString(), model.description)
} else {
wallet.callAttr("sign_transaction", model.tx, password)
}
}

override fun onPostExecute(result: Unit) {
if (!DONT_SEND) {
sendDialog.dismiss()
toast(R.string.payment_sent, Toast.LENGTH_SHORT)
} else {
sendDialog.dismiss()
copyToClipboard(model.tx.toString(), R.string.signed_transaction)

}
sendDialog.dismiss()
if (!sendDialog.unbroadcasted) {
toast(R.string.payment_sent, Toast.LENGTH_SHORT)
} else {
copyToClipboard(model.tx.toString(), R.string.signed_transaction)
}
}
}

Expand Down

0 comments on commit 6ee6ceb

Please sign in to comment.