add validations to draft notes

This commit is contained in:
greenart7c3
2024-03-20 10:25:41 -03:00
parent bc180ae210
commit 220ce75f19
7 changed files with 83 additions and 14 deletions
@@ -846,7 +846,14 @@ class Account(
} }
suspend fun delete(note: Note) { suspend fun delete(note: Note) {
return delete(listOf(note)) if (note.isDraft()) {
note.event?.let {
val drafts = LocalCache.getDrafts(it.id())
return delete(drafts)
}
} else {
return delete(listOf(note))
}
} }
suspend fun delete(notes: List<Note>) { suspend fun delete(notes: List<Note>) {
@@ -898,10 +905,17 @@ class Account(
fun broadcast(note: Note) { fun broadcast(note: Note) {
note.event?.let { note.event?.let {
if (it is WrappedEvent && it.host != null) { if (note.isDraft()) {
it.host?.let { hostEvent -> Client.send(hostEvent) } val drafts = LocalCache.getDrafts(it.id())
drafts.forEach { draftNote ->
broadcast(draftNote)
}
} else { } else {
Client.send(it) if (it is WrappedEvent && it.host != null) {
it.host?.let { hostEvent -> Client.send(hostEvent) }
} else {
Client.send(it)
}
} }
} }
} }
@@ -930,6 +944,7 @@ class Account(
fun timestamp(note: Note) { fun timestamp(note: Note) {
if (!isWriteable()) return if (!isWriteable()) return
if (note.isDraft()) return
val id = note.event?.id() ?: note.idHex val id = note.event?.id() ?: note.idHex
@@ -1942,6 +1957,7 @@ class Account(
isPrivate: Boolean, isPrivate: Boolean,
) { ) {
if (!isWriteable()) return if (!isWriteable()) return
if (note.isDraft()) return
if (note is AddressableNote) { if (note is AddressableNote) {
BookmarkListEvent.addReplaceable( BookmarkListEvent.addReplaceable(
@@ -148,6 +148,14 @@ object LocalCache {
} ?: listOf() } ?: listOf()
} }
fun getDrafts(eventId: String): List<Note> {
return drafts.filter {
it.value.any { it == eventId }
}.keys.mapNotNull {
checkGetOrCreateNote(it)
}
}
fun addDraft( fun addDraft(
key: String, key: String,
value: String, value: String,
@@ -307,7 +307,12 @@ fun RenderZapRaiser(
} }
LinearProgressIndicator( LinearProgressIndicator(
modifier = remember(details) { Modifier.fillMaxWidth().height(if (details) 24.dp else 4.dp) }, modifier =
remember(details) {
Modifier
.fillMaxWidth()
.height(if (details) 24.dp else 4.dp)
},
color = color, color = color,
progress = { zapraiserStatus.progress }, progress = { zapraiserStatus.progress },
) )
@@ -587,6 +592,13 @@ fun ReplyReaction(
IconButton( IconButton(
modifier = iconSizeModifier, modifier = iconSizeModifier,
onClick = { onClick = {
if (baseNote.isDraft()) {
accountViewModel.toast(
R.string.draft_note,
R.string.it_s_not_possible_to_reply_to_a_draft_note,
)
return@IconButton
}
if (accountViewModel.isWriteable()) { if (accountViewModel.isWriteable()) {
onPress() onPress()
} else { } else {
@@ -774,7 +786,8 @@ fun LikeReaction(
Box( Box(
contentAlignment = Center, contentAlignment = Center,
modifier = modifier =
Modifier.size(iconSize) Modifier
.size(iconSize)
.combinedClickable( .combinedClickable(
role = Role.Button, role = Role.Button,
interactionSource = remember { MutableInteractionSource() }, interactionSource = remember { MutableInteractionSource() },
@@ -782,6 +795,7 @@ fun LikeReaction(
onClick = { onClick = {
likeClick( likeClick(
accountViewModel, accountViewModel,
baseNote,
onMultipleChoices = { wantsToReact = true }, onMultipleChoices = { wantsToReact = true },
onWantsToSignReaction = { accountViewModel.reactToOrDelete(baseNote) }, onWantsToSignReaction = { accountViewModel.reactToOrDelete(baseNote) },
) )
@@ -884,9 +898,17 @@ fun ObserveLikeText(
private fun likeClick( private fun likeClick(
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
baseNote: Note,
onMultipleChoices: () -> Unit, onMultipleChoices: () -> Unit,
onWantsToSignReaction: () -> Unit, onWantsToSignReaction: () -> Unit,
) { ) {
if (baseNote.isDraft()) {
accountViewModel.toast(
R.string.draft_note,
R.string.it_s_not_possible_to_react_to_a_draft_note,
)
return
}
if (accountViewModel.account.reactionChoices.isEmpty()) { if (accountViewModel.account.reactionChoices.isEmpty()) {
accountViewModel.toast( accountViewModel.toast(
R.string.no_reactions_setup, R.string.no_reactions_setup,
@@ -1080,6 +1102,14 @@ fun zapClick(
onError: (String, String) -> Unit, onError: (String, String) -> Unit,
onPayViaIntent: (ImmutableList<ZapPaymentHandler.Payable>) -> Unit, onPayViaIntent: (ImmutableList<ZapPaymentHandler.Payable>) -> Unit,
) { ) {
if (baseNote.isDraft()) {
accountViewModel.toast(
R.string.draft_note,
R.string.it_s_not_possible_to_zap_to_a_draft_note,
)
return
}
if (accountViewModel.account.zapAmountChoices.isEmpty()) { if (accountViewModel.account.zapAmountChoices.isEmpty()) {
accountViewModel.toast( accountViewModel.toast(
context.getString(R.string.error_dialog_zap_error), context.getString(R.string.error_dialog_zap_error),
@@ -179,7 +179,7 @@ fun ZapCustomDialog(
) )
ZapButton( ZapButton(
isActive = postViewModel.canSend(), isActive = postViewModel.canSend() && !baseNote.isDraft(),
) { ) {
accountViewModel.zap( accountViewModel.zap(
baseNote, baseNote,
@@ -237,13 +237,15 @@ fun NoteDropDownMenu(
}, },
) )
HorizontalDivider(thickness = DividerThickness) HorizontalDivider(thickness = DividerThickness)
DropdownMenuItem( if (!note.isDraft()) {
text = { Text(stringResource(R.string.edit_draft)) }, DropdownMenuItem(
onClick = { text = { Text(stringResource(R.string.edit_draft)) },
wantsToEditDraft.value = true onClick = {
}, wantsToEditDraft.value = true
) },
if (note.event is TextNoteEvent) { )
}
if (note.event is TextNoteEvent && !note.isDraft()) {
if (state.isLoggedUser) { if (state.isLoggedUser) {
DropdownMenuItem( DropdownMenuItem(
text = { Text(stringResource(R.string.edit_post)) }, text = { Text(stringResource(R.string.edit_post)) },
@@ -1209,6 +1209,14 @@ class AccountViewModel(val account: Account, val settings: SettingsState) : View
baseNote: Note, baseNote: Note,
onMore: () -> Unit, onMore: () -> Unit,
) { ) {
if (baseNote.isDraft()) {
toast(
R.string.draft_note,
R.string.it_s_not_possible_to_quote_to_a_draft_note,
)
return
}
if (isWriteable()) { if (isWriteable()) {
if (hasBoosted(baseNote)) { if (hasBoosted(baseNote)) {
deleteBoostsTo(baseNote) deleteBoostsTo(baseNote)
+5
View File
@@ -821,4 +821,9 @@
<string name="accessibility_play_username">Play username as audio</string> <string name="accessibility_play_username">Play username as audio</string>
<string name="accessibility_scan_qr_code">Scan QR code</string> <string name="accessibility_scan_qr_code">Scan QR code</string>
<string name="accessibility_navigate_to_alby">Navigate to the third-party wallet provider Alby</string> <string name="accessibility_navigate_to_alby">Navigate to the third-party wallet provider Alby</string>
<string name="it_s_not_possible_to_reply_to_a_draft_note">It\'s not possible to reply a draft note</string>
<string name="it_s_not_possible_to_quote_to_a_draft_note">It\'s not possible to quote a draft note</string>
<string name="it_s_not_possible_to_react_to_a_draft_note">It\'s not possible to react a draft note</string>
<string name="it_s_not_possible_to_zap_to_a_draft_note">It\'s not possible to zap a draft note</string>
<string name="draft_note">Draft Note</string>
</resources> </resources>