Making Toast work to display error messages when Zaps fail.

This commit is contained in:
Vitor Pamplona
2023-02-25 19:33:04 -05:00
parent bdf2896df9
commit 81f9edf00c
2 changed files with 20 additions and 22 deletions
@@ -57,12 +57,15 @@ class LightningAddressResolver {
client.newCall(request).enqueue(object : Callback { client.newCall(request).enqueue(object : Callback {
override fun onResponse(call: Call, response: Response) { override fun onResponse(call: Call, response: Response) {
response.use { 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) { 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() e.printStackTrace()
} }
}) })
@@ -93,12 +96,15 @@ class LightningAddressResolver {
client.newCall(request).enqueue(object : Callback { client.newCall(request).enqueue(object : Callback {
override fun onResponse(call: Call, response: Response) { override fun onResponse(call: Call, response: Response) {
response.use { 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) { 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() e.printStackTrace()
} }
}) })
@@ -125,6 +131,7 @@ class LightningAddressResolver {
onError("Error Parsing JSON from Lightning Address. Check the user's lightning setup") onError("Error Parsing JSON from Lightning Address. Check the user's lightning setup")
null null
} }
val callback = lnurlp?.get("callback")?.asText() val callback = lnurlp?.get("callback")?.asText()
if (callback == null) { if (callback == null) {
@@ -145,7 +152,7 @@ class LightningAddressResolver {
lnInvoice?.get("pr")?.asText()?.let { pr -> lnInvoice?.get("pr")?.asText()?.let { pr ->
onSuccess(pr) onSuccess(pr)
} } ?: onError("Invoice Not Created (element pr not found in the resulting JSON)")
}, },
onError = onError onError = onError
) )
@@ -372,6 +372,11 @@ fun ZapReaction(
onChangeAmount = { onChangeAmount = {
wantsToZap = false wantsToZap = false
wantsToChangeZapAmount = true 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) @OptIn(ExperimentalLayoutApi::class)
@Composable @Composable
private fun BoostTypeChoicePopup(baseNote: Note, accountViewModel: AccountViewModel, onDismiss: () -> Unit, onQuote: () -> Unit) { 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( Popup(
alignment = Alignment.BottomCenter, alignment = Alignment.BottomCenter,
offset = IntOffset(0, -50), offset = IntOffset(0, -50),
@@ -482,9 +482,8 @@ private fun BoostTypeChoicePopup(baseNote: Note, accountViewModel: AccountViewMo
@OptIn(ExperimentalFoundationApi::class, ExperimentalLayoutApi::class) @OptIn(ExperimentalFoundationApi::class, ExperimentalLayoutApi::class)
@Composable @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 context = LocalContext.current
val scope = rememberCoroutineScope()
val accountState by accountViewModel.accountLiveData.observeAsState() val accountState by accountViewModel.accountLiveData.observeAsState()
val account = accountState?.account ?: return val account = accountState?.account ?: return
@@ -501,11 +500,7 @@ fun ZapAmountChoicePopup(baseNote: Note, accountViewModel: AccountViewModel, onD
Button( Button(
modifier = Modifier.padding(horizontal = 3.dp), modifier = Modifier.padding(horizontal = 3.dp),
onClick = { onClick = {
accountViewModel.zap(baseNote, amountInSats * 1000, "", context) { accountViewModel.zap(baseNote, amountInSats * 1000, "", context, onError)
scope.launch {
Toast.makeText(context, it, Toast.LENGTH_SHORT).show()
}
}
onDismiss() onDismiss()
}, },
shape = RoundedCornerShape(20.dp), shape = RoundedCornerShape(20.dp),
@@ -519,11 +514,7 @@ fun ZapAmountChoicePopup(baseNote: Note, accountViewModel: AccountViewModel, onD
textAlign = TextAlign.Center, textAlign = TextAlign.Center,
modifier = Modifier.combinedClickable( modifier = Modifier.combinedClickable(
onClick = { onClick = {
accountViewModel.zap(baseNote, amountInSats * 1000, "", context) { accountViewModel.zap(baseNote, amountInSats * 1000, "", context, onError)
scope.launch {
Toast.makeText(context, it, Toast.LENGTH_SHORT).show()
}
}
onDismiss() onDismiss()
}, },
onLongClick = { onLongClick = {