2022-03-08 11:05:03 -08:00
|
|
|
package co.electriccoin.zcash.ui.screen.send.view
|
2022-03-01 05:11:23 -08:00
|
|
|
|
|
|
|
import androidx.compose.foundation.layout.Column
|
2022-06-09 09:31:44 -07:00
|
|
|
import androidx.compose.foundation.layout.PaddingValues
|
2022-03-01 05:11:23 -08:00
|
|
|
import androidx.compose.foundation.layout.Row
|
|
|
|
import androidx.compose.foundation.layout.Spacer
|
|
|
|
import androidx.compose.foundation.layout.fillMaxHeight
|
|
|
|
import androidx.compose.foundation.layout.fillMaxWidth
|
2022-06-09 09:31:44 -07:00
|
|
|
import androidx.compose.foundation.layout.padding
|
2022-03-01 05:11:23 -08:00
|
|
|
import androidx.compose.foundation.layout.size
|
|
|
|
import androidx.compose.foundation.text.KeyboardOptions
|
|
|
|
import androidx.compose.material.icons.Icons
|
|
|
|
import androidx.compose.material.icons.filled.ArrowBack
|
|
|
|
import androidx.compose.material3.Button
|
|
|
|
import androidx.compose.material3.ExperimentalMaterial3Api
|
|
|
|
import androidx.compose.material3.Icon
|
|
|
|
import androidx.compose.material3.IconButton
|
|
|
|
import androidx.compose.material3.Scaffold
|
|
|
|
import androidx.compose.material3.SmallTopAppBar
|
|
|
|
import androidx.compose.material3.Text
|
2022-04-04 05:56:34 -07:00
|
|
|
import androidx.compose.material3.TextField
|
2022-03-01 05:11:23 -08:00
|
|
|
import androidx.compose.runtime.Composable
|
|
|
|
import androidx.compose.runtime.getValue
|
|
|
|
import androidx.compose.runtime.mutableStateOf
|
|
|
|
import androidx.compose.runtime.saveable.rememberSaveable
|
|
|
|
import androidx.compose.runtime.setValue
|
|
|
|
import androidx.compose.ui.Modifier
|
2022-04-01 07:28:16 -07:00
|
|
|
import androidx.compose.ui.platform.LocalContext
|
2022-03-01 05:11:23 -08:00
|
|
|
import androidx.compose.ui.res.stringResource
|
|
|
|
import androidx.compose.ui.text.input.KeyboardType
|
|
|
|
import androidx.compose.ui.tooling.preview.Preview
|
|
|
|
import androidx.compose.ui.unit.dp
|
2022-04-08 05:33:08 -07:00
|
|
|
import cash.z.ecc.sdk.ext.ui.ZecSendExt
|
2022-04-01 07:28:16 -07:00
|
|
|
import cash.z.ecc.sdk.ext.ui.ZecStringExt
|
2022-04-08 05:33:08 -07:00
|
|
|
import cash.z.ecc.sdk.ext.ui.model.MonetarySeparators
|
|
|
|
import cash.z.ecc.sdk.ext.ui.model.ZecString
|
|
|
|
import cash.z.ecc.sdk.ext.ui.model.toZecString
|
2022-03-01 05:11:23 -08:00
|
|
|
import cash.z.ecc.sdk.fixture.ZatoshiFixture
|
|
|
|
import cash.z.ecc.sdk.model.Memo
|
|
|
|
import cash.z.ecc.sdk.model.Zatoshi
|
|
|
|
import cash.z.ecc.sdk.model.ZecSend
|
2022-03-08 11:05:03 -08:00
|
|
|
import co.electriccoin.zcash.ui.R
|
|
|
|
import co.electriccoin.zcash.ui.design.MINIMAL_WEIGHT
|
|
|
|
import co.electriccoin.zcash.ui.design.component.GradientSurface
|
|
|
|
import co.electriccoin.zcash.ui.design.component.PrimaryButton
|
|
|
|
import co.electriccoin.zcash.ui.design.theme.ZcashTheme
|
|
|
|
import co.electriccoin.zcash.ui.screen.send.ext.ABBREVIATION_INDEX
|
|
|
|
import co.electriccoin.zcash.ui.screen.send.ext.Saver
|
|
|
|
import co.electriccoin.zcash.ui.screen.send.ext.abbreviated
|
|
|
|
import co.electriccoin.zcash.ui.screen.send.model.SendStage
|
2022-03-01 05:11:23 -08:00
|
|
|
|
|
|
|
@Composable
|
|
|
|
@Preview
|
|
|
|
fun PreviewSend() {
|
|
|
|
ZcashTheme(darkTheme = true) {
|
|
|
|
GradientSurface {
|
|
|
|
Send(
|
|
|
|
mySpendableBalance = ZatoshiFixture.new(),
|
|
|
|
goBack = {},
|
|
|
|
onCreateAndSend = {}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@OptIn(ExperimentalMaterial3Api::class)
|
|
|
|
@Composable
|
|
|
|
fun Send(
|
|
|
|
mySpendableBalance: Zatoshi,
|
|
|
|
goBack: () -> Unit,
|
|
|
|
onCreateAndSend: (ZecSend) -> Unit,
|
|
|
|
) {
|
|
|
|
// For now, we're avoiding sub-navigation to keep the navigation logic simple. But this might
|
|
|
|
// change once deep-linking support is added. It depends on whether deep linking should do one of:
|
|
|
|
// 1. Use a different UI flow entirely
|
|
|
|
// 2. Show a pre-filled Send form
|
|
|
|
// 3. Go directly to the press-and-hold confirmation
|
|
|
|
val (sendStage, setSendStage) = rememberSaveable { mutableStateOf(SendStage.Form) }
|
|
|
|
|
|
|
|
Scaffold(topBar = {
|
|
|
|
SendTopAppBar(onBack = {
|
|
|
|
when (sendStage) {
|
|
|
|
SendStage.Form -> goBack()
|
|
|
|
SendStage.Confirmation -> setSendStage(SendStage.Form)
|
|
|
|
}
|
|
|
|
})
|
2022-06-09 09:31:44 -07:00
|
|
|
}) { paddingValues ->
|
2022-03-01 05:11:23 -08:00
|
|
|
SendMainContent(
|
2022-06-09 09:31:44 -07:00
|
|
|
paddingValues,
|
2022-03-01 05:11:23 -08:00
|
|
|
mySpendableBalance,
|
|
|
|
sendStage,
|
|
|
|
setSendStage,
|
|
|
|
onCreateAndSend = onCreateAndSend
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
private fun SendTopAppBar(onBack: () -> Unit) {
|
|
|
|
SmallTopAppBar(
|
|
|
|
title = { Text(text = stringResource(id = R.string.send_title)) },
|
|
|
|
navigationIcon = {
|
|
|
|
IconButton(
|
|
|
|
onClick = onBack
|
|
|
|
) {
|
|
|
|
Icon(
|
|
|
|
imageVector = Icons.Filled.ArrowBack,
|
|
|
|
contentDescription = stringResource(R.string.send_back_content_description)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
private fun SendMainContent(
|
2022-06-09 09:31:44 -07:00
|
|
|
paddingValues: PaddingValues,
|
2022-03-01 05:11:23 -08:00
|
|
|
myBalance: Zatoshi,
|
|
|
|
sendStage: SendStage,
|
|
|
|
setSendStage: (SendStage) -> Unit,
|
|
|
|
onCreateAndSend: (ZecSend) -> Unit
|
|
|
|
) {
|
|
|
|
val (zecSend, setZecSend) = rememberSaveable(stateSaver = ZecSend.Saver) { mutableStateOf(null) }
|
|
|
|
|
|
|
|
if (sendStage == SendStage.Form || null == zecSend) {
|
|
|
|
SendForm(
|
2022-06-09 09:31:44 -07:00
|
|
|
paddingValues,
|
2022-03-01 05:11:23 -08:00
|
|
|
myBalance = myBalance,
|
2022-06-09 09:31:44 -07:00
|
|
|
previousZecSend = zecSend
|
|
|
|
) {
|
|
|
|
setSendStage(SendStage.Confirmation)
|
|
|
|
setZecSend(it)
|
|
|
|
}
|
2022-03-01 05:11:23 -08:00
|
|
|
} else {
|
2022-06-09 09:31:44 -07:00
|
|
|
Confirmation(
|
|
|
|
paddingValues,
|
|
|
|
zecSend
|
|
|
|
) {
|
2022-03-01 05:11:23 -08:00
|
|
|
onCreateAndSend(zecSend)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO [#217]: Need to handle changing of Locale after user input, but before submitting the button.
|
2022-04-01 07:28:16 -07:00
|
|
|
// TODO [#288]: TextField component can't do long-press backspace.
|
|
|
|
// TODO [#294]: DetektAll failed LongMethod
|
|
|
|
@Suppress("LongMethod")
|
2022-03-01 05:11:23 -08:00
|
|
|
@Composable
|
|
|
|
private fun SendForm(
|
2022-06-09 09:31:44 -07:00
|
|
|
paddingValues: PaddingValues,
|
2022-03-01 05:11:23 -08:00
|
|
|
myBalance: Zatoshi,
|
|
|
|
previousZecSend: ZecSend?,
|
|
|
|
onCreateAndSend: (ZecSend) -> Unit
|
|
|
|
) {
|
2022-04-01 07:28:16 -07:00
|
|
|
val context = LocalContext.current
|
2022-03-01 05:11:23 -08:00
|
|
|
val monetarySeparators = MonetarySeparators.current()
|
|
|
|
val allowedCharacters = ZecString.allowedCharacters(monetarySeparators)
|
|
|
|
|
|
|
|
var amountZecString by rememberSaveable {
|
|
|
|
mutableStateOf(previousZecSend?.amount?.toZecString() ?: "")
|
|
|
|
}
|
|
|
|
var recipientAddressString by rememberSaveable {
|
|
|
|
mutableStateOf(previousZecSend?.destination?.address ?: "")
|
|
|
|
}
|
|
|
|
var memoString by rememberSaveable { mutableStateOf(previousZecSend?.memo?.value ?: "") }
|
|
|
|
|
2022-04-08 05:33:08 -07:00
|
|
|
var validation by rememberSaveable {
|
|
|
|
mutableStateOf<Set<ZecSendExt.ZecSendValidation.Invalid.ValidationError>>(emptySet())
|
|
|
|
}
|
2022-03-01 05:11:23 -08:00
|
|
|
|
2022-06-09 09:31:44 -07:00
|
|
|
Column(
|
|
|
|
Modifier
|
|
|
|
.fillMaxHeight()
|
|
|
|
.padding(top = paddingValues.calculateTopPadding())
|
|
|
|
) {
|
2022-03-01 05:11:23 -08:00
|
|
|
Row(Modifier.fillMaxWidth()) {
|
|
|
|
Text(text = myBalance.toZecString())
|
|
|
|
}
|
|
|
|
|
|
|
|
TextField(
|
|
|
|
value = amountZecString,
|
|
|
|
onValueChange = { newValue ->
|
2022-04-01 07:28:16 -07:00
|
|
|
if (!ZecStringExt.filterContinuous(context, monetarySeparators, newValue)) {
|
|
|
|
return@TextField
|
|
|
|
}
|
2022-03-01 05:11:23 -08:00
|
|
|
amountZecString = newValue.filter { allowedCharacters.contains(it) }
|
|
|
|
},
|
|
|
|
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
|
|
|
|
label = { Text(stringResource(id = R.string.send_amount)) }
|
|
|
|
)
|
|
|
|
|
|
|
|
Spacer(Modifier.size(8.dp))
|
|
|
|
|
|
|
|
TextField(
|
|
|
|
value = recipientAddressString,
|
|
|
|
onValueChange = { recipientAddressString = it },
|
|
|
|
label = { Text(stringResource(id = R.string.send_to)) }
|
|
|
|
)
|
|
|
|
|
|
|
|
Spacer(Modifier.size(8.dp))
|
|
|
|
|
|
|
|
TextField(value = memoString, onValueChange = {
|
|
|
|
if (Memo.isWithinMaxLength(it)) {
|
|
|
|
memoString = it
|
|
|
|
}
|
|
|
|
}, label = { Text(stringResource(id = R.string.send_memo)) })
|
|
|
|
|
|
|
|
Spacer(Modifier.fillMaxHeight(MINIMAL_WEIGHT))
|
|
|
|
|
|
|
|
if (validation.isNotEmpty()) {
|
|
|
|
/*
|
|
|
|
* Note: this is not localized in that it uses the enum constant name and joins the string
|
|
|
|
* without regard for RTL. This will get resolved once we do proper validation for
|
|
|
|
* the fields.
|
|
|
|
*/
|
|
|
|
Text(validation.joinToString(", "))
|
|
|
|
}
|
|
|
|
|
|
|
|
PrimaryButton(
|
|
|
|
onClick = {
|
2022-04-08 05:33:08 -07:00
|
|
|
val zecSendValidation = ZecSendExt.new(
|
|
|
|
context,
|
2022-03-01 05:11:23 -08:00
|
|
|
recipientAddressString,
|
|
|
|
amountZecString,
|
|
|
|
memoString,
|
|
|
|
monetarySeparators
|
|
|
|
)
|
|
|
|
|
|
|
|
when (zecSendValidation) {
|
2022-04-08 05:33:08 -07:00
|
|
|
is ZecSendExt.ZecSendValidation.Valid -> onCreateAndSend(zecSendValidation.zecSend)
|
|
|
|
is ZecSendExt.ZecSendValidation.Invalid -> validation = zecSendValidation.validationErrors
|
2022-03-01 05:11:23 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
text = stringResource(id = R.string.send_create),
|
|
|
|
// Check for ABBREVIATION_INDEX goes away once proper address validation is in place.
|
|
|
|
// For now, it just prevents a crash on the confirmation screen.
|
|
|
|
enabled = amountZecString.isNotBlank() && recipientAddressString.length > ABBREVIATION_INDEX
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Composable
|
2022-06-09 09:31:44 -07:00
|
|
|
private fun Confirmation(
|
|
|
|
paddingValues: PaddingValues,
|
|
|
|
zecSend: ZecSend,
|
|
|
|
onConfirmation: () -> Unit
|
|
|
|
) {
|
|
|
|
Column(
|
|
|
|
Modifier
|
|
|
|
.padding(top = paddingValues.calculateTopPadding())
|
|
|
|
) {
|
2022-03-01 05:11:23 -08:00
|
|
|
Text(
|
|
|
|
stringResource(
|
|
|
|
R.string.send_amount_and_address_format,
|
|
|
|
zecSend.amount.toZecString(),
|
|
|
|
zecSend.destination.abbreviated()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO [#249]: Implement press-and-hold
|
|
|
|
Button(onClick = onConfirmation) {
|
|
|
|
Text(text = stringResource(id = R.string.send_confirm))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|