fix add new poll option button onclick,

prefix all poll composables with 'New',
move all poll composables to actions folder,
move jsonToPollOptions parser to PollNoteEvent
This commit is contained in:
toadlyBroodle
2023-03-20 10:20:39 +09:00
parent 3cdfdfa8e8
commit bd37e4a9df
9 changed files with 30 additions and 35 deletions

View File

@@ -3,6 +3,7 @@ package com.vitorpamplona.amethyst.service.model
import com.vitorpamplona.amethyst.model.HexKey import com.vitorpamplona.amethyst.model.HexKey
import com.vitorpamplona.amethyst.model.toHexKey import com.vitorpamplona.amethyst.model.toHexKey
import nostr.postr.Utils import nostr.postr.Utils
import org.json.JSONObject
import java.util.Date import java.util.Date
const val POLL_OPTIONS = "poll_options" const val POLL_OPTIONS = "poll_options"
@@ -73,8 +74,13 @@ class PollNoteEvent(
return PollNoteEvent(id.toHexKey(), pubKey, createdAt, tags, msg, sig.toHexKey()) return PollNoteEvent(id.toHexKey(), pubKey, createdAt, tags, msg, sig.toHexKey())
} }
fun parseJsonPollOptions(s: String): Map<Int, String> { fun jsonToPollOptions(jsonString: String): Map<Int, String> {
return gson.fromJson<Map<Int, String>>(s, MutableMap::class.java) val jsonMap = mutableMapOf<Int, String>()
val jsonObject = JSONObject(jsonString)
jsonObject.keys().forEach {
jsonMap[it.toString().toInt()] = jsonObject.getString(it)
}
return jsonMap
} }
} }
} }

View File

@@ -24,7 +24,7 @@ import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.ui.actions.NewPollViewModel import com.vitorpamplona.amethyst.ui.actions.NewPollViewModel
@Composable @Composable
fun PollClosing(pollViewModel: NewPollViewModel) { fun NewPollClosing(pollViewModel: NewPollViewModel) {
var text by rememberSaveable { mutableStateOf("") } var text by rememberSaveable { mutableStateOf("") }
pollViewModel.isValidClosedAt.value = true pollViewModel.isValidClosedAt.value = true
@@ -74,6 +74,6 @@ fun PollClosing(pollViewModel: NewPollViewModel) {
@Preview @Preview
@Composable @Composable
fun PollClosingPreview() { fun NewPollClosingPreview() {
PollClosing(NewPollViewModel()) NewPollClosing(NewPollViewModel())
} }

View File

@@ -24,7 +24,7 @@ import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.ui.actions.NewPollViewModel import com.vitorpamplona.amethyst.ui.actions.NewPollViewModel
@Composable @Composable
fun PollConsensusThreshold(pollViewModel: NewPollViewModel) { fun NewPollConsensusThreshold(pollViewModel: NewPollViewModel) {
var text by rememberSaveable { mutableStateOf("") } var text by rememberSaveable { mutableStateOf("") }
pollViewModel.isValidConsensusThreshold.value = true pollViewModel.isValidConsensusThreshold.value = true
@@ -74,6 +74,6 @@ fun PollConsensusThreshold(pollViewModel: NewPollViewModel) {
@Preview @Preview
@Composable @Composable
fun PollConsensusThresholdPreview() { fun NewPollConsensusThresholdPreview() {
PollConsensusThreshold(NewPollViewModel()) NewPollConsensusThreshold(NewPollViewModel())
} }

View File

@@ -1,4 +1,4 @@
package com.vitorpamplona.amethyst.ui.components package com.vitorpamplona.amethyst.ui.actions
import androidx.compose.foundation.BorderStroke import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image import androidx.compose.foundation.Image
@@ -12,10 +12,9 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.ui.actions.NewPollViewModel
@Composable @Composable
fun PollOption(pollViewModel: NewPollViewModel, optionIndex: Int) { fun NewPollOption(pollViewModel: NewPollViewModel, optionIndex: Int) {
val colorInValid = TextFieldDefaults.outlinedTextFieldColors( val colorInValid = TextFieldDefaults.outlinedTextFieldColors(
focusedBorderColor = MaterialTheme.colors.error, focusedBorderColor = MaterialTheme.colors.error,
unfocusedBorderColor = Color.Red unfocusedBorderColor = Color.Red
@@ -68,6 +67,6 @@ fun PollOption(pollViewModel: NewPollViewModel, optionIndex: Int) {
@Preview @Preview
@Composable @Composable
fun PollOptionPreview() { fun NewPollOptionPreview() {
PollOption(NewPollViewModel(), 0) NewPollOption(NewPollViewModel(), 0)
} }

View File

@@ -22,7 +22,7 @@ import com.vitorpamplona.amethyst.ui.actions.UrlUserTagTransformation
@OptIn(ExperimentalComposeUiApi::class) @OptIn(ExperimentalComposeUiApi::class)
@Composable @Composable
fun PollPrimaryDescription(pollViewModel: NewPollViewModel) { fun NewPollPrimaryDescription(pollViewModel: NewPollViewModel) {
// initialize focus reference to be able to request focus programmatically // initialize focus reference to be able to request focus programmatically
val focusRequester = remember { FocusRequester() } val focusRequester = remember { FocusRequester() }
val keyboardController = LocalSoftwareKeyboardController.current val keyboardController = LocalSoftwareKeyboardController.current

View File

@@ -12,7 +12,7 @@ import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.ui.actions.NewPollViewModel import com.vitorpamplona.amethyst.ui.actions.NewPollViewModel
@Composable @Composable
fun PollRecipientsField(pollViewModel: NewPollViewModel, account: Account) { fun NewPollRecipientsField(pollViewModel: NewPollViewModel, account: Account) {
// if no recipients, add user's pubkey // if no recipients, add user's pubkey
if (pollViewModel.zapRecipients.isEmpty()) { if (pollViewModel.zapRecipients.isEmpty()) {
pollViewModel.zapRecipients.add(account.userProfile().pubkeyHex) pollViewModel.zapRecipients.add(account.userProfile().pubkeyHex)

View File

@@ -114,13 +114,13 @@ fun NewPollView(onClose: () -> Unit, baseReplyTo: Note? = null, quote: Note? = n
} }
Text(stringResource(R.string.poll_heading_required)) Text(stringResource(R.string.poll_heading_required))
PollRecipientsField(pollViewModel, account) NewPollRecipientsField(pollViewModel, account)
PollPrimaryDescription(pollViewModel) NewPollPrimaryDescription(pollViewModel)
pollViewModel.pollOptions.values.forEachIndexed { index, element -> pollViewModel.pollOptions.values.forEachIndexed { index, element ->
PollOption(pollViewModel, index) NewPollOption(pollViewModel, index)
} }
Button( Button(
onClick = { pollViewModel.pollOptions.values.add("") }, onClick = { pollViewModel.pollOptions[pollViewModel.pollOptions.size] = "" },
border = BorderStroke(1.dp, MaterialTheme.colors.onSurface.copy(alpha = 0.32f)), border = BorderStroke(1.dp, MaterialTheme.colors.onSurface.copy(alpha = 0.32f)),
colors = ButtonDefaults.outlinedButtonColors( colors = ButtonDefaults.outlinedButtonColors(
contentColor = MaterialTheme.colors.onSurface.copy(alpha = 0.32f) contentColor = MaterialTheme.colors.onSurface.copy(alpha = 0.32f)
@@ -133,9 +133,9 @@ fun NewPollView(onClose: () -> Unit, baseReplyTo: Note? = null, quote: Note? = n
) )
} }
Text(stringResource(R.string.poll_heading_optional)) Text(stringResource(R.string.poll_heading_optional))
PollVoteValueRange(pollViewModel) NewPollVoteValueRange(pollViewModel)
PollConsensusThreshold(pollViewModel) NewPollConsensusThreshold(pollViewModel)
PollClosing(pollViewModel) NewPollClosing(pollViewModel)
} }
} }

View File

@@ -6,7 +6,6 @@ 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
import org.json.JSONObject
class NewPollViewModel : NewPostViewModel() { class NewPollViewModel : NewPostViewModel() {
@@ -135,12 +134,3 @@ class NewPollViewModel : NewPostViewModel() {
closedAt = null closedAt = null
} }
} }
fun jsonToPollOptions(jsonString: String): Map<Int, String> {
val jsonMap = mutableMapOf<Int, String>()
val jsonObject = JSONObject(jsonString)
jsonObject.keys().forEach {
jsonMap[it.toString().toInt()] = jsonObject.getString(it)
}
return jsonMap
}

View File

@@ -24,7 +24,7 @@ import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.ui.actions.NewPollViewModel import com.vitorpamplona.amethyst.ui.actions.NewPollViewModel
@Composable @Composable
fun PollVoteValueRange(pollViewModel: NewPollViewModel) { fun NewPollVoteValueRange(pollViewModel: NewPollViewModel) {
var textMax by rememberSaveable { mutableStateOf("") } var textMax by rememberSaveable { mutableStateOf("") }
var textMin by rememberSaveable { mutableStateOf("") } var textMin by rememberSaveable { mutableStateOf("") }
@@ -122,6 +122,6 @@ fun PollVoteValueRange(pollViewModel: NewPollViewModel) {
@Preview @Preview
@Composable @Composable
fun PollVoteValueRangePreview() { fun NewPollVoteValueRangePreview() {
PollVoteValueRange(NewPollViewModel()) NewPollVoteValueRange(NewPollViewModel())
} }