refactor: extract pairing dialogue from nwc

This commit is contained in:
2026-06-10 16:44:48 +02:00
parent a22e5e4628
commit af59b385e6
2 changed files with 101 additions and 87 deletions
@@ -0,0 +1,101 @@
package com.bitcointxoko.gudariwallet.ui.nwc
import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Intent
import android.view.WindowManager
import android.widget.Toast
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.core.net.toUri
import com.bitcointxoko.gudariwallet.ui.receive.QrDisplayCard
@Composable
internal fun NwcPairingUrlDialog(
url : String,
onDismiss : () -> Unit
) {
// Brightness state lives here so the dialog owns the screen-brightness side-effect
var brightnessOn by remember { mutableStateOf(false) }
val window = (LocalContext.current as? Activity)?.window
val context = LocalContext.current
// Boost / restore screen brightness when the toggle changes
LaunchedEffect(brightnessOn) {
window?.attributes = window.attributes?.apply {
screenBrightness = if (brightnessOn) 1f
else WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
}
}
// Restore brightness unconditionally when the dialog leaves composition
DisposableEffect(Unit) {
onDispose {
window?.attributes = window.attributes?.apply {
screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
}
}
}
AlertDialog(
onDismissRequest = onDismiss,
title = { Text("Connection created") },
text = {
Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {
Text(
text = "Scan or copy this connection string into your app. It will not be shown again.",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
QrDisplayCard(
modifier = Modifier.fillMaxWidth(),
content = url,
contentDescription = "NWC pairing QR code",
clipLabel = "NWC connection string",
shareTitle = "Share NWC connection string",
textToCopy = url,
brightnessOn = brightnessOn,
onToggleBrightness = { brightnessOn = !brightnessOn },
// No NFC for a one-shot pairing string
isNfcEmulating = null,
qrModifier = Modifier.fillMaxWidth(),
onOpenUri = {
val intent = Intent(Intent.ACTION_VIEW, url.toUri())
try {
context.startActivity(
Intent.createChooser(intent, "Open in app")
)
} catch (e: ActivityNotFoundException) {
// No app installed that handles nostr+walletconnect://
// Show a snackbar / toast as appropriate for your app
Toast.makeText(
context,
"No compatible app found", // "No compatible app found"
Toast.LENGTH_SHORT
).show()
}
}
)
}
},
confirmButton = {}, // actions live inside QrDisplayCard
dismissButton = {
TextButton(onClick = onDismiss) { Text("Done") }
}
)
}
@@ -1,10 +1,5 @@
package com.bitcointxoko.gudariwallet.ui.nwc
import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Intent
import android.view.WindowManager
import android.widget.Toast
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
@@ -22,16 +17,13 @@ import androidx.compose.material3.pulltorefresh.PullToRefreshBox
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.bitcointxoko.gudariwallet.api.NwcNewBudget
import com.bitcointxoko.gudariwallet.ui.receive.QrDisplayCard
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale
import androidx.core.net.toUri
// ── Screen ────────────────────────────────────────────────────────────────────
@@ -521,83 +513,4 @@ private fun BudgetRow(
}
}
}
}
// ── Pairing URL dialog ────────────────────────────────────────────────────────
@Composable
private fun NwcPairingUrlDialog(
url : String,
onDismiss : () -> Unit
) {
// Brightness state lives here so the dialog owns the screen-brightness side-effect
var brightnessOn by remember { mutableStateOf(false) }
val window = (LocalContext.current as? Activity)?.window
val context = LocalContext.current
// Boost / restore screen brightness when the toggle changes
LaunchedEffect(brightnessOn) {
window?.attributes = window.attributes?.apply {
screenBrightness = if (brightnessOn) 1f
else WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
}
}
// Restore brightness unconditionally when the dialog leaves composition
DisposableEffect(Unit) {
onDispose {
window?.attributes = window.attributes?.apply {
screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
}
}
}
AlertDialog(
onDismissRequest = onDismiss,
title = { Text("Connection created") },
text = {
Column(verticalArrangement = Arrangement.spacedBy(12.dp)) {
Text(
text = "Scan or copy this connection string into your app. It will not be shown again.",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
QrDisplayCard(
modifier = Modifier.fillMaxWidth(),
content = url,
contentDescription = "NWC pairing QR code",
clipLabel = "NWC connection string",
shareTitle = "Share NWC connection string",
textToCopy = url,
brightnessOn = brightnessOn,
onToggleBrightness = { brightnessOn = !brightnessOn },
// No NFC for a one-shot pairing string
isNfcEmulating = null,
qrModifier = Modifier.fillMaxWidth(),
onOpenUri = {
val intent = Intent(Intent.ACTION_VIEW, url.toUri())
try {
context.startActivity(
Intent.createChooser(intent, "Open in app")
)
} catch (e: ActivityNotFoundException) {
// No app installed that handles nostr+walletconnect://
// Show a snackbar / toast as appropriate for your app
Toast.makeText(
context,
"No compatible app found", // "No compatible app found"
Toast.LENGTH_SHORT
).show()
}
}
)
}
},
confirmButton = {}, // actions live inside QrDisplayCard
dismissButton = {
TextButton(onClick = onDismiss) { Text("Done") }
}
)
}