add fields to pollNoteEvent,

add clearState fun to pollViewModel,
add refined PollNoteEvent to Event,
This commit is contained in:
toadlyBroodle 2023-03-18 15:38:58 +09:00
parent 551ed64e98
commit 7a53708ccc
5 changed files with 53 additions and 16 deletions

View File

@ -178,6 +178,7 @@ open class Event(
LnZapRequestEvent.kind -> LnZapRequestEvent(id, pubKey, createdAt, tags, content, sig)
LongTextNoteEvent.kind -> LongTextNoteEvent(id, pubKey, createdAt, tags, content, sig)
MetadataEvent.kind -> MetadataEvent(id, pubKey, createdAt, tags, content, sig)
PollNoteEvent.kind -> PollNoteEvent(id, pubKey, createdAt, tags, content, sig)
PrivateDmEvent.kind -> PrivateDmEvent(id, pubKey, createdAt, tags, content, sig)
ReactionEvent.kind -> ReactionEvent(id, pubKey, createdAt, tags, content, sig)
RecommendRelayEvent.kind -> RecommendRelayEvent(id, pubKey, createdAt, tags, content, sig, lenient)

View File

@ -5,11 +5,18 @@ import com.vitorpamplona.amethyst.model.toHexKey
import nostr.postr.Utils
import java.util.Date
const val POLL_OPTIONS = "poll_options"
const val VALUE_MAXIMUM = "value_maximum"
const val VALUE_MINIMUM = "value_minimum"
const val CONSENSUS_THRESHOLD = "consensus_threshold"
const val CLOSED_AT = "closed_at"
class PollNoteEvent(
id: HexKey,
pubKey: HexKey,
createdAt: Long,
tags: List<List<String>>,
// ots: , TODO implement OTS: https://github.com/opentimestamps/java-opentimestamps
content: String,
sig: HexKey
) : Event(id, pubKey, createdAt, kind, tags, content, sig) {
@ -23,15 +30,28 @@ class PollNoteEvent(
fun replyTos() = tags.filter { it.firstOrNull() == "e" }.mapNotNull { it.getOrNull(1) }
fun pollOptions() = tags.filter { it.firstOrNull() == POLL_OPTIONS }.mapNotNull { it.getOrNull(1) }
fun valueMaximum() = tags.filter { it.firstOrNull() == VALUE_MAXIMUM }.mapNotNull { it.getOrNull(1) }
fun valueMinimum() = tags.filter { it.firstOrNull() == VALUE_MINIMUM }.mapNotNull { it.getOrNull(1) }
fun consensusThreshold() = tags.filter { it.firstOrNull() == CONSENSUS_THRESHOLD }.mapNotNull { it.getOrNull(1) }
fun closedAt() = tags.filter { it.firstOrNull() == CLOSED_AT }.mapNotNull { it.getOrNull(1) }
companion object {
const val kind = 6969
fun create(msg: String,
replyTos: List<String>?,
mentions: List<String>?,
addresses: List<ATag>?,
privateKey: ByteArray,
createdAt: Long = Date().time / 1000): PollNoteEvent {
fun create(
msg: String,
replyTos: List<String>?,
mentions: List<String>?,
addresses: List<ATag>?,
privateKey: ByteArray,
createdAt: Long = Date().time / 1000,
pollOptions: List<Map<Int, String>>,
valueMaximum: Int?,
valueMinimum: Int?,
consensusThreshold: Int?,
closedAt: Int?
): PollNoteEvent {
val pubKey = Utils.pubkeyCreate(privateKey).toHexKey()
val tags = mutableListOf<List<String>>()
replyTos?.forEach {
@ -43,6 +63,13 @@ class PollNoteEvent(
addresses?.forEach {
tags.add(listOf("a", it.toTag()))
}
pollOptions.forEach {
tags.add(listOf(POLL_OPTIONS, it.toString()))
}
tags.add(listOf(VALUE_MAXIMUM, valueMaximum.toString()))
tags.add(listOf(VALUE_MINIMUM, valueMinimum.toString()))
tags.add(listOf(CONSENSUS_THRESHOLD, consensusThreshold.toString()))
tags.add(listOf(CLOSED_AT, closedAt.toString()))
val id = generateId(pubKey, createdAt, kind, tags, msg)
val sig = Utils.sign(id, privateKey)
return PollNoteEvent(id.toHexKey(), pubKey, createdAt, tags, msg, sig.toHexKey())

View File

@ -39,11 +39,6 @@ fun NewPollView(onClose: () -> Unit, baseReplyTo: Note? = null, quote: Note? = n
val scrollState = rememberScrollState()
// if no recipients, add user's pubkey
if (pollViewModel.zapRecipients.isEmpty()) {
pollViewModel.zapRecipients.add(account.userProfile().pubkeyHex)
}
LaunchedEffect(Unit) {
pollViewModel.load(account, baseReplyTo, quote)
delay(100)
@ -114,7 +109,7 @@ fun NewPollView(onClose: () -> Unit, baseReplyTo: Note? = null, quote: Note? = n
}
Text(stringResource(R.string.poll_heading_required))
PollRecipientsField(pollViewModel)
PollRecipientsField(pollViewModel, account)
PollPrimaryDescription(pollViewModel)
pollViewModel.pollOptions.forEachIndexed { index, element ->
PollOption(pollViewModel, index)

View File

@ -39,14 +39,13 @@ class NewPollViewModel : NewPostViewModel() {
override fun sendPost() {
super.sendPost()
// delete existing pollOptions
pollOptions = mutableStateListOf("", "")
clearStates()
}
override fun cancel() {
super.cancel()
pollOptions = mutableStateListOf("", "")
clearStates()
}
override fun findUrlInMessage(): String? {
@ -64,4 +63,14 @@ class NewPollViewModel : NewPostViewModel() {
override fun autocompleteWithUser(item: User) {
super.autocompleteWithUser(item)
}
private fun clearStates() {
// clear states
zapRecipients = mutableStateListOf<HexKey>()
pollOptions = mutableStateListOf("", "")
zapMax = null
zapMin = null
consensus = null
closedAfter = null
}
}

View File

@ -8,10 +8,15 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.ui.actions.NewPollViewModel
@Composable
fun PollRecipientsField(pollViewModel: NewPollViewModel) {
fun PollRecipientsField(pollViewModel: NewPollViewModel, account: Account) {
// if no recipients, add user's pubkey
if (pollViewModel.zapRecipients.isEmpty()) {
pollViewModel.zapRecipients.add(account.userProfile().pubkeyHex)
}
OutlinedTextField(
modifier = Modifier