mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-28 12:07:03 +02:00
move valid input flags to pollViewModel,
disable sendPoll button unless all fields valid
This commit is contained in:
@@ -88,7 +88,12 @@ fun NewPollView(onClose: () -> Unit, baseReplyTo: Note? = null, quote: Note? = n
|
|||||||
onClose()
|
onClose()
|
||||||
},
|
},
|
||||||
isActive = pollViewModel.message.text.isNotBlank() &&
|
isActive = pollViewModel.message.text.isNotBlank() &&
|
||||||
!pollViewModel.isUploadingImage
|
pollViewModel.pollOptions.all { it.isNotEmpty() } &&
|
||||||
|
pollViewModel.isValidRecipients.value &&
|
||||||
|
pollViewModel.isValidvalueMaximum.value &&
|
||||||
|
pollViewModel.isValidvalueMinimum.value &&
|
||||||
|
pollViewModel.isValidConsensusThreshold.value &&
|
||||||
|
pollViewModel.isValidClosedAt.value
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
package com.vitorpamplona.amethyst.ui.actions
|
package com.vitorpamplona.amethyst.ui.actions
|
||||||
|
|
||||||
import androidx.compose.runtime.mutableStateListOf
|
import androidx.compose.runtime.mutableStateListOf
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.ui.text.input.TextFieldValue
|
import androidx.compose.ui.text.input.TextFieldValue
|
||||||
import com.vitorpamplona.amethyst.model.*
|
import com.vitorpamplona.amethyst.model.*
|
||||||
import com.vitorpamplona.amethyst.service.nip19.Nip19
|
import com.vitorpamplona.amethyst.service.nip19.Nip19
|
||||||
@@ -14,6 +15,12 @@ class NewPollViewModel : NewPostViewModel() {
|
|||||||
var consensusThreshold: Int? = null
|
var consensusThreshold: Int? = null
|
||||||
var closedAt: Int? = null
|
var closedAt: Int? = null
|
||||||
|
|
||||||
|
var isValidRecipients = mutableStateOf(true)
|
||||||
|
var isValidvalueMaximum = mutableStateOf(true)
|
||||||
|
var isValidvalueMinimum = mutableStateOf(true)
|
||||||
|
var isValidConsensusThreshold = mutableStateOf(true)
|
||||||
|
var isValidClosedAt = mutableStateOf(true)
|
||||||
|
|
||||||
override fun load(account: Account, replyingTo: Note?, quote: Note?) {
|
override fun load(account: Account, replyingTo: Note?, quote: Note?) {
|
||||||
super.load(account, replyingTo, quote)
|
super.load(account, replyingTo, quote)
|
||||||
}
|
}
|
||||||
|
@@ -27,14 +27,14 @@ import com.vitorpamplona.amethyst.ui.actions.NewPollViewModel
|
|||||||
fun PollClosing(pollViewModel: NewPollViewModel) {
|
fun PollClosing(pollViewModel: NewPollViewModel) {
|
||||||
var text by rememberSaveable { mutableStateOf("") }
|
var text by rememberSaveable { mutableStateOf("") }
|
||||||
|
|
||||||
var isInputValid = true
|
pollViewModel.isValidClosedAt.value = true
|
||||||
if (text.isNotEmpty()) {
|
if (text.isNotEmpty()) {
|
||||||
try {
|
try {
|
||||||
val int = text.toInt()
|
val int = text.toInt()
|
||||||
if (int < 0) {
|
if (int < 0) {
|
||||||
isInputValid = false
|
pollViewModel.isValidClosedAt.value = false
|
||||||
} else { pollViewModel.closedAt = int }
|
} else { pollViewModel.closedAt = int }
|
||||||
} catch (e: Exception) { isInputValid = false }
|
} catch (e: Exception) { pollViewModel.isValidClosedAt.value = false }
|
||||||
}
|
}
|
||||||
|
|
||||||
val colorInValid = TextFieldDefaults.outlinedTextFieldColors(
|
val colorInValid = TextFieldDefaults.outlinedTextFieldColors(
|
||||||
@@ -55,7 +55,7 @@ fun PollClosing(pollViewModel: NewPollViewModel) {
|
|||||||
onValueChange = { text = it },
|
onValueChange = { text = it },
|
||||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
|
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
|
||||||
modifier = Modifier.width(150.dp),
|
modifier = Modifier.width(150.dp),
|
||||||
colors = if (isInputValid) colorValid else colorInValid,
|
colors = if (pollViewModel.isValidClosedAt.value) colorValid else colorInValid,
|
||||||
label = {
|
label = {
|
||||||
Text(
|
Text(
|
||||||
text = stringResource(R.string.poll_closing_time),
|
text = stringResource(R.string.poll_closing_time),
|
||||||
|
@@ -27,14 +27,14 @@ import com.vitorpamplona.amethyst.ui.actions.NewPollViewModel
|
|||||||
fun PollConsensusThreshold(pollViewModel: NewPollViewModel) {
|
fun PollConsensusThreshold(pollViewModel: NewPollViewModel) {
|
||||||
var text by rememberSaveable { mutableStateOf("") }
|
var text by rememberSaveable { mutableStateOf("") }
|
||||||
|
|
||||||
var isInputValid = true
|
pollViewModel.isValidConsensusThreshold.value = true
|
||||||
if (text.isNotEmpty()) {
|
if (text.isNotEmpty()) {
|
||||||
try {
|
try {
|
||||||
val int = text.toInt()
|
val int = text.toInt()
|
||||||
if (int < 0 || int > 100) {
|
if (int < 0 || int > 100) {
|
||||||
isInputValid = false
|
pollViewModel.isValidConsensusThreshold.value = false
|
||||||
} else { pollViewModel.consensusThreshold = int }
|
} else { pollViewModel.consensusThreshold = int }
|
||||||
} catch (e: Exception) { isInputValid = false }
|
} catch (e: Exception) { pollViewModel.isValidConsensusThreshold.value = false }
|
||||||
}
|
}
|
||||||
|
|
||||||
val colorInValid = TextFieldDefaults.outlinedTextFieldColors(
|
val colorInValid = TextFieldDefaults.outlinedTextFieldColors(
|
||||||
@@ -55,7 +55,7 @@ fun PollConsensusThreshold(pollViewModel: NewPollViewModel) {
|
|||||||
onValueChange = { text = it },
|
onValueChange = { text = it },
|
||||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
|
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
|
||||||
modifier = Modifier.width(150.dp),
|
modifier = Modifier.width(150.dp),
|
||||||
colors = if (isInputValid) colorValid else colorInValid,
|
colors = if (pollViewModel.isValidConsensusThreshold.value) colorValid else colorInValid,
|
||||||
label = {
|
label = {
|
||||||
Text(
|
Text(
|
||||||
text = stringResource(R.string.poll_consensus_threshold),
|
text = stringResource(R.string.poll_consensus_threshold),
|
||||||
|
@@ -16,11 +16,6 @@ import com.vitorpamplona.amethyst.ui.actions.NewPollViewModel
|
|||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun PollOption(pollViewModel: NewPollViewModel, optionIndex: Int) {
|
fun PollOption(pollViewModel: NewPollViewModel, optionIndex: Int) {
|
||||||
var isInputValid = true
|
|
||||||
if (pollViewModel.pollOptions[optionIndex].isEmpty()) {
|
|
||||||
isInputValid = false
|
|
||||||
}
|
|
||||||
|
|
||||||
val colorInValid = TextFieldDefaults.outlinedTextFieldColors(
|
val colorInValid = TextFieldDefaults.outlinedTextFieldColors(
|
||||||
focusedBorderColor = MaterialTheme.colors.error,
|
focusedBorderColor = MaterialTheme.colors.error,
|
||||||
unfocusedBorderColor = Color.Red
|
unfocusedBorderColor = Color.Red
|
||||||
@@ -48,7 +43,7 @@ fun PollOption(pollViewModel: NewPollViewModel, optionIndex: Int) {
|
|||||||
color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f)
|
color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f)
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
colors = if (isInputValid) colorValid else colorInValid
|
colors = if (pollViewModel.pollOptions[optionIndex].isNotEmpty()) colorValid else colorInValid
|
||||||
)
|
)
|
||||||
if (optionIndex > 1) {
|
if (optionIndex > 1) {
|
||||||
Button(
|
Button(
|
||||||
|
@@ -18,6 +18,8 @@ fun PollRecipientsField(pollViewModel: NewPollViewModel, account: Account) {
|
|||||||
pollViewModel.zapRecipients.add(account.userProfile().pubkeyHex)
|
pollViewModel.zapRecipients.add(account.userProfile().pubkeyHex)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO allow add multiple recipients and check input validity
|
||||||
|
|
||||||
OutlinedTextField(
|
OutlinedTextField(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxWidth(),
|
.fillMaxWidth(),
|
||||||
|
@@ -29,25 +29,25 @@ fun PollVoteValueRange(pollViewModel: NewPollViewModel) {
|
|||||||
var textMin by rememberSaveable { mutableStateOf("") }
|
var textMin by rememberSaveable { mutableStateOf("") }
|
||||||
|
|
||||||
// check for zapMax amounts < 1
|
// check for zapMax amounts < 1
|
||||||
var isMaxValid = true
|
pollViewModel.isValidvalueMaximum.value = true
|
||||||
if (textMax.isNotEmpty()) {
|
if (textMax.isNotEmpty()) {
|
||||||
try {
|
try {
|
||||||
val int = textMax.toInt()
|
val int = textMax.toInt()
|
||||||
if (int < 1) {
|
if (int < 1) {
|
||||||
isMaxValid = false
|
pollViewModel.isValidvalueMaximum.value = false
|
||||||
} else { pollViewModel.valueMaximum = int }
|
} else { pollViewModel.valueMaximum = int }
|
||||||
} catch (e: Exception) { isMaxValid = false }
|
} catch (e: Exception) { pollViewModel.isValidvalueMaximum.value = false }
|
||||||
}
|
}
|
||||||
|
|
||||||
// check for minZap amounts < 1
|
// check for minZap amounts < 1
|
||||||
var isMinValid = true
|
pollViewModel.isValidvalueMinimum.value = true
|
||||||
if (textMin.isNotEmpty()) {
|
if (textMin.isNotEmpty()) {
|
||||||
try {
|
try {
|
||||||
val int = textMin.toInt()
|
val int = textMin.toInt()
|
||||||
if (int < 1) {
|
if (int < 1) {
|
||||||
isMinValid = false
|
pollViewModel.isValidvalueMinimum.value = false
|
||||||
} else { pollViewModel.valueMinimum = int }
|
} else { pollViewModel.valueMinimum = int }
|
||||||
} catch (e: Exception) { isMinValid = false }
|
} catch (e: Exception) { pollViewModel.isValidvalueMinimum.value = false }
|
||||||
}
|
}
|
||||||
|
|
||||||
// check for zapMin > zapMax
|
// check for zapMin > zapMax
|
||||||
@@ -57,12 +57,12 @@ fun PollVoteValueRange(pollViewModel: NewPollViewModel) {
|
|||||||
val intMax = textMax.toInt()
|
val intMax = textMax.toInt()
|
||||||
|
|
||||||
if (intMin > intMax) {
|
if (intMin > intMax) {
|
||||||
isMinValid = false
|
pollViewModel.isValidvalueMinimum.value = false
|
||||||
isMaxValid = false
|
pollViewModel.isValidvalueMaximum.value = false
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
isMinValid = false
|
pollViewModel.isValidvalueMinimum.value = false
|
||||||
isMaxValid = false
|
pollViewModel.isValidvalueMaximum.value = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +84,7 @@ fun PollVoteValueRange(pollViewModel: NewPollViewModel) {
|
|||||||
onValueChange = { textMin = it },
|
onValueChange = { textMin = it },
|
||||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
|
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
|
||||||
modifier = Modifier.width(150.dp),
|
modifier = Modifier.width(150.dp),
|
||||||
colors = if (isMinValid) colorValid else colorInValid,
|
colors = if (pollViewModel.isValidvalueMinimum.value) colorValid else colorInValid,
|
||||||
label = {
|
label = {
|
||||||
Text(
|
Text(
|
||||||
text = stringResource(R.string.poll_zap_value_min),
|
text = stringResource(R.string.poll_zap_value_min),
|
||||||
@@ -103,7 +103,7 @@ fun PollVoteValueRange(pollViewModel: NewPollViewModel) {
|
|||||||
onValueChange = { textMax = it },
|
onValueChange = { textMax = it },
|
||||||
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
|
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
|
||||||
modifier = Modifier.width(150.dp),
|
modifier = Modifier.width(150.dp),
|
||||||
colors = if (isMaxValid) colorValid else colorInValid,
|
colors = if (pollViewModel.isValidvalueMaximum.value) colorValid else colorInValid,
|
||||||
label = {
|
label = {
|
||||||
Text(
|
Text(
|
||||||
text = stringResource(R.string.poll_zap_value_max),
|
text = stringResource(R.string.poll_zap_value_max),
|
||||||
|
Reference in New Issue
Block a user