From fcb91ddb52e426f43408b29be4e2853db339c043 Mon Sep 17 00:00:00 2001 From: Erdem Yerebasmaz Date: Mon, 11 Nov 2024 18:51:29 +0300 Subject: [PATCH] Add QR scan to app bar to handle LNURL withdraw requests closes #78 --- .../receive_payment/receive_payment_page.dart | 63 ++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/lib/routes/receive_payment/receive_payment_page.dart b/lib/routes/receive_payment/receive_payment_page.dart index 1d3bc8b1..990c928e 100644 --- a/lib/routes/receive_payment/receive_payment_page.dart +++ b/lib/routes/receive_payment/receive_payment_page.dart @@ -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"; @@ -24,10 +29,29 @@ class _ReceivePaymentPageState extends State { @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), @@ -40,7 +64,7 @@ class _ReceivePaymentPageState extends State { 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: @@ -49,4 +73,41 @@ class _ReceivePaymentPageState extends State { return texts.invoice_lightning_title; } } + + void _scanBarcode() { + final texts = context.texts(); + + Focus.maybeOf(context)?.unfocus(); + Navigator.pushNamed(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 _validateInput(String barcode) async { + final texts = context.texts(); + final inputCubit = context.read(); + + 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); + } + } + } }