Skip to content

Commit

Permalink
Handle input on clicking "Paste Invoice or ID"
Browse files Browse the repository at this point in the history
  • Loading branch information
erdemyerebasmaz committed Jul 5, 2023
1 parent 3b32393 commit 54f7de6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 36 deletions.
24 changes: 15 additions & 9 deletions lib/handlers/input_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,7 @@ class InputHandler extends Handler {
_handlingRequest = true;
handleInputData(inputState.inputData)
.then((result) {
_log.v("Input state handled: $result");
if (result is LNURLPageResult && result.protocol != null) {
final context = contextProvider?.getBuildContext();
if (context != null) {
handleLNURLPageResult(context, result);
} else {
_log.v("Skipping handling of result: $result because context is null");
}
}
handleResult(result);
})
.whenComplete(() => _handlingRequest = false)
.onError((error, _) {
Expand All @@ -92,6 +84,18 @@ class InputHandler extends Handler {
});
}

void handleResult(result) {
_log.v("Input state handled: $result");
if (result is LNURLPageResult && result.protocol != null) {
final context = contextProvider?.getBuildContext();
if (context != null) {
handleLNURLPageResult(context, result);
} else {
_log.v("Skipping handling of result: $result because context is null");
}
}
}

Future handleInputData(dynamic parsedInput) async {
_log.v("handle input $parsedInput");
final context = contextProvider?.getBuildContext();
Expand All @@ -110,6 +114,8 @@ class InputHandler extends Handler {
} else if (parsedInput is InputType_NodeId) {
return handleNodeID(context, parsedInput.nodeId);
}
// Input wasn't handled
return false;
}

Future handleInvoice(BuildContext context, Invoice invoice) async {
Expand Down
16 changes: 10 additions & 6 deletions lib/routes/home/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,18 @@ class HomeState extends State<Home> with AutoLockMixin, HandlerContextProvider {
final GlobalKey firstPaymentItemKey = GlobalKey();
final ScrollController scrollController = ScrollController();
final handlers = <Handler>[];
late final InputHandler inputHandler = InputHandler(
firstPaymentItemKey,
scrollController,
_scaffoldKey,
);

@override
void initState() {
super.initState();
SchedulerBinding.instance.addPostFrameCallback((_) {
handlers.addAll([
InputHandler(
firstPaymentItemKey,
scrollController,
_scaffoldKey,
),
inputHandler,
ConnectivityHandler(),
PaymentResultHandler(),
]);
Expand Down Expand Up @@ -89,7 +90,10 @@ class HomeState extends State<Home> with AutoLockMixin, HandlerContextProvider {
drawerDragStartBehavior: DragStartBehavior.down,
drawerEdgeDragWidth: mediaSize.width,
drawer: HomeDrawer(key: _drawerKey),
bottomNavigationBar: BottomActionsBar(firstPaymentItemKey),
bottomNavigationBar: BottomActionsBar(
firstPaymentItemKey,
inputHandler,
),
floatingActionButton: QrActionButton(firstPaymentItemKey),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
body: _drawerKey.currentState?.screen() ?? AccountPage(firstPaymentItemKey, scrollController),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:auto_size_text/auto_size_text.dart';
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/handlers/input_handler.dart';
import 'package:c_breez/routes/home/widgets/bottom_actions_bar/receive_options_bottom_sheet.dart';
import 'package:c_breez/routes/home/widgets/bottom_actions_bar/send_options_bottom_sheet.dart';
import 'package:flutter/material.dart';
Expand All @@ -11,9 +12,11 @@ import 'bottom_action_item.dart';

class BottomActionsBar extends StatelessWidget {
final GlobalKey firstPaymentItemKey;
final InputHandler inputHandler;

const BottomActionsBar(
this.firstPaymentItemKey, {
this.firstPaymentItemKey,
this.inputHandler, {
Key? key,
}) : super(key: key);

Expand All @@ -31,6 +34,7 @@ class BottomActionsBar extends StatelessWidget {
children: [
SendOptions(
firstPaymentItemKey: firstPaymentItemKey,
inputHandler: inputHandler,
actionsGroup: actionsGroup,
),
Container(width: 64),
Expand All @@ -49,11 +53,13 @@ class BottomActionsBar extends StatelessWidget {

class SendOptions extends StatelessWidget {
final GlobalKey<State<StatefulWidget>> firstPaymentItemKey;
final InputHandler inputHandler;
final AutoSizeGroup actionsGroup;

const SendOptions({
Key? key,
required this.firstPaymentItemKey,
required this.inputHandler,
required this.actionsGroup,
}) : super(key: key);

Expand All @@ -67,6 +73,7 @@ class SendOptions extends StatelessWidget {
builder: (context) {
return SendOptionsBottomSheet(
firstPaymentItemKey: firstPaymentItemKey,
inputHandler: inputHandler,
);
},
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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/spontaneous_payment/spontaneous_payment_page.dart';
Expand All @@ -18,8 +19,13 @@ final _log = FimberLog("SendOptionsBottomSheet");

class SendOptionsBottomSheet extends StatelessWidget {
final GlobalKey firstPaymentItemKey;
final InputHandler inputHandler;

const SendOptionsBottomSheet({super.key, required this.firstPaymentItemKey});
const SendOptionsBottomSheet({
super.key,
required this.firstPaymentItemKey,
required this.inputHandler,
});

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -77,31 +83,32 @@ 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(
BuildContext context,
dynamic inputData,
GlobalKey firstPaymentItemKey,
) async {
// Close bottom sheet
Navigator.of(context).pop();
if (inputData is InputType_NodeId) {
_log.v("Input data is of type InputType_NodeId, pushing SpontaneousPaymentPage");
Navigator.of(context).push(
FadeInRoute(
builder: (_) => SpontaneousPaymentPage(
inputData.nodeId,
firstPaymentItemKey,
),
),
);
} else {
_log.v("Input data is $inputData, showing EnterPaymentInfoDialog");
await showDialog(
useRootNavigator: false,
context: context,
barrierDismissible: false,
builder: (_) => EnterPaymentInfoDialog(paymentItemKey: firstPaymentItemKey),
);
}
// 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);
}
});
}

void _push(BuildContext context, String route, {Object? arguments}) {
Expand Down

0 comments on commit 54f7de6

Please sign in to comment.