From b90279e174f49e3300c46fff82fa9699b7ec93f2 Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Sat, 27 Jun 2026 01:14:26 +0000 Subject: [PATCH] chore: improve qr scan result handling (#27) Reviewed-on: https://git.reya.su/reya/coop-mobile/pulls/27 --- .../kotlin/su/reya/coop/screens/ImportScreen.kt | 11 ++++++----- .../kotlin/su/reya/coop/screens/NewChatScreen.kt | 15 +++++++-------- .../kotlin/su/reya/coop/screens/ScanScreen.kt | 15 +++------------ 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/composeApp/src/androidMain/kotlin/su/reya/coop/screens/ImportScreen.kt b/composeApp/src/androidMain/kotlin/su/reya/coop/screens/ImportScreen.kt index 336f727..bb29514 100644 --- a/composeApp/src/androidMain/kotlin/su/reya/coop/screens/ImportScreen.kt +++ b/composeApp/src/androidMain/kotlin/su/reya/coop/screens/ImportScreen.kt @@ -94,17 +94,18 @@ fun ImportScreen() { LaunchedEffect(qrScanResult.content) { qrScanResult.content?.let { result -> runCatching { - if (result.startsWith("nsec")) { + if (result.startsWith("nsec1")) { Keys.parse(result) } else if (result.startsWith("bunker://")) { NostrConnectUri.parse(result) } else { - throw IllegalArgumentException("Invalid secret: $result") + throw IllegalArgumentException("Unsupported secret format") } + }.onSuccess { + secret = result + }.onFailure { e -> + snackbarHostState.showSnackbar("Invalid secret: ${e.message}") } - .onSuccess { it -> secret = result } - .onFailure { e -> println("Failed to parse QR: ${e.message}") } - // Clear the nav state qrScanResult.clear() } diff --git a/composeApp/src/androidMain/kotlin/su/reya/coop/screens/NewChatScreen.kt b/composeApp/src/androidMain/kotlin/su/reya/coop/screens/NewChatScreen.kt index 13426bd..59b1e16 100644 --- a/composeApp/src/androidMain/kotlin/su/reya/coop/screens/NewChatScreen.kt +++ b/composeApp/src/androidMain/kotlin/su/reya/coop/screens/NewChatScreen.kt @@ -109,14 +109,13 @@ fun NewChatScreen() { LaunchedEffect(qrScanResult.content) { qrScanResult.content?.let { result -> // Verify the content - runCatching { PublicKey.parse(result) } - .onSuccess { pubkey -> - selectedReceivers.add(pubkey) - } - .onFailure { e -> - println("Failed to parse QR: ${e.message}") - } - + runCatching { + PublicKey.parse(result) + }.onSuccess { pubkey -> + selectedReceivers.add(pubkey) + }.onFailure { e -> + snackbarHostState.showSnackbar("Invalid address: ${e.message}") + } // Clear the nav state qrScanResult.clear() } diff --git a/composeApp/src/androidMain/kotlin/su/reya/coop/screens/ScanScreen.kt b/composeApp/src/androidMain/kotlin/su/reya/coop/screens/ScanScreen.kt index c785a1d..bac50b3 100644 --- a/composeApp/src/androidMain/kotlin/su/reya/coop/screens/ScanScreen.kt +++ b/composeApp/src/androidMain/kotlin/su/reya/coop/screens/ScanScreen.kt @@ -5,16 +5,15 @@ import androidx.compose.foundation.Canvas import androidx.compose.foundation.border import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.CenterAlignedTopAppBar import androidx.compose.material3.Icon import androidx.compose.material3.IconButton import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Scaffold import androidx.compose.material3.SnackbarHost import androidx.compose.material3.Text -import androidx.compose.material3.TopAppBar import androidx.compose.material3.TopAppBarDefaults import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment @@ -44,10 +43,10 @@ fun ScanScreen() { Scaffold( snackbarHost = { SnackbarHost(snackbarHostState) }, topBar = { - TopAppBar( + CenterAlignedTopAppBar( title = { Text( - text = "Scan QR", + text = "Scan a Nostr Address", style = MaterialTheme.typography.titleMediumEmphasized ) }, @@ -97,14 +96,6 @@ fun ScanScreen() { .align(Alignment.Center) .border(2.dp, Color.White, RoundedCornerShape(12.dp)) ) - Text( - text = "Scan a Nostr address", - style = MaterialTheme.typography.titleSmallEmphasized, - color = Color.White, - modifier = Modifier - .align(Alignment.BottomCenter) - .padding(bottom = 64.dp) - ) } } } \ No newline at end of file