| 1 | import 'dart:async'; |
| 2 | import 'package:cake_wallet/buy/moonpay/moonpay_provider.dart'; |
| 3 | import 'package:cake_wallet/buy/wyre/wyre_buy_provider.dart'; |
| 4 | import 'package:cake_wallet/generated/i18n.dart'; |
| 5 | import 'package:cake_wallet/src/screens/base_page.dart'; |
| 6 | import 'package:cake_wallet/store/dashboard/orders_store.dart'; |
| 7 | import 'package:cake_wallet/view_model/buy/buy_view_model.dart'; |
| 8 | import 'package:cw_core/utils/print_verbose.dart'; |
| 9 | import 'package:flutter/material.dart'; |
| 10 | import 'package:flutter_inappwebview/flutter_inappwebview.dart'; |
| 11 | |
| 12 | class BuyWebViewPage extends BasePage { |
| 13 | BuyWebViewPage({required this.buyViewModel, required this.ordersStore, required this.url}); |
| 14 | |
| 15 | final OrdersStore ordersStore; |
| 16 | final String url; |
| 17 | final BuyViewModel buyViewModel; |
| 18 | |
| 19 | @override |
| 20 | String get title => S.current.buy; |
| 21 | |
| 22 | @override |
| 23 | Widget body(BuildContext context) => |
| 24 | BuyWebViewPageBody(buyViewModel, ordersStore: ordersStore, url: url); |
| 25 | } |
| 26 | |
| 27 | class BuyWebViewPageBody extends StatefulWidget { |
| 28 | BuyWebViewPageBody(this.buyViewModel, {required this.ordersStore, this.url}); |
| 29 | |
| 30 | final OrdersStore ordersStore; |
| 31 | final String? url; |
| 32 | final BuyViewModel buyViewModel; |
| 33 | |
| 34 | @override |
| 35 | BuyWebViewPageBodyState createState() => BuyWebViewPageBodyState(); |
| 36 | } |
| 37 | |
| 38 | class BuyWebViewPageBodyState extends State<BuyWebViewPageBody> { |
| 39 | BuyWebViewPageBodyState() |
| 40 | : _webViewkey = GlobalKey(), |
| 41 | _isSaving = false, |
| 42 | orderId = ''; |
| 43 | |
| 44 | String orderId; |
| 45 | InAppWebViewController? _webViewController; |
| 46 | GlobalKey _webViewkey; |
| 47 | Timer? _timer; |
| 48 | bool _isSaving; |
| 49 | |
| 50 | @override |
| 51 | void initState() { |
| 52 | super.initState(); |
| 53 | _webViewkey = GlobalKey(); |
| 54 | _isSaving = false; |
| 55 | widget.ordersStore.orderId = ''; |
| 56 | |
| 57 | if (widget.buyViewModel.selectedProvider is WyreBuyProvider) { |
| 58 | _saveOrder(keyword: 'completed', splitSymbol: '/'); |
| 59 | } |
| 60 | |
| 61 | if (widget.buyViewModel.selectedProvider is MoonPayProvider) { |
| 62 | _saveOrder(keyword: 'transactionId', splitSymbol: '='); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | @override |
| 67 | Widget build(BuildContext context) { |
| 68 | return InAppWebView( |
| 69 | key: _webViewkey, |
| 70 | initialSettings: InAppWebViewSettings( |
| 71 | transparentBackground: true, |
| 72 | ), |
| 73 | initialUrlRequest: URLRequest(url: WebUri(widget.url ?? '')), |
| 74 | onWebViewCreated: (InAppWebViewController controller) => |
| 75 | setState(() => _webViewController = controller)); |
| 76 | } |
| 77 | |
| 78 | void _saveOrder({required String keyword, required String splitSymbol}) { |
| 79 | _timer?.cancel(); |
| 80 | _timer = Timer.periodic(Duration(seconds: 1), (timer) async { |
| 81 | try { |
| 82 | if (_webViewController == null || _isSaving) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | final url = (await _webViewController!.getUrl())?.toString(); |
| 87 | if (url == null) { |
| 88 | throw Exception('_saveOrder: Url is null'); |
| 89 | } |
| 90 | |
| 91 | if (url.contains(keyword)) { |
| 92 | final urlParts = url.split(splitSymbol); |
| 93 | orderId = urlParts.last; |
| 94 | widget.ordersStore.orderId = orderId; |
| 95 | |
| 96 | if (orderId.isNotEmpty) { |
| 97 | _isSaving = true; |
| 98 | await widget.buyViewModel.saveOrder(orderId); |
| 99 | timer.cancel(); |
| 100 | } |
| 101 | } |
| 102 | } catch (e) { |
| 103 | _isSaving = false; |
| 104 | printV(e); |
| 105 | } |
| 106 | }); |
| 107 | } |
| 108 | } |