2021-04-13 18:40:44 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:cake_wallet/buy/buy_provider.dart';
|
|
|
|
import 'package:cake_wallet/buy/moonpay/moonpay_buy_provider.dart';
|
|
|
|
import 'package:cake_wallet/buy/wyre/wyre_buy_provider.dart';
|
|
|
|
import 'package:cake_wallet/generated/i18n.dart';
|
|
|
|
import 'package:cake_wallet/palette.dart';
|
|
|
|
import 'package:cake_wallet/src/screens/base_page.dart';
|
|
|
|
import 'package:cake_wallet/store/dashboard/orders_store.dart';
|
|
|
|
import 'package:cake_wallet/view_model/buy/buy_view_model.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2023-01-05 08:40:05 +00:00
|
|
|
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
|
2021-04-13 18:40:44 +00:00
|
|
|
|
|
|
|
class BuyWebViewPage extends BasePage {
|
2023-01-05 08:40:05 +00:00
|
|
|
BuyWebViewPage({required this.buyViewModel, required this.ordersStore, required this.url});
|
2021-04-13 18:40:44 +00:00
|
|
|
|
|
|
|
final OrdersStore ordersStore;
|
|
|
|
final String url;
|
|
|
|
final BuyViewModel buyViewModel;
|
|
|
|
|
|
|
|
@override
|
|
|
|
String get title => S.current.buy;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Color get backgroundDarkColor => Colors.white;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Color get titleColor => Palette.darkBlueCraiola;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget body(BuildContext context) =>
|
|
|
|
BuyWebViewPageBody(buyViewModel, ordersStore: ordersStore, url: url);
|
|
|
|
}
|
|
|
|
|
|
|
|
class BuyWebViewPageBody extends StatefulWidget {
|
2022-10-12 17:09:57 +00:00
|
|
|
BuyWebViewPageBody(this.buyViewModel, {required this.ordersStore, this.url});
|
2021-04-13 18:40:44 +00:00
|
|
|
|
|
|
|
final OrdersStore ordersStore;
|
2022-10-12 17:09:57 +00:00
|
|
|
final String? url;
|
2021-04-13 18:40:44 +00:00
|
|
|
final BuyViewModel buyViewModel;
|
|
|
|
|
|
|
|
@override
|
|
|
|
BuyWebViewPageBodyState createState() => BuyWebViewPageBodyState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class BuyWebViewPageBodyState extends State<BuyWebViewPageBody> {
|
2022-10-12 17:09:57 +00:00
|
|
|
BuyWebViewPageBodyState()
|
2023-01-05 08:40:05 +00:00
|
|
|
: _webViewkey = GlobalKey(),
|
|
|
|
_isSaving = false,
|
|
|
|
orderId = '';
|
2022-10-12 17:09:57 +00:00
|
|
|
|
2021-04-13 18:40:44 +00:00
|
|
|
String orderId;
|
2023-01-05 08:40:05 +00:00
|
|
|
InAppWebViewController? _webViewController;
|
2021-04-13 18:40:44 +00:00
|
|
|
GlobalKey _webViewkey;
|
2022-10-12 17:09:57 +00:00
|
|
|
Timer? _timer;
|
2021-04-13 18:40:44 +00:00
|
|
|
bool _isSaving;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_webViewkey = GlobalKey();
|
|
|
|
_isSaving = false;
|
|
|
|
widget.ordersStore.orderId = '';
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
if (widget.buyViewModel.selectedProvider is WyreBuyProvider) {
|
2021-04-15 17:10:23 +00:00
|
|
|
_saveOrder(keyword: 'completed', splitSymbol: '/');
|
2021-04-13 18:40:44 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
if (widget.buyViewModel.selectedProvider is MoonPayBuyProvider) {
|
2021-04-15 17:10:23 +00:00
|
|
|
_saveOrder(keyword: 'transactionId', splitSymbol: '=');
|
2021-04-13 18:40:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-01-05 08:40:05 +00:00
|
|
|
return InAppWebView(
|
2021-04-13 18:40:44 +00:00
|
|
|
key: _webViewkey,
|
2023-01-06 07:08:36 +00:00
|
|
|
initialOptions: InAppWebViewGroupOptions(
|
|
|
|
crossPlatform: InAppWebViewOptions(transparentBackground: true),
|
|
|
|
),
|
2023-01-05 08:40:05 +00:00
|
|
|
initialUrlRequest: URLRequest(url: Uri.tryParse(widget.url ?? '')),
|
|
|
|
onWebViewCreated: (InAppWebViewController controller) =>
|
2021-04-13 18:40:44 +00:00
|
|
|
setState(() => _webViewController = controller));
|
|
|
|
}
|
2021-04-15 17:10:23 +00:00
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
void _saveOrder({required String keyword, required String splitSymbol}) {
|
2021-04-15 17:10:23 +00:00
|
|
|
_timer?.cancel();
|
|
|
|
_timer = Timer.periodic(Duration(seconds: 1), (timer) async {
|
|
|
|
try {
|
|
|
|
if (_webViewController == null || _isSaving) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-05 08:40:05 +00:00
|
|
|
final url = await _webViewController!.getUrl();
|
|
|
|
final urlString = url.toString();
|
2022-10-12 17:09:57 +00:00
|
|
|
if (url == null) {
|
|
|
|
throw Exception('_saveOrder: Url is null');
|
|
|
|
}
|
2021-04-15 17:10:23 +00:00
|
|
|
|
2023-01-05 08:40:05 +00:00
|
|
|
if (urlString.toString().contains(keyword)) {
|
|
|
|
final urlParts = urlString.split(splitSymbol);
|
2021-04-15 17:10:23 +00:00
|
|
|
orderId = urlParts.last;
|
|
|
|
widget.ordersStore.orderId = orderId;
|
|
|
|
|
|
|
|
if (orderId.isNotEmpty) {
|
|
|
|
_isSaving = true;
|
|
|
|
await widget.buyViewModel.saveOrder(orderId);
|
|
|
|
timer.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
_isSaving = false;
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-04-13 18:40:44 +00:00
|
|
|
}
|