Merge pull request #657 from cake-tech/CW-66-open-app-from-qr-code

Cw 66 open app from qr code
This commit is contained in:
Omar Hatem
2022-12-13 20:38:06 +02:00
committed by GitHub
25 changed files with 195 additions and 51 deletions
+38 -1
View File
@@ -1,10 +1,12 @@
import 'dart:async';
import 'package:cake_wallet/utils/payment_request.dart';
import 'package:flutter/material.dart';
import 'package:cake_wallet/routes.dart';
import 'package:cake_wallet/src/screens/auth/auth_page.dart';
import 'package:cake_wallet/store/app_store.dart';
import 'package:cake_wallet/store/authentication_store.dart';
import 'package:cake_wallet/entities/qr_scanner.dart';
import 'package:uni_links/uni_links.dart';
class Root extends StatefulWidget {
Root(
@@ -35,6 +37,9 @@ class RootState extends State<Root> with WidgetsBindingObserver {
bool _isInactive;
bool _postFrameCallback;
StreamSubscription<Uri?>? stream;
Uri? launchUri;
@override
void initState() {
_isInactiveController = StreamController<bool>.broadcast();
@@ -42,6 +47,34 @@ class RootState extends State<Root> with WidgetsBindingObserver {
_postFrameCallback = false;
WidgetsBinding.instance.addObserver(this);
super.initState();
initUniLinks();
}
@override
void dispose() {
stream?.cancel();
super.dispose();
}
/// handle app links while the app is already started
/// whether its in the foreground or in the background.
Future<void> initUniLinks() async {
try {
stream = uriLinkStream.listen((Uri? uri) {
handleDeepLinking(uri);
});
handleDeepLinking(await getInitialUri());
} catch (e) {
print(e);
}
}
void handleDeepLinking(Uri? uri) {
if (uri == null || !mounted) return;
launchUri = uri;
}
@override
@@ -75,7 +108,11 @@ class RootState extends State<Root> with WidgetsBindingObserver {
}
_reset();
auth.close();
auth.close(
route: launchUri != null ? Routes.send : null,
arguments: PaymentRequest.fromUri(launchUri),
);
launchUri = null;
});
});
}
+7 -1
View File
@@ -4,6 +4,7 @@ import 'package:cake_wallet/src/screens/send/widgets/send_card.dart';
import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart';
import 'package:cake_wallet/src/widgets/picker.dart';
import 'package:cake_wallet/src/widgets/template_tile.dart';
import 'package:cake_wallet/utils/payment_request.dart';
import 'package:cake_wallet/view_model/send/output.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
@@ -25,11 +26,15 @@ import 'package:smooth_page_indicator/smooth_page_indicator.dart';
import 'package:cw_core/crypto_currency.dart';
class SendPage extends BasePage {
SendPage({required this.sendViewModel}) : _formKey = GlobalKey<FormState>();
SendPage({
required this.sendViewModel,
this.initialPaymentRequest,
}) : _formKey = GlobalKey<FormState>();
final SendViewModel sendViewModel;
final GlobalKey<FormState> _formKey;
final controller = PageController(initialPage: 0);
final PaymentRequest? initialPaymentRequest;
bool _effectsInstalled = false;
@@ -116,6 +121,7 @@ class SendPage extends BasePage {
key: output.key,
output: output,
sendViewModel: sendViewModel,
initialPaymentRequest: initialPaymentRequest,
);
});
},
+46 -5
View File
@@ -1,4 +1,5 @@
import 'package:cake_wallet/entities/priority_for_wallet_type.dart';
import 'package:cake_wallet/src/widgets/alert_with_one_action.dart';
import 'package:cake_wallet/utils/payment_request.dart';
import 'package:cw_core/transaction_priority.dart';
import 'package:cake_wallet/routes.dart';
@@ -20,21 +21,28 @@ class SendCard extends StatefulWidget {
SendCard({
Key? key,
required this.output,
required this.sendViewModel}) : super(key: key);
required this.sendViewModel,
this.initialPaymentRequest,
}) : super(key: key);
final Output output;
final SendViewModel sendViewModel;
final PaymentRequest? initialPaymentRequest;
@override
SendCardState createState() => SendCardState(
output: output,
sendViewModel: sendViewModel
sendViewModel: sendViewModel,
initialPaymentRequest: initialPaymentRequest,
);
}
class SendCardState extends State<SendCard>
with AutomaticKeepAliveClientMixin<SendCard> {
SendCardState({required this.output, required this.sendViewModel})
SendCardState({
required this.output,
required this.sendViewModel,
this.initialPaymentRequest})
: addressController = TextEditingController(),
cryptoAmountController = TextEditingController(),
fiatAmountController = TextEditingController(),
@@ -49,6 +57,7 @@ class SendCardState extends State<SendCard>
final Output output;
final SendViewModel sendViewModel;
final PaymentRequest? initialPaymentRequest;
final TextEditingController addressController;
final TextEditingController cryptoAmountController;
@@ -61,6 +70,27 @@ class SendCardState extends State<SendCard>
bool _effectsInstalled = false;
@override
void initState() {
super.initState();
/// if the current wallet doesn't match the one in the qr code
if (initialPaymentRequest != null &&
sendViewModel.walletCurrencyName != initialPaymentRequest!.scheme.toLowerCase()) {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
showPopUp<void>(
context: context,
builder: (BuildContext context) {
return AlertWithOneAction(
alertTitle: S.of(context).error,
alertContent: S.of(context).unmatched_currencies,
buttonText: S.of(context).ok,
buttonAction: () => Navigator.of(context).pop());
});
});
}
}
@override
Widget build(BuildContext context) {
super.build(context);
@@ -512,8 +542,12 @@ class SendCardState extends State<SendCard>
}
void _setEffects(BuildContext context) {
addressController.text = output.address;
cryptoAmountController.text = output.cryptoAmount;
if (output.address.isNotEmpty) {
addressController.text = output.address;
}
if (output.cryptoAmount.isNotEmpty) {
cryptoAmountController.text = output.cryptoAmount;
}
fiatAmountController.text = output.fiatAmount;
noteController.text = output.note;
extractedAddressController.text = output.extractedAddress;
@@ -605,6 +639,13 @@ class SendCardState extends State<SendCard>
extractedAddressController.text = extractedAddress;
});
if (initialPaymentRequest != null &&
sendViewModel.walletCurrencyName == initialPaymentRequest!.scheme.toLowerCase()) {
addressController.text = initialPaymentRequest!.address;
cryptoAmountController.text = initialPaymentRequest!.amount;
noteController.text = initialPaymentRequest!.note;
}
_effectsInstalled = true;
}