Skip to content

Commit

Permalink
Add QR scan to app bar to handle LNURL withdraw requests
Browse files Browse the repository at this point in the history
closes #78
  • Loading branch information
erdemyerebasmaz committed Nov 11, 2024
1 parent 587e10a commit fcb91dd
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion lib/routes/receive_payment/receive_payment_page.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import 'package:breez_translations/breez_translations_locales.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_breez_liquid/flutter_breez_liquid.dart';
import 'package:l_breez/cubit/cubit.dart';
import 'package:l_breez/routes/qr_scan/qr_scan.dart';
import 'package:l_breez/routes/receive_payment/lightning/receive_lightning_page.dart';
import 'package:l_breez/routes/receive_payment/ln_address/receive_lightning_address_page.dart';
import 'package:l_breez/routes/receive_payment/onchain/bitcoin_address/receive_bitcoin_address_payment_page.dart';
import 'package:l_breez/widgets/back_button.dart' as back_button;
import 'package:l_breez/widgets/flushbar.dart';

class ReceivePaymentPage extends StatefulWidget {
static const routeName = "/receive_payment";
Expand All @@ -24,10 +29,29 @@ class _ReceivePaymentPageState extends State<ReceivePaymentPage> {

@override
Widget build(BuildContext context) {
final texts = context.texts();
final themeData = Theme.of(context);

return Scaffold(
appBar: AppBar(
leading: const back_button.BackButton(),
title: Text(_getTitle()),
actions: widget.initialPageIndex == ReceiveLightningPaymentPage.pageIndex
? [
IconButton(
alignment: Alignment.center,
icon: Image(
image: const AssetImage("assets/icons/qr_scan.png"),
color: themeData.iconTheme.color,
fit: BoxFit.contain,
width: 24.0,
height: 24.0,
),
tooltip: texts.invoice_action_scan_barcode,
onPressed: () => _scanBarcode(),
)
]
: [],
),
body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 0.0),
Expand All @@ -40,7 +64,7 @@ class _ReceivePaymentPageState extends State<ReceivePaymentPage> {
final texts = context.texts();
switch (widget.initialPageIndex) {
case ReceiveLightningPaymentPage.pageIndex:
return texts.invoice_lightning_title;
return texts.invoice_title;
case ReceiveLightningAddressPage.pageIndex:
return texts.invoice_ln_address_title;
case ReceiveBitcoinAddressPaymentPage.pageIndex:
Expand All @@ -49,4 +73,41 @@ class _ReceivePaymentPageState extends State<ReceivePaymentPage> {
return texts.invoice_lightning_title;
}
}

void _scanBarcode() {
final texts = context.texts();

Focus.maybeOf(context)?.unfocus();
Navigator.pushNamed<String>(context, QRScan.routeName).then((barcode) async {
if (barcode == null || barcode.isEmpty) {
if (context.mounted) showFlushbar(context, message: texts.payment_info_dialog_error_qrcode);
return;
}

await _validateInput(barcode);
});
}

Future<void> _validateInput(String barcode) async {
final texts = context.texts();
final inputCubit = context.read<InputCubit>();

var errMsg = "";
try {
final inputType = await inputCubit.parseInput(input: barcode);
if (inputType is! InputType_LnUrlWithdraw) {
errMsg = texts.payment_info_dialog_error_unsupported_input;
}
if (mounted) {
Navigator.pop(context);
}
inputCubit.addIncomingInput(barcode, InputSource.qrcodeReader);
} catch (error) {
var errStr = error.toString();
errMsg = errStr.contains("Unrecognized") ? texts.payment_info_dialog_error_unsupported_input : errStr;
if (mounted) {
showFlushbar(context, message: errMsg);
}
}
}
}

0 comments on commit fcb91dd

Please sign in to comment.