diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPollView.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPollView.kt index c3c768b31..22fa9d3ef 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPollView.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPollView.kt @@ -88,7 +88,12 @@ fun NewPollView(onClose: () -> Unit, baseReplyTo: Note? = null, quote: Note? = n onClose() }, 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 ) } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPollViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPollViewModel.kt index 2259cc83e..5b5b637e9 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPollViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPollViewModel.kt @@ -1,6 +1,7 @@ package com.vitorpamplona.amethyst.ui.actions import androidx.compose.runtime.mutableStateListOf +import androidx.compose.runtime.mutableStateOf import androidx.compose.ui.text.input.TextFieldValue import com.vitorpamplona.amethyst.model.* import com.vitorpamplona.amethyst.service.nip19.Nip19 @@ -14,6 +15,12 @@ class NewPollViewModel : NewPostViewModel() { var consensusThreshold: 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?) { super.load(account, replyingTo, quote) } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/components/PollClosing.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/components/PollClosing.kt index 9d4e45f3f..c35b5faa7 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/components/PollClosing.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/components/PollClosing.kt @@ -27,14 +27,14 @@ import com.vitorpamplona.amethyst.ui.actions.NewPollViewModel fun PollClosing(pollViewModel: NewPollViewModel) { var text by rememberSaveable { mutableStateOf("") } - var isInputValid = true + pollViewModel.isValidClosedAt.value = true if (text.isNotEmpty()) { try { val int = text.toInt() if (int < 0) { - isInputValid = false + pollViewModel.isValidClosedAt.value = false } else { pollViewModel.closedAt = int } - } catch (e: Exception) { isInputValid = false } + } catch (e: Exception) { pollViewModel.isValidClosedAt.value = false } } val colorInValid = TextFieldDefaults.outlinedTextFieldColors( @@ -55,7 +55,7 @@ fun PollClosing(pollViewModel: NewPollViewModel) { onValueChange = { text = it }, keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), modifier = Modifier.width(150.dp), - colors = if (isInputValid) colorValid else colorInValid, + colors = if (pollViewModel.isValidClosedAt.value) colorValid else colorInValid, label = { Text( text = stringResource(R.string.poll_closing_time), diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/components/PollConsensusThreshold.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/components/PollConsensusThreshold.kt index f60a11e77..bb8e3d42a 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/components/PollConsensusThreshold.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/components/PollConsensusThreshold.kt @@ -27,14 +27,14 @@ import com.vitorpamplona.amethyst.ui.actions.NewPollViewModel fun PollConsensusThreshold(pollViewModel: NewPollViewModel) { var text by rememberSaveable { mutableStateOf("") } - var isInputValid = true + pollViewModel.isValidConsensusThreshold.value = true if (text.isNotEmpty()) { try { val int = text.toInt() if (int < 0 || int > 100) { - isInputValid = false + pollViewModel.isValidConsensusThreshold.value = false } else { pollViewModel.consensusThreshold = int } - } catch (e: Exception) { isInputValid = false } + } catch (e: Exception) { pollViewModel.isValidConsensusThreshold.value = false } } val colorInValid = TextFieldDefaults.outlinedTextFieldColors( @@ -55,7 +55,7 @@ fun PollConsensusThreshold(pollViewModel: NewPollViewModel) { onValueChange = { text = it }, keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), modifier = Modifier.width(150.dp), - colors = if (isInputValid) colorValid else colorInValid, + colors = if (pollViewModel.isValidConsensusThreshold.value) colorValid else colorInValid, label = { Text( text = stringResource(R.string.poll_consensus_threshold), diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/components/PollOption.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/components/PollOption.kt index df9533e9a..441c71142 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/components/PollOption.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/components/PollOption.kt @@ -16,11 +16,6 @@ import com.vitorpamplona.amethyst.ui.actions.NewPollViewModel @Composable fun PollOption(pollViewModel: NewPollViewModel, optionIndex: Int) { - var isInputValid = true - if (pollViewModel.pollOptions[optionIndex].isEmpty()) { - isInputValid = false - } - val colorInValid = TextFieldDefaults.outlinedTextFieldColors( focusedBorderColor = MaterialTheme.colors.error, unfocusedBorderColor = Color.Red @@ -48,7 +43,7 @@ fun PollOption(pollViewModel: NewPollViewModel, optionIndex: Int) { 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) { Button( diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/components/PollRecipientsField.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/components/PollRecipientsField.kt index 602a1c1af..4498d2ad6 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/components/PollRecipientsField.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/components/PollRecipientsField.kt @@ -18,6 +18,8 @@ fun PollRecipientsField(pollViewModel: NewPollViewModel, account: Account) { pollViewModel.zapRecipients.add(account.userProfile().pubkeyHex) } + // TODO allow add multiple recipients and check input validity + OutlinedTextField( modifier = Modifier .fillMaxWidth(), diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/components/PollVoteValueRange.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/components/PollVoteValueRange.kt index fa6f93c62..598290bf7 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/components/PollVoteValueRange.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/components/PollVoteValueRange.kt @@ -29,25 +29,25 @@ fun PollVoteValueRange(pollViewModel: NewPollViewModel) { var textMin by rememberSaveable { mutableStateOf("") } // check for zapMax amounts < 1 - var isMaxValid = true + pollViewModel.isValidvalueMaximum.value = true if (textMax.isNotEmpty()) { try { val int = textMax.toInt() if (int < 1) { - isMaxValid = false + pollViewModel.isValidvalueMaximum.value = false } else { pollViewModel.valueMaximum = int } - } catch (e: Exception) { isMaxValid = false } + } catch (e: Exception) { pollViewModel.isValidvalueMaximum.value = false } } // check for minZap amounts < 1 - var isMinValid = true + pollViewModel.isValidvalueMinimum.value = true if (textMin.isNotEmpty()) { try { val int = textMin.toInt() if (int < 1) { - isMinValid = false + pollViewModel.isValidvalueMinimum.value = false } else { pollViewModel.valueMinimum = int } - } catch (e: Exception) { isMinValid = false } + } catch (e: Exception) { pollViewModel.isValidvalueMinimum.value = false } } // check for zapMin > zapMax @@ -57,12 +57,12 @@ fun PollVoteValueRange(pollViewModel: NewPollViewModel) { val intMax = textMax.toInt() if (intMin > intMax) { - isMinValid = false - isMaxValid = false + pollViewModel.isValidvalueMinimum.value = false + pollViewModel.isValidvalueMaximum.value = false } } catch (e: Exception) { - isMinValid = false - isMaxValid = false + pollViewModel.isValidvalueMinimum.value = false + pollViewModel.isValidvalueMaximum.value = false } } @@ -84,7 +84,7 @@ fun PollVoteValueRange(pollViewModel: NewPollViewModel) { onValueChange = { textMin = it }, keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), modifier = Modifier.width(150.dp), - colors = if (isMinValid) colorValid else colorInValid, + colors = if (pollViewModel.isValidvalueMinimum.value) colorValid else colorInValid, label = { Text( text = stringResource(R.string.poll_zap_value_min), @@ -103,7 +103,7 @@ fun PollVoteValueRange(pollViewModel: NewPollViewModel) { onValueChange = { textMax = it }, keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), modifier = Modifier.width(150.dp), - colors = if (isMaxValid) colorValid else colorInValid, + colors = if (pollViewModel.isValidvalueMaximum.value) colorValid else colorInValid, label = { Text( text = stringResource(R.string.poll_zap_value_max),