Skip to content

Commit

Permalink
Only use clipboard item for 'Paste Invoice or ID' button
Browse files Browse the repository at this point in the history
  • Loading branch information
erdemyerebasmaz committed Jul 6, 2023
1 parent c529ca7 commit 222c64c
Showing 1 changed file with 72 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ 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';
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");
Expand All @@ -32,20 +32,15 @@ class SendOptionsBottomSheet extends StatelessWidget {
mainAxisSize: MainAxisSize.min,
children: [
const SizedBox(height: 8.0),
BlocBuilder<InputBloc, InputState>(
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,
Expand All @@ -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<void> _pasteFromClipboard(BuildContext context) async {
try {
final inputBloc = context.read<InputBloc>();
// 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<void> _showEnterPaymentInfoDialog(
BuildContext context,
dynamic inputData,
GlobalKey firstPaymentItemKey,
GlobalKey<State<StatefulWidget>> 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) {
Expand Down

0 comments on commit 222c64c

Please sign in to comment.