Skip to content

Commit

Permalink
Support pasting LNURL #591
Browse files Browse the repository at this point in the history
- Update breez_translations for unsupported input's error message
- Expose parseInput api through InputBloc
- Remove utils/lnurl.dart
  • Loading branch information
erdemyerebasmaz committed Jul 13, 2023
1 parent 5fdef96 commit 746bbc6
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 56 deletions.
4 changes: 3 additions & 1 deletion lib/bloc/input/input_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class InputBloc extends Cubit<InputState> {
// Emit an empty InputState with isLoading to display a loader on UI layer
emit(InputState(isLoading: true));
try {
final parsedInput = await _breezLib.parseInput(input: input);
final parsedInput = await parseInput(input: input);
// Todo: Merge these functions w/o sacrificing readability
_logParsedInput(parsedInput);
return await _handleParsedInput(parsedInput);
Expand Down Expand Up @@ -147,4 +147,6 @@ class InputBloc extends Cubit<InputState> {
_log.i("url: ${parsedInput.url}");
}
}

Future<InputType> parseInput({required String input}) async => await _breezLib.parseInput(input: input);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import 'package:breez_sdk/bridge_generated.dart';
import 'package:breez_translations/breez_translations_locales.dart';
import 'package:c_breez/bloc/input/input_bloc.dart';
import 'package:c_breez/theme/theme_provider.dart' as theme;
import 'package:c_breez/utils/lnurl.dart';
import 'package:c_breez/utils/node_id.dart';
import 'package:c_breez/widgets/flushbar.dart';
import 'package:c_breez/widgets/loader.dart';
import 'package:fimber/fimber.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

final _log = FimberLog("EnterPaymentInfoDialog");

class EnterPaymentInfoDialog extends StatefulWidget {
final GlobalKey paymentItemKey;

Expand All @@ -24,6 +27,9 @@ class EnterPaymentInfoDialogState extends State<EnterPaymentInfoDialog> {
final _paymentInfoFocusNode = FocusNode();

String _scannerErrorMessage = "";
String _validatorErrorMessage = "";

ModalRoute? _loaderRoute;

@override
void initState() {
Expand Down Expand Up @@ -95,12 +101,9 @@ class EnterPaymentInfoDialogState extends State<EnterPaymentInfoDialog> {
style: TextStyle(
color: themeData.primaryTextTheme.headlineMedium!.color,
),
validator: (v) {
var value = v!;
if (parseNodeId(value) == null &&
_decodeInvoice(value) == null &&
!isLightningAddress(value)) {
return texts.payment_info_dialog_error;
validator: (value) {
if (_validatorErrorMessage.isNotEmpty) {
return _validatorErrorMessage;
}
return null;
},
Expand Down Expand Up @@ -151,10 +154,23 @@ class EnterPaymentInfoDialogState extends State<EnterPaymentInfoDialog> {
actions.add(
SimpleDialogOption(
onPressed: (() async {
if (_formKey.currentState!.validate()) {
Navigator.of(context).pop();
final inputBloc = context.read<InputBloc>();
inputBloc.addIncomingInput(_paymentInfoController.text);
final inputBloc = context.read<InputBloc>();
final navigator = Navigator.of(context);
_setLoading(true);

try {
await _validateInput(_paymentInfoController.text);
if (_formKey.currentState!.validate()) {
_setLoading(false);
navigator.pop();
inputBloc.addIncomingInput(_paymentInfoController.text);
}
} catch (error) {
_setLoading(false);
_log.w(error.toString(), ex: error);
_setValidatorErrorMessage(texts.payment_info_dialog_error);
} finally {
_setLoading(false);
}
}),
child: Text(
Expand Down Expand Up @@ -186,17 +202,47 @@ class EnterPaymentInfoDialogState extends State<EnterPaymentInfoDialog> {
setState(() {
_paymentInfoController.text = barcode;
_scannerErrorMessage = "";
_setValidatorErrorMessage("");
});
}

Future<void> _validateInput(String input) async {
final texts = context.texts();
try {
_setValidatorErrorMessage("");
final inputType = await context.read<InputBloc>().parseInput(input: input);
_log.v("Parsed input type: '${inputType.runtimeType.toString()}");
// Can't compare against a list of InputType as runtime type comparison is a bit tricky with binding generated enums
if (!(inputType is InputType_Bolt11 ||
inputType is InputType_LnUrlPay ||
inputType is InputType_LnUrlWithdraw ||
inputType is InputType_LnUrlAuth ||
inputType is InputType_LnUrlError ||
inputType is InputType_NodeId)) {
_setValidatorErrorMessage(texts.payment_info_dialog_error_unsupported_input);
}
} catch (e) {
rethrow;
}
}

_setValidatorErrorMessage(String errorMessage) {
setState(() {
_validatorErrorMessage = errorMessage;
});
_formKey.currentState?.validate();
}

String? _decodeInvoice(String invoiceString) {
String normalized = invoiceString.toLowerCase();
if (normalized.startsWith("lightning:")) {
normalized = normalized.substring(10);
void _setLoading(bool visible) {
if (visible && _loaderRoute == null) {
_loaderRoute = createLoaderRoute(context);
Navigator.of(context).push(_loaderRoute!);
return;
}
if (normalized.startsWith("ln") && !normalized.startsWith("lnurl")) {
return invoiceString;

if (!visible && (_loaderRoute != null && _loaderRoute!.isActive)) {
_loaderRoute!.navigator?.removeRoute(_loaderRoute!);
_loaderRoute = null;
}
return null;
}
}
33 changes: 0 additions & 33 deletions lib/utils/lnurl.dart

This file was deleted.

4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ packages:
dependency: "direct main"
description:
path: "."
ref: "1360928cfe6d7b7c3ed876267dcabb6e6522d58f"
resolved-ref: "1360928cfe6d7b7c3ed876267dcabb6e6522d58f"
ref: a8951fbebdcac5cd63e58ae5e4f711fcd5aed14b
resolved-ref: a8951fbebdcac5cd63e58ae5e4f711fcd5aed14b
url: "https://github.com/breez/Breez-Translations"
source: git
version: "1.0.0"
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dependencies:
breez_translations:
git:
url: https://github.com/breez/Breez-Translations
ref: 1360928cfe6d7b7c3ed876267dcabb6e6522d58f
ref: a8951fbebdcac5cd63e58ae5e4f711fcd5aed14b
clipboard_watcher: ^0.2.0
connectivity_plus: ^4.0.1
drag_and_drop_lists: ^0.3.3
Expand Down

0 comments on commit 746bbc6

Please sign in to comment.