From d257f7e253f8dc87c574e0b4eaa87cf027c9d83f Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Thu, 29 Feb 2024 17:35:44 -0500 Subject: [PATCH] Fixes not being able to create an invoice because of an old cache of the lud16 Fixes inserting invoice in the cursor. --- .../amethyst/ui/actions/NewPostView.kt | 48 +++++++++---------- .../amethyst/ui/actions/NewPostViewModel.kt | 16 +++++++ 2 files changed, 39 insertions(+), 25 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 e8bdad62f..854fa87f8 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 @@ -475,34 +475,32 @@ fun NewPostView( } } - val user = postViewModel.account?.userProfile() - val lud16 = user?.info?.lnAddress() - - if (lud16 != null && postViewModel.wantsInvoice) { - Row( - verticalAlignment = Alignment.CenterVertically, - modifier = Modifier.padding(vertical = Size5dp, horizontal = Size10dp), - ) { - Column(Modifier.fillMaxWidth()) { - InvoiceRequest( - lud16, - user.pubkeyHex, - accountViewModel.account, - stringResource(id = R.string.lightning_invoice), - stringResource(id = R.string.lightning_create_and_add_invoice), - onSuccess = { - postViewModel.message = - TextFieldValue(postViewModel.message.text + "\n\n" + it) - postViewModel.wantsInvoice = false - }, - onClose = { postViewModel.wantsInvoice = false }, - onError = { title, message -> accountViewModel.toast(title, message) }, - ) + if (postViewModel.wantsInvoice) { + postViewModel.lnAddress()?.let { lud16 -> + Row( + verticalAlignment = Alignment.CenterVertically, + modifier = Modifier.padding(vertical = Size5dp, horizontal = Size10dp), + ) { + Column(Modifier.fillMaxWidth()) { + InvoiceRequest( + lud16, + accountViewModel.account.userProfile().pubkeyHex, + accountViewModel.account, + stringResource(id = R.string.lightning_invoice), + stringResource(id = R.string.lightning_create_and_add_invoice), + onSuccess = { + postViewModel.insertAtCursor(it) + postViewModel.wantsInvoice = false + }, + onClose = { postViewModel.wantsInvoice = false }, + onError = { title, message -> accountViewModel.toast(title, message) }, + ) + } } } } - if (lud16 != null && postViewModel.wantsZapraiser) { + if (postViewModel.wantsZapraiser && postViewModel.hasLnAddress()) { Row( verticalAlignment = Alignment.CenterVertically, modifier = Modifier.padding(vertical = Size5dp, horizontal = Size10dp), @@ -569,7 +567,7 @@ private fun BottomRowActions(postViewModel: NewPostViewModel) { } } - if (postViewModel.canAddInvoice) { + if (postViewModel.canAddInvoice && postViewModel.hasLnAddress()) { AddLnInvoiceButton(postViewModel.wantsInvoice) { postViewModel.wantsInvoice = !postViewModel.wantsInvoice } 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 1c7284708..47838ecd5 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 @@ -163,6 +163,18 @@ open class NewPostViewModel() : ViewModel() { // NIP24 Wrapped DMs / Group messages var nip24 by mutableStateOf(false) + fun lnAddress(): String? { + return account?.userProfile()?.info?.lnAddress() + } + + fun hasLnAddress(): Boolean { + return account?.userProfile()?.info?.lnAddress() != null + } + + fun user(): User? { + return account?.userProfile() + } + open fun load( accountViewModel: AccountViewModel, replyingTo: Note?, @@ -819,6 +831,10 @@ open class NewPostViewModel() : ViewModel() { ) } + fun insertAtCursor(newElement: String) { + message = message.insertUrlAtCursor(newElement) + } + fun createNIP95Record( bytes: ByteArray, mimeType: String?,