cake_wallet/lib/src/screens/wyre/wyre_page.dart

98 lines
2.5 KiB
Dart
Raw Normal View History

2021-03-23 18:09:35 +00:00
import 'dart:async';
2021-03-17 10:32:22 +00:00
import 'dart:io';
import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/palette.dart';
2021-03-17 10:32:22 +00:00
import 'package:cake_wallet/src/screens/base_page.dart';
import 'package:cake_wallet/store/dashboard/orders_store.dart';
2021-03-23 11:05:01 +00:00
import 'package:cake_wallet/view_model/wyre_view_model.dart';
2021-03-17 10:32:22 +00:00
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class WyrePage extends BasePage {
2021-03-24 09:50:51 +00:00
WyrePage(this.wyreViewModel,
{@required this.ordersStore, @required this.url});
2021-03-17 10:32:22 +00:00
final OrdersStore ordersStore;
2021-03-17 10:32:22 +00:00
final String url;
2021-03-23 11:05:01 +00:00
final WyreViewModel wyreViewModel;
2021-03-17 10:32:22 +00:00
@override
String get title => S.current.buy;
@override
Color get backgroundDarkColor => Colors.white;
@override
Color get titleColor => Palette.darkBlueCraiola;
2021-03-17 10:32:22 +00:00
@override
Widget body(BuildContext context) =>
2021-03-24 09:50:51 +00:00
WyrePageBody(wyreViewModel, ordersStore: ordersStore, url: url);
2021-03-17 10:32:22 +00:00
}
class WyrePageBody extends StatefulWidget {
2021-03-23 11:05:01 +00:00
WyrePageBody(this.wyreViewModel, {this.ordersStore, this.url});
2021-03-17 10:32:22 +00:00
final OrdersStore ordersStore;
2021-03-17 10:32:22 +00:00
final String url;
2021-03-23 11:05:01 +00:00
final WyreViewModel wyreViewModel;
2021-03-17 10:32:22 +00:00
@override
WyrePageBodyState createState() => WyrePageBodyState();
2021-03-17 10:32:22 +00:00
}
class WyrePageBodyState extends State<WyrePageBody> {
2021-03-17 14:14:48 +00:00
String orderId;
WebViewController _webViewController;
GlobalKey _webViewkey;
2021-03-23 18:09:35 +00:00
Timer _timer;
2021-03-24 09:50:51 +00:00
bool _isSaving;
2021-03-17 10:32:22 +00:00
@override
void initState() {
super.initState();
2021-03-17 14:14:48 +00:00
_webViewkey = GlobalKey();
2021-03-24 09:50:51 +00:00
_isSaving = false;
widget.ordersStore.orderId = '';
2021-03-17 14:14:48 +00:00
2021-03-17 10:32:22 +00:00
if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
2021-03-23 18:09:35 +00:00
2021-03-24 09:50:51 +00:00
_timer?.cancel();
_timer = Timer.periodic(Duration(seconds: 1), (timer) async {
try {
if (_webViewController == null || _isSaving) {
return;
}
2021-03-23 18:09:35 +00:00
2021-03-24 09:50:51 +00:00
final url = await _webViewController.currentUrl();
2021-03-23 18:09:35 +00:00
2021-03-24 09:50:51 +00:00
if (url.contains('completed')) {
final urlParts = url.split('/');
orderId = urlParts.last;
widget.ordersStore.orderId = orderId;
2021-03-23 18:09:35 +00:00
2021-03-24 09:50:51 +00:00
if (orderId.isNotEmpty) {
_isSaving = true;
await widget.wyreViewModel.saveOrder(orderId);
timer.cancel();
}
2021-03-23 18:09:35 +00:00
}
2021-03-24 09:50:51 +00:00
} catch (e) {
_isSaving = false;
print(e);
2021-03-23 18:09:35 +00:00
}
});
2021-03-17 10:32:22 +00:00
}
@override
Widget build(BuildContext context) {
return WebView(
2021-03-17 14:14:48 +00:00
key: _webViewkey,
initialUrl: widget.url,
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController controller) =>
2021-03-23 18:09:35 +00:00
setState(() => _webViewController = controller));
2021-03-17 10:32:22 +00:00
}
2021-03-17 14:14:48 +00:00
}