diff --git a/lib/routes/home/widgets/bottom_actions_bar/send_options_bottom_sheet.dart b/lib/routes/home/widgets/bottom_actions_bar/send_options_bottom_sheet.dart index 06aafdbb2..4bd9d5397 100644 --- a/lib/routes/home/widgets/bottom_actions_bar/send_options_bottom_sheet.dart +++ b/lib/routes/home/widgets/bottom_actions_bar/send_options_bottom_sheet.dart @@ -2,7 +2,6 @@ import 'package:breez_translations/breez_translations_locales.dart'; import 'package:c_breez/bloc/account/account_bloc.dart'; import 'package:c_breez/bloc/account/account_state.dart'; import 'package:c_breez/bloc/input/input_bloc.dart'; -import 'package:c_breez/bloc/input/input_state.dart'; import 'package:c_breez/handlers/input_handler.dart'; import 'package:c_breez/routes/home/widgets/bottom_actions_bar/bottom_action_item_image.dart'; import 'package:c_breez/routes/home/widgets/bottom_actions_bar/enter_payment_info_dialog.dart'; @@ -10,6 +9,7 @@ import 'package:c_breez/routes/withdraw_funds/withdraw_funds_address_page.dart'; import 'package:c_breez/theme/theme_provider.dart' as theme; import 'package:fimber/fimber.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; final _log = FimberLog("SendOptionsBottomSheet"); @@ -32,20 +32,15 @@ class SendOptionsBottomSheet extends StatelessWidget { mainAxisSize: MainAxisSize.min, children: [ const SizedBox(height: 8.0), - BlocBuilder( - builder: (context, snapshot) { - _log.v("Building paste item with snapshot: $snapshot"); - return ListTile( - leading: const BottomActionItemImage( - iconAssetPath: "src/icon/paste.png", - ), - title: Text( - texts.bottom_action_bar_paste_invoice, - style: theme.bottomSheetTextStyle, - ), - onTap: () => _pasteTapped(context, snapshot.inputData, firstPaymentItemKey), - ); - }, + ListTile( + leading: const BottomActionItemImage( + iconAssetPath: "src/icon/paste.png", + ), + title: Text( + texts.bottom_action_bar_paste_invoice, + style: theme.bottomSheetTextStyle, + ), + onTap: () => _pasteFromClipboard(context), ), Divider( height: 0.0, @@ -72,32 +67,70 @@ class SendOptionsBottomSheet extends StatelessWidget { ); } - /// This documentation is for clarification purposes. - /// - /// Even though this function is named paste tapped it assumes that the - /// latest inputData on InputBloc's InputState comes from device's clipboard stream. - /// Which is a good assumption because otherwise that inputData would've been handled. - void _pasteTapped( + // TODO: Improve error handling flow to reduce open Enter Payment Info Dialog calls + Future _pasteFromClipboard(BuildContext context) async { + try { + final inputBloc = context.read(); + // Get clipboard data + await Clipboard.getData("text/plain").then( + (clipboardData) async { + final clipboardText = clipboardData?.text; + _log.v("Clipboard text: $clipboardText"); + // Close bottom sheet + Navigator.of(context).pop(); + if (clipboardText != null) { + // Parse clipboard text + await inputBloc.parseInput(input: clipboardText).then( + (parsedInput) { + // Handle parsed input + inputHandler.handleInputData(parsedInput).then( + (result) async { + // If input data can't be handled(unsupported input type) display EnterPaymentInfoDialog + if (result == false) { + _showEnterPaymentInfoDialog(context, firstPaymentItemKey); + } else { + inputHandler.handleResult(result); + } + }, + ).catchError((e) { + // If there's error handling parsed input display EnterPaymentInfoDialog + _showEnterPaymentInfoDialog( + context, + firstPaymentItemKey, + ); + }); + }, + ); + } else { + // If clipboard data is empty, display EnterPaymentInfoDialog + _showEnterPaymentInfoDialog( + context, + firstPaymentItemKey, + ); + } + }, + ); + } catch (e) { + // If there's an error getting the clipboard data, display EnterPaymentInfoDialog + _showEnterPaymentInfoDialog( + context, + firstPaymentItemKey, + ); + } + } + + Future _showEnterPaymentInfoDialog( BuildContext context, - dynamic inputData, - GlobalKey firstPaymentItemKey, + GlobalKey> firstPaymentItemKey, ) async { - // Close bottom sheet - Navigator.of(context).pop(); - // and handle input data - inputHandler.handleInputData(inputData).then((result) async { - // If input data can't be handled(unsupported input type, empty device clipboard) display EnterPaymentInfoDialog - if (result == false) { - await showDialog( - useRootNavigator: false, - context: context, - barrierDismissible: false, - builder: (_) => EnterPaymentInfoDialog(paymentItemKey: firstPaymentItemKey), - ); - } else { - inputHandler.handleResult(result); - } - }); + await showDialog( + useRootNavigator: false, + context: context, + barrierDismissible: false, + builder: (_) => EnterPaymentInfoDialog( + paymentItemKey: firstPaymentItemKey, + ), + ); } void _sendToBTCAddress(BuildContext context, int maxValue) {