From 81f9edf00cbe1cb0126f6fbc29d6039478fb0b9c Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Sat, 25 Feb 2023 19:33:04 -0500 Subject: [PATCH] Making Toast work to display error messages when Zaps fail. --- .../lnurl/LightningAddressResolver.kt | 17 +++++++++---- .../amethyst/ui/note/ReactionsRow.kt | 25 ++++++------------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/lnurl/LightningAddressResolver.kt b/app/src/main/java/com/vitorpamplona/amethyst/lnurl/LightningAddressResolver.kt index 8ac62041c..791895905 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/lnurl/LightningAddressResolver.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/lnurl/LightningAddressResolver.kt @@ -57,12 +57,15 @@ class LightningAddressResolver { client.newCall(request).enqueue(object : Callback { override fun onResponse(call: Call, response: Response) { response.use { - onSuccess(response.body.string()) + if (it.isSuccessful) + onSuccess(it.body.string()) + else + onError("Could not resolve ${lnaddress}. Error: ${it.code}. Check if the server up and if the lightning address ${lnaddress} is correct") } } override fun onFailure(call: Call, e: java.io.IOException) { - onError("Could not resolve User's LNURL address from ${url}. Check if the server up and if the lightning address ${lnaddress} is correct") + onError("Could not resolve ${url}. Check if the server up and if the lightning address ${lnaddress} is correct") e.printStackTrace() } }) @@ -93,12 +96,15 @@ class LightningAddressResolver { client.newCall(request).enqueue(object : Callback { override fun onResponse(call: Call, response: Response) { response.use { - onSuccess(response.body.string()) + if (it.isSuccessful) + onSuccess(response.body.string()) + else + onError("Could not fetch invoice from $lnCallback") } } override fun onFailure(call: Call, e: java.io.IOException) { - onError("Could not fetch an invoice from ${lnCallback}. Message ${e.message}") + onError("Could not fetch an invoice from $lnCallback. Message ${e.message}") e.printStackTrace() } }) @@ -125,6 +131,7 @@ class LightningAddressResolver { onError("Error Parsing JSON from Lightning Address. Check the user's lightning setup") null } + val callback = lnurlp?.get("callback")?.asText() if (callback == null) { @@ -145,7 +152,7 @@ class LightningAddressResolver { lnInvoice?.get("pr")?.asText()?.let { pr -> onSuccess(pr) - } + } ?: onError("Invoice Not Created (element pr not found in the resulting JSON)") }, onError = onError ) 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 1e24e3202..ec42c1aab 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 @@ -372,6 +372,11 @@ fun ZapReaction( onChangeAmount = { wantsToZap = false wantsToChangeZapAmount = true + }, + onError = { + scope.launch { + Toast.makeText(context, it, Toast.LENGTH_SHORT).show() + } } ) } @@ -439,11 +444,6 @@ private fun ViewCountReaction(baseNote: Note, textModifier: Modifier = Modifier) @OptIn(ExperimentalLayoutApi::class) @Composable private fun BoostTypeChoicePopup(baseNote: Note, accountViewModel: AccountViewModel, onDismiss: () -> Unit, onQuote: () -> Unit) { - val scope = rememberCoroutineScope() - - val accountState by accountViewModel.accountLiveData.observeAsState() - val account = accountState?.account ?: return - Popup( alignment = Alignment.BottomCenter, offset = IntOffset(0, -50), @@ -482,9 +482,8 @@ private fun BoostTypeChoicePopup(baseNote: Note, accountViewModel: AccountViewMo @OptIn(ExperimentalFoundationApi::class, ExperimentalLayoutApi::class) @Composable -fun ZapAmountChoicePopup(baseNote: Note, accountViewModel: AccountViewModel, onDismiss: () -> Unit, onChangeAmount: () -> Unit) { +fun ZapAmountChoicePopup(baseNote: Note, accountViewModel: AccountViewModel, onDismiss: () -> Unit, onChangeAmount: () -> Unit, onError: (text: String) -> Unit) { val context = LocalContext.current - val scope = rememberCoroutineScope() val accountState by accountViewModel.accountLiveData.observeAsState() val account = accountState?.account ?: return @@ -501,11 +500,7 @@ fun ZapAmountChoicePopup(baseNote: Note, accountViewModel: AccountViewModel, onD Button( modifier = Modifier.padding(horizontal = 3.dp), onClick = { - accountViewModel.zap(baseNote, amountInSats * 1000, "", context) { - scope.launch { - Toast.makeText(context, it, Toast.LENGTH_SHORT).show() - } - } + accountViewModel.zap(baseNote, amountInSats * 1000, "", context, onError) onDismiss() }, shape = RoundedCornerShape(20.dp), @@ -519,11 +514,7 @@ fun ZapAmountChoicePopup(baseNote: Note, accountViewModel: AccountViewModel, onD textAlign = TextAlign.Center, modifier = Modifier.combinedClickable( onClick = { - accountViewModel.zap(baseNote, amountInSats * 1000, "", context) { - scope.launch { - Toast.makeText(context, it, Toast.LENGTH_SHORT).show() - } - } + accountViewModel.zap(baseNote, amountInSats * 1000, "", context, onError) onDismiss() }, onLongClick = {