mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-21 20:41:56 +02:00
change pollOptions to type Map<Int, String>,
remove always true condition from Account.isAcceptable()
This commit is contained in:
@@ -4,21 +4,10 @@ import android.content.res.Resources
|
|||||||
import androidx.core.os.ConfigurationCompat
|
import androidx.core.os.ConfigurationCompat
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import com.vitorpamplona.amethyst.service.model.*
|
import com.vitorpamplona.amethyst.service.model.*
|
||||||
import com.vitorpamplona.amethyst.service.relays.Client
|
import com.vitorpamplona.amethyst.service.relays.*
|
||||||
import com.vitorpamplona.amethyst.service.relays.Constants
|
import kotlinx.coroutines.*
|
||||||
import com.vitorpamplona.amethyst.service.relays.FeedType
|
|
||||||
import com.vitorpamplona.amethyst.service.relays.Relay
|
|
||||||
import com.vitorpamplona.amethyst.service.relays.RelayPool
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.GlobalScope
|
|
||||||
import kotlinx.coroutines.Job
|
|
||||||
import kotlinx.coroutines.NonCancellable
|
|
||||||
import kotlinx.coroutines.delay
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
import nostr.postr.Persona
|
import nostr.postr.Persona
|
||||||
import java.util.Locale
|
import java.util.*
|
||||||
import java.util.concurrent.atomic.AtomicBoolean
|
import java.util.concurrent.atomic.AtomicBoolean
|
||||||
|
|
||||||
val DefaultChannels = setOf(
|
val DefaultChannels = setOf(
|
||||||
@@ -291,7 +280,7 @@ class Account(
|
|||||||
message: String,
|
message: String,
|
||||||
replyTo: List<Note>?,
|
replyTo: List<Note>?,
|
||||||
mentions: List<User>?,
|
mentions: List<User>?,
|
||||||
pollOptions: List<Map<Int, String>>,
|
pollOptions: Map<Int, String>,
|
||||||
valueMaximum: Int?,
|
valueMaximum: Int?,
|
||||||
valueMinimum: Int?,
|
valueMinimum: Int?,
|
||||||
consensusThreshold: Int?,
|
consensusThreshold: Int?,
|
||||||
@@ -535,7 +524,7 @@ class Account(
|
|||||||
isAcceptableDirect(note) &&
|
isAcceptableDirect(note) &&
|
||||||
(
|
(
|
||||||
note.event !is RepostEvent ||
|
note.event !is RepostEvent ||
|
||||||
(note.event is RepostEvent && note.replyTo?.firstOrNull { isAcceptableDirect(it) } != null)
|
(note.replyTo?.firstOrNull { isAcceptableDirect(it) } != null)
|
||||||
) // is not a reaction about a blocked post
|
) // is not a reaction about a blocked post
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -46,7 +46,7 @@ class PollNoteEvent(
|
|||||||
addresses: List<ATag>?,
|
addresses: List<ATag>?,
|
||||||
privateKey: ByteArray,
|
privateKey: ByteArray,
|
||||||
createdAt: Long = Date().time / 1000,
|
createdAt: Long = Date().time / 1000,
|
||||||
pollOptions: List<Map<Int, String>>,
|
pollOptions: Map<Int, String>,
|
||||||
valueMaximum: Int?,
|
valueMaximum: Int?,
|
||||||
valueMinimum: Int?,
|
valueMinimum: Int?,
|
||||||
consensusThreshold: Int?,
|
consensusThreshold: Int?,
|
||||||
@@ -63,7 +63,7 @@ class PollNoteEvent(
|
|||||||
addresses?.forEach {
|
addresses?.forEach {
|
||||||
tags.add(listOf("a", it.toTag()))
|
tags.add(listOf("a", it.toTag()))
|
||||||
}
|
}
|
||||||
tags.add(listOf(POLL_OPTIONS, pollOptions.toString()))
|
tags.add(listOf(POLL_OPTIONS, gson.toJson(pollOptions)))
|
||||||
tags.add(listOf(VALUE_MAXIMUM, valueMaximum.toString()))
|
tags.add(listOf(VALUE_MAXIMUM, valueMaximum.toString()))
|
||||||
tags.add(listOf(VALUE_MINIMUM, valueMinimum.toString()))
|
tags.add(listOf(VALUE_MINIMUM, valueMinimum.toString()))
|
||||||
tags.add(listOf(CONSENSUS_THRESHOLD, consensusThreshold.toString()))
|
tags.add(listOf(CONSENSUS_THRESHOLD, consensusThreshold.toString()))
|
||||||
@@ -72,6 +72,10 @@ class PollNoteEvent(
|
|||||||
val sig = Utils.sign(id, privateKey)
|
val sig = Utils.sign(id, privateKey)
|
||||||
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> {
|
||||||
|
return gson.fromJson<Map<Int, String>>(s, MutableMap::class.java)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,10 +88,11 @@ class PollNoteEvent(
|
|||||||
"tags": [
|
"tags": [
|
||||||
["e", <32-bytes hex of the id of the poll event>, <primary poll host relay URL>],
|
["e", <32-bytes hex of the id of the poll event>, <primary poll host relay URL>],
|
||||||
["p", <32-bytes hex of the key>, <primary poll host relay URL>],
|
["p", <32-bytes hex of the key>, <primary poll host relay URL>],
|
||||||
["poll_options",
|
["poll_options", "{
|
||||||
"[[0, 'poll option 0 description string'],
|
\"0\": \"poll option 0 description string\",
|
||||||
[1, 'poll option 1 description string'],
|
\"1\": \"poll option 1 description string\",
|
||||||
[<n>, 'poll option <n> description string']]"
|
\"n\": \"poll option <n> description string\"
|
||||||
|
}"
|
||||||
],
|
],
|
||||||
["value_maximum", "maximum satoshi value for inclusion in tally"],
|
["value_maximum", "maximum satoshi value for inclusion in tally"],
|
||||||
["value_minimum", "minimum satoshi value for inclusion in tally"],
|
["value_minimum", "minimum satoshi value for inclusion in tally"],
|
||||||
|
@@ -88,7 +88,7 @@ fun NewPollView(onClose: () -> Unit, baseReplyTo: Note? = null, quote: Note? = n
|
|||||||
onClose()
|
onClose()
|
||||||
},
|
},
|
||||||
isActive = pollViewModel.message.text.isNotBlank() &&
|
isActive = pollViewModel.message.text.isNotBlank() &&
|
||||||
pollViewModel.pollOptions.all { it.isNotEmpty() } &&
|
pollViewModel.pollOptions.values.all { it.isNotEmpty() } &&
|
||||||
pollViewModel.isValidRecipients.value &&
|
pollViewModel.isValidRecipients.value &&
|
||||||
pollViewModel.isValidvalueMaximum.value &&
|
pollViewModel.isValidvalueMaximum.value &&
|
||||||
pollViewModel.isValidvalueMinimum.value &&
|
pollViewModel.isValidvalueMinimum.value &&
|
||||||
@@ -116,11 +116,11 @@ 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)
|
PollRecipientsField(pollViewModel, account)
|
||||||
PollPrimaryDescription(pollViewModel)
|
PollPrimaryDescription(pollViewModel)
|
||||||
pollViewModel.pollOptions.forEachIndexed { index, element ->
|
pollViewModel.pollOptions.values.forEachIndexed { index, element ->
|
||||||
PollOption(pollViewModel, index)
|
PollOption(pollViewModel, index)
|
||||||
}
|
}
|
||||||
Button(
|
Button(
|
||||||
onClick = { pollViewModel.pollOptions.add("") },
|
onClick = { pollViewModel.pollOptions.values.add("") },
|
||||||
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)
|
||||||
|
@@ -1,15 +1,19 @@
|
|||||||
package com.vitorpamplona.amethyst.ui.actions
|
package com.vitorpamplona.amethyst.ui.actions
|
||||||
|
|
||||||
|
import androidx.annotation.Keep
|
||||||
import androidx.compose.runtime.mutableStateListOf
|
import androidx.compose.runtime.mutableStateListOf
|
||||||
|
import androidx.compose.runtime.mutableStateMapOf
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.ui.text.input.TextFieldValue
|
import androidx.compose.ui.text.input.TextFieldValue
|
||||||
|
import com.google.gson.Gson
|
||||||
|
import com.google.gson.reflect.TypeToken
|
||||||
import com.vitorpamplona.amethyst.model.*
|
import com.vitorpamplona.amethyst.model.*
|
||||||
import com.vitorpamplona.amethyst.service.nip19.Nip19
|
import com.vitorpamplona.amethyst.service.nip19.Nip19
|
||||||
|
|
||||||
class NewPollViewModel : NewPostViewModel() {
|
class NewPollViewModel : NewPostViewModel() {
|
||||||
|
|
||||||
var zapRecipients = mutableStateListOf<HexKey>()
|
var zapRecipients = mutableStateListOf<HexKey>()
|
||||||
var pollOptions = mutableStateListOf("", "")
|
var pollOptions = mutableStateMapOf<Int, String>(Pair(0, ""), Pair(1, ""))
|
||||||
var valueMaximum: Int? = null
|
var valueMaximum: Int? = null
|
||||||
var valueMinimum: Int? = null
|
var valueMinimum: Int? = null
|
||||||
var consensusThreshold: Int? = null
|
var consensusThreshold: Int? = null
|
||||||
@@ -91,7 +95,7 @@ class NewPollViewModel : NewPostViewModel() {
|
|||||||
account?.sendPoll(newMessage, replyTos, mentions)
|
account?.sendPoll(newMessage, replyTos, mentions)
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
account?.sendPoll(newMessage, replyTos, mentions, getPollOptionsList(), valueMaximum, valueMinimum, consensusThreshold, closedAt)
|
account?.sendPoll(newMessage, replyTos, mentions, pollOptions, valueMaximum, valueMinimum, consensusThreshold, closedAt)
|
||||||
|
|
||||||
clearPollStates()
|
clearPollStates()
|
||||||
}
|
}
|
||||||
@@ -126,18 +130,17 @@ class NewPollViewModel : NewPostViewModel() {
|
|||||||
mentions = null
|
mentions = null
|
||||||
|
|
||||||
zapRecipients = mutableStateListOf<HexKey>()
|
zapRecipients = mutableStateListOf<HexKey>()
|
||||||
pollOptions = mutableStateListOf("", "")
|
pollOptions = mutableStateMapOf<Int, String>(Pair(0, ""), Pair(1, ""))
|
||||||
valueMaximum = null
|
valueMaximum = null
|
||||||
valueMinimum = null
|
valueMinimum = null
|
||||||
consensusThreshold = null
|
consensusThreshold = null
|
||||||
closedAt = null
|
closedAt = null
|
||||||
}
|
}
|
||||||
|
}
|
||||||
private fun getPollOptionsList(): List<Map<Int, String>> {
|
|
||||||
val optionsList: MutableList<Map<Int, String>> = mutableListOf()
|
@Keep // Do not obfuscate! Variable names are needed for parsers
|
||||||
pollOptions.forEachIndexed { i, s ->
|
data class PollOptions(var poll_options: List<String>)
|
||||||
optionsList.add(mapOf(Pair(i, s)))
|
fun parseJsonPollOption(json: String): PollOptions {
|
||||||
}
|
val typeToken = object : TypeToken<PollOptions>() {}.type
|
||||||
return optionsList
|
return Gson().fromJson(json, typeToken)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -29,7 +29,7 @@ fun PollOption(pollViewModel: NewPollViewModel, optionIndex: Int) {
|
|||||||
OutlinedTextField(
|
OutlinedTextField(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.weight(1F),
|
.weight(1F),
|
||||||
value = pollViewModel.pollOptions[optionIndex],
|
value = pollViewModel.pollOptions[optionIndex] ?: "",
|
||||||
onValueChange = { pollViewModel.pollOptions[optionIndex] = it },
|
onValueChange = { pollViewModel.pollOptions[optionIndex] = it },
|
||||||
label = {
|
label = {
|
||||||
Text(
|
Text(
|
||||||
@@ -43,14 +43,14 @@ fun PollOption(pollViewModel: NewPollViewModel, optionIndex: Int) {
|
|||||||
color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f)
|
color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f)
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
colors = if (pollViewModel.pollOptions[optionIndex].isNotEmpty()) colorValid else colorInValid
|
colors = if (pollViewModel.pollOptions[optionIndex]?.isNotEmpty() == true) colorValid else colorInValid
|
||||||
)
|
)
|
||||||
if (optionIndex > 1) {
|
if (optionIndex > 1) {
|
||||||
Button(
|
Button(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.padding(start = 6.dp, top = 2.dp)
|
.padding(start = 6.dp, top = 2.dp)
|
||||||
.imePadding(),
|
.imePadding(),
|
||||||
onClick = { pollViewModel.pollOptions.removeAt(optionIndex) },
|
onClick = { pollViewModel.pollOptions.remove(optionIndex) },
|
||||||
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)
|
||||||
|
Reference in New Issue
Block a user