Add copy button to Cashu Rendering, better default error message

This commit is contained in:
Believethehype
2023-06-28 11:49:14 +02:00
parent 3967633f77
commit 985771beba
2 changed files with 57 additions and 20 deletions

View File

@@ -14,6 +14,7 @@ import java.util.Base64
@Immutable @Immutable
data class CashuToken( data class CashuToken(
val token: String,
val mint: String, val mint: String,
val totalAmount: Long, val totalAmount: Long,
val fees: Int, val fees: Int,
@@ -37,7 +38,7 @@ class CashuProcessor {
val fees = Math.max(((totalAmount * 0.02).toInt()), 2) val fees = Math.max(((totalAmount * 0.02).toInt()), 2)
val redeemInvoiceAmount = totalAmount - fees val redeemInvoiceAmount = totalAmount - fees
return GenericLoadable.Loaded(CashuToken(mint, totalAmount, fees, redeemInvoiceAmount, proofs)) return GenericLoadable.Loaded(CashuToken(cashuToken, mint, totalAmount, fees, redeemInvoiceAmount, proofs))
} catch (e: Exception) { } catch (e: Exception) {
return GenericLoadable.Error<CashuToken>("Could not parse this cashu token") return GenericLoadable.Error<CashuToken>("Could not parse this cashu token")
} }
@@ -84,7 +85,7 @@ class CashuProcessor {
if (successful) { if (successful) {
onSuccess("Redeemed ${token.totalAmount} Sats" + " (Fees: ${token.fees} Sats)") onSuccess("Redeemed ${token.totalAmount} Sats" + " (Fees: ${token.fees} Sats)")
} else { } else {
onError(tree?.get("detail")?.asText()?.split('.')?.getOrNull(0) ?: "Error") onError(tree?.get("detail")?.asText()?.split('.')?.getOrNull(0) ?: "Cashu: Tokens already spent.")
} }
} catch (e: Exception) { } catch (e: Exception) {
onError("Token melt failure: " + e.message) onError("Token melt failure: " + e.message)

View File

@@ -16,9 +16,11 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextDirection import androidx.compose.ui.text.style.TextDirection
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
@@ -67,6 +69,7 @@ fun CashuPreview(token: CashuToken, accountViewModel: AccountViewModel) {
val useWebService = false val useWebService = false
val context = LocalContext.current val context = LocalContext.current
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
val clipboardManager = LocalClipboardManager.current
Column( Column(
modifier = Modifier modifier = Modifier
@@ -114,17 +117,17 @@ fun CashuPreview(token: CashuToken, accountViewModel: AccountViewModel) {
) )
} }
Button( Row(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.padding(vertical = 10.dp), .padding(bottom = 10.dp)
) {
Button(
modifier = Modifier
.padding(vertical = 10.dp).padding(horizontal = 2.dp),
onClick = { onClick = {
// Just in case we want to use a webservice instead of directly contacting the mint
if (useWebService) {
val url = "https://redeem.cashu.me?token=$token&lightning=$lud16&autopay=true"
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(context, intent, null)
} else {
if (lud16 != null) { if (lud16 != null) {
CashuProcessor().melt( CashuProcessor().melt(
token, token,
@@ -142,8 +145,11 @@ fun CashuPreview(token: CashuToken, accountViewModel: AccountViewModel) {
) )
} else { } else {
scope.launch { scope.launch {
Toast.makeText(context, "No Lightning Address set", Toast.LENGTH_SHORT).show() Toast.makeText(
} context,
"No Lightning Address set",
Toast.LENGTH_SHORT
).show()
} }
} }
}, },
@@ -152,7 +158,37 @@ fun CashuPreview(token: CashuToken, accountViewModel: AccountViewModel) {
backgroundColor = MaterialTheme.colors.primary backgroundColor = MaterialTheme.colors.primary
) )
) { ) {
Text(stringResource(R.string.cashu_redeem), color = Color.White, fontSize = 20.sp) Text(
stringResource(R.string.cashu_redeem),
color = Color.White,
fontSize = 18.sp
)
}
Button(
modifier = Modifier
.padding(vertical = 10.dp).padding(horizontal = 1.dp),
onClick = {
if (useWebService) {
// In case we want to use the cashu.me webservice
val url = "https://redeem.cashu.me?token=$token&lightning=$lud16&autopay=false"
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(context, intent, null)
} else {
// Copying the token to clipboard for now
var orignaltoken = token.token
clipboardManager.setText(AnnotatedString("$orignaltoken"))
scope.launch {
Toast.makeText(context, "Copied token to clipboard", Toast.LENGTH_SHORT).show()
}
}
},
shape = QuoteBorder,
colors = ButtonDefaults.buttonColors(
backgroundColor = MaterialTheme.colors.primary
)
) {
Text("", color = Color.White, fontSize = 18.sp)
}
} }
} }
} }