From 58a4fb18ae38bda82b13dd504fbb32136626fa57 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Sun, 18 Jun 2023 14:40:46 -0400 Subject: [PATCH] BugFixes for NPE --- .../amethyst/ui/actions/NewPostView.kt | 4 +--- .../amethyst/ui/actions/NewPostViewModel.kt | 17 ++++++++++------- .../amethyst/ui/components/ZapRaiserRequest.kt | 16 +++++++++------- .../amethyst/ui/note/ReactionsRow.kt | 2 +- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostView.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostView.kt index ebd1cda35..2a6f43872 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostView.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostView.kt @@ -284,9 +284,7 @@ fun NewPostView(onClose: () -> Unit, baseReplyTo: Note? = null, quote: Note? = n Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.padding(vertical = 5.dp)) { ZapRaiserRequest( stringResource(id = R.string.zapraiser), - onSuccess = { - postViewModel.zapRaiserAmount = it - } + postViewModel ) } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt index b3d9791df..3f216b014 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt @@ -77,7 +77,7 @@ open class NewPostViewModel() : ViewModel() { // ZapRaiser var canAddZapRaiser by mutableStateOf(false) var wantsZapraiser by mutableStateOf(false) - var zapRaiserAmount by mutableStateOf(0L) + var zapRaiserAmount by mutableStateOf(null) open fun load(account: Account, replyingTo: Note?, quote: Note?) { originalNote = replyingTo @@ -117,6 +117,7 @@ open class NewPostViewModel() : ViewModel() { wantsForwardZapTo = false wantsToMarkAsSensitive = false wantsZapraiser = false + zapRaiserAmount = null forwardZapTo = null forwardZapToEditting = TextFieldValue("") @@ -137,12 +138,14 @@ open class NewPostViewModel() : ViewModel() { null } + val localZapRaiserAmount = if (wantsZapraiser) zapRaiserAmount else null + if (wantsPoll) { - account?.sendPoll(tagger.message, tagger.replyTos, tagger.mentions, pollOptions, valueMaximum, valueMinimum, consensusThreshold, closedAt, zapReceiver, wantsToMarkAsSensitive, zapRaiserAmount) + account?.sendPoll(tagger.message, tagger.replyTos, tagger.mentions, pollOptions, valueMaximum, valueMinimum, consensusThreshold, closedAt, zapReceiver, wantsToMarkAsSensitive, localZapRaiserAmount) } else if (originalNote?.channelHex() != null) { - account?.sendChannelMessage(tagger.message, tagger.channelHex!!, tagger.replyTos, tagger.mentions, zapReceiver, wantsToMarkAsSensitive, zapRaiserAmount) + account?.sendChannelMessage(tagger.message, tagger.channelHex!!, tagger.replyTos, tagger.mentions, zapReceiver, wantsToMarkAsSensitive, localZapRaiserAmount) } else if (originalNote?.event is PrivateDmEvent) { - account?.sendPrivateMessage(tagger.message, originalNote!!.author!!, originalNote!!, tagger.mentions, zapReceiver, wantsToMarkAsSensitive, zapRaiserAmount) + account?.sendPrivateMessage(tagger.message, originalNote!!.author!!, originalNote!!, tagger.mentions, zapReceiver, wantsToMarkAsSensitive, localZapRaiserAmount) } else { // adds markers val rootId = @@ -158,7 +161,7 @@ open class NewPostViewModel() : ViewModel() { tags = null, zapReceiver = zapReceiver, wantsToMarkAsSensitive = wantsToMarkAsSensitive, - zapRaiserAmount = zapRaiserAmount, + zapRaiserAmount = localZapRaiserAmount, replyingTo = replyId, root = rootId, directMentions = tagger.directMentions @@ -237,6 +240,7 @@ open class NewPostViewModel() : ViewModel() { wantsInvoice = false wantsZapraiser = false + zapRaiserAmount = null wantsForwardZapTo = false wantsToMarkAsSensitive = false @@ -342,8 +346,7 @@ open class NewPostViewModel() : ViewModel() { } fun canPost(): Boolean { - return message.text.isNotBlank() && !isUploadingImage && !wantsInvoice - (!wantsPoll || pollOptions.values.all { it.isNotEmpty() }) && contentToAddUrl == null + return message.text.isNotBlank() && !isUploadingImage && !wantsInvoice && (!wantsZapraiser || zapRaiserAmount != null) && (!wantsPoll || pollOptions.values.all { it.isNotEmpty() }) && contentToAddUrl == null } fun includePollHashtagInMessage(include: Boolean, hashtag: String) { diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/components/ZapRaiserRequest.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/components/ZapRaiserRequest.kt index 4ba3fde76..8637b52f5 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/components/ZapRaiserRequest.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/components/ZapRaiserRequest.kt @@ -26,12 +26,13 @@ import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.vitorpamplona.amethyst.R +import com.vitorpamplona.amethyst.ui.actions.NewPostViewModel import com.vitorpamplona.amethyst.ui.theme.placeholderText @Composable fun ZapRaiserRequest( titleText: String? = null, - onSuccess: (Long) -> Unit + newPostViewModel: NewPostViewModel ) { Column( modifier = Modifier @@ -70,20 +71,21 @@ fun ZapRaiserRequest( modifier = Modifier.padding(vertical = 10.dp) ) - var amount by remember { mutableStateOf(10000L) } - OutlinedTextField( label = { Text(text = stringResource(R.string.zapraiser_target_amount_in_sats)) }, modifier = Modifier.fillMaxWidth(), - value = amount.toString(), + value = if (newPostViewModel.zapRaiserAmount != null) { + newPostViewModel.zapRaiserAmount.toString() + } else { + "" + }, onValueChange = { runCatching { if (it.isEmpty()) { - amount = 0 + newPostViewModel.zapRaiserAmount = null } else { - amount = it.toLong() + newPostViewModel.zapRaiserAmount = it.toLongOrNull() } - onSuccess(amount) } }, placeholder = { diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReactionsRow.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReactionsRow.kt index 0beaf0844..4c249cf00 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReactionsRow.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReactionsRow.kt @@ -127,7 +127,7 @@ fun ReactionsRow(baseNote: Note, showReactionDetail: Boolean, accountViewModel: } } - if (zapraiserAmount != null) { + if (zapraiserAmount != null && zapraiserAmount > 0) { Spacer(modifier = Modifier.height(4.dp)) Row( verticalAlignment = CenterVertically,