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.toHexKey
import nostr.postr.Utils
import org.json.JSONObject
import java.util.Date
const val POLL_OPTIONS = "poll_options"
@@ -73,8 +74,13 @@ class PollNoteEvent(
return PollNoteEvent(id.toHexKey(), pubKey, createdAt, tags, msg, sig.toHexKey())
}
fun parseJsonPollOptions(s: String): Map<Int, String> {
return gson.fromJson<Map<Int, String>>(s, MutableMap::class.java)
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
@Composable
fun PollClosing(pollViewModel: NewPollViewModel) {
fun NewPollClosing(pollViewModel: NewPollViewModel) {
var text by rememberSaveable { mutableStateOf("") }
pollViewModel.isValidClosedAt.value = true
@@ -74,6 +74,6 @@ fun PollClosing(pollViewModel: NewPollViewModel) {
@Preview
@Composable
fun PollClosingPreview() {
PollClosing(NewPollViewModel())
fun NewPollClosingPreview() {
NewPollClosing(NewPollViewModel())
}

View File

@@ -24,7 +24,7 @@ import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.ui.actions.NewPollViewModel
@Composable
fun PollConsensusThreshold(pollViewModel: NewPollViewModel) {
fun NewPollConsensusThreshold(pollViewModel: NewPollViewModel) {
var text by rememberSaveable { mutableStateOf("") }
pollViewModel.isValidConsensusThreshold.value = true
@@ -74,6 +74,6 @@ fun PollConsensusThreshold(pollViewModel: NewPollViewModel) {
@Preview
@Composable
fun PollConsensusThresholdPreview() {
PollConsensusThreshold(NewPollViewModel())
fun NewPollConsensusThresholdPreview() {
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.Image
@@ -12,10 +12,9 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.ui.actions.NewPollViewModel
@Composable
fun PollOption(pollViewModel: NewPollViewModel, optionIndex: Int) {
fun NewPollOption(pollViewModel: NewPollViewModel, optionIndex: Int) {
val colorInValid = TextFieldDefaults.outlinedTextFieldColors(
focusedBorderColor = MaterialTheme.colors.error,
unfocusedBorderColor = Color.Red
@@ -68,6 +67,6 @@ fun PollOption(pollViewModel: NewPollViewModel, optionIndex: Int) {
@Preview
@Composable
fun PollOptionPreview() {
PollOption(NewPollViewModel(), 0)
fun NewPollOptionPreview() {
NewPollOption(NewPollViewModel(), 0)
}

View File

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

View File

@@ -12,7 +12,7 @@ import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.ui.actions.NewPollViewModel
@Composable
fun PollRecipientsField(pollViewModel: NewPollViewModel, account: Account) {
fun NewPollRecipientsField(pollViewModel: NewPollViewModel, account: Account) {
// if no recipients, add user's pubkey
if (pollViewModel.zapRecipients.isEmpty()) {
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))
PollRecipientsField(pollViewModel, account)
PollPrimaryDescription(pollViewModel)
NewPollRecipientsField(pollViewModel, account)
NewPollPrimaryDescription(pollViewModel)
pollViewModel.pollOptions.values.forEachIndexed { index, element ->
PollOption(pollViewModel, index)
NewPollOption(pollViewModel, index)
}
Button(
onClick = { pollViewModel.pollOptions.values.add("") },
onClick = { pollViewModel.pollOptions[pollViewModel.pollOptions.size] = "" },
border = BorderStroke(1.dp, MaterialTheme.colors.onSurface.copy(alpha = 0.32f)),
colors = ButtonDefaults.outlinedButtonColors(
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))
PollVoteValueRange(pollViewModel)
PollConsensusThreshold(pollViewModel)
PollClosing(pollViewModel)
NewPollVoteValueRange(pollViewModel)
NewPollConsensusThreshold(pollViewModel)
NewPollClosing(pollViewModel)
}
}

View File

@@ -6,7 +6,6 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.text.input.TextFieldValue
import com.vitorpamplona.amethyst.model.*
import com.vitorpamplona.amethyst.service.nip19.Nip19
import org.json.JSONObject
class NewPollViewModel : NewPostViewModel() {
@@ -135,12 +134,3 @@ class NewPollViewModel : NewPostViewModel() {
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
@Composable
fun PollVoteValueRange(pollViewModel: NewPollViewModel) {
fun NewPollVoteValueRange(pollViewModel: NewPollViewModel) {
var textMax by rememberSaveable { mutableStateOf("") }
var textMin by rememberSaveable { mutableStateOf("") }
@@ -122,6 +122,6 @@ fun PollVoteValueRange(pollViewModel: NewPollViewModel) {
@Preview
@Composable
fun PollVoteValueRangePreview() {
PollVoteValueRange(NewPollViewModel())
fun NewPollVoteValueRangePreview() {
NewPollVoteValueRange(NewPollViewModel())
}