From af356bd8f108cd02a9c2a4cc03fe0f2a619f9c6c Mon Sep 17 00:00:00 2001 From: OleksandrSobol Date: Fri, 27 Aug 2021 20:46:24 +0300 Subject: [PATCH] CAKE-356 | applied yat record to parse_address_from_domain.dart and parse_address_from_domain_alert.dart; fixed yat_record.dart, yat_view_model.dart and yat_webview_page.dart --- lib/entities/parse_address_from_domain.dart | 16 +++- lib/entities/parsed_address.dart | 2 +- .../parse_address_from_domain_alert.dart | 4 + lib/src/screens/yat/yat_webview_page.dart | 85 +++++++++++++++++-- lib/view_model/yat_view_model.dart | 16 ++-- lib/yat/yat_record.dart | 14 +-- 6 files changed, 108 insertions(+), 29 deletions(-) diff --git a/lib/entities/parse_address_from_domain.dart b/lib/entities/parse_address_from_domain.dart index 2fb121c7f..438cd9ddb 100644 --- a/lib/entities/parse_address_from_domain.dart +++ b/lib/entities/parse_address_from_domain.dart @@ -1,6 +1,7 @@ import 'package:cake_wallet/entities/openalias_record.dart'; import 'package:cake_wallet/entities/parsed_address.dart'; import 'package:cake_wallet/entities/unstoppable_domain_address.dart'; +import 'package:cake_wallet/yat/yat_record.dart'; const unstoppableDomains = [ 'crypto', @@ -22,7 +23,20 @@ Future parseAddressFromDomain( final name = domainParts.last; if (domainParts.length <= 1 || domainParts.first.isEmpty || name.isEmpty) { - return ParsedAddress(address: domain); + try { + final address = await fetchYatAddress(domain); + + if (address?.isEmpty ?? true) { + return ParsedAddress(address: domain); + } + + return ParsedAddress( + address: address, + name: domain, + parseFrom: ParseFrom.yatRecord); + } catch (e) { + return ParsedAddress(address: domain); + } } if (unstoppableDomains.any((domain) => name.contains(domain))) { diff --git a/lib/entities/parsed_address.dart b/lib/entities/parsed_address.dart index 91fec6750..192d839b5 100644 --- a/lib/entities/parsed_address.dart +++ b/lib/entities/parsed_address.dart @@ -1,4 +1,4 @@ -enum ParseFrom {unstoppableDomains, openAlias, notParsed} +enum ParseFrom {unstoppableDomains, openAlias, yatRecord, notParsed} class ParsedAddress { ParsedAddress({ diff --git a/lib/src/screens/send/widgets/parse_address_from_domain_alert.dart b/lib/src/screens/send/widgets/parse_address_from_domain_alert.dart index 526393109..87c841f72 100644 --- a/lib/src/screens/send/widgets/parse_address_from_domain_alert.dart +++ b/lib/src/screens/send/widgets/parse_address_from_domain_alert.dart @@ -17,6 +17,10 @@ void showAddressAlert(BuildContext context, ParsedAddress parsedAddress) async { title = S.of(context).openalias_alert_title; content = S.of(context).openalias_alert_content(parsedAddress.name); break; + case ParseFrom.yatRecord: + title = 'Address detected'; + content = 'This address is from ${parsedAddress.name} on Yat'; + break; case ParseFrom.notParsed: return; } diff --git a/lib/src/screens/yat/yat_webview_page.dart b/lib/src/screens/yat/yat_webview_page.dart index dbfb8b14a..34de2de28 100644 --- a/lib/src/screens/yat/yat_webview_page.dart +++ b/lib/src/screens/yat/yat_webview_page.dart @@ -1,3 +1,5 @@ +import 'dart:async'; +import 'dart:io'; import 'package:cake_wallet/palette.dart'; import 'package:cake_wallet/src/screens/base_page.dart'; import 'package:cake_wallet/view_model/yat_view_model.dart'; @@ -7,7 +9,37 @@ import 'package:webview_flutter/webview_flutter.dart'; enum YatMode {create, connect} class YatWebViewPage extends BasePage { - YatWebViewPage({this.yatViewModel, this.mode}) { + YatWebViewPage({this.yatViewModel, this.mode}); + + final YatMode mode; + final YatViewModel yatViewModel; + + @override + String get title => 'Yat'; + + @override + Color get backgroundDarkColor => Colors.white; + + @override + Color get titleColor => Palette.darkBlueCraiola; + + @override + Widget body(BuildContext context) => YatWebViewPageBody(yatViewModel, mode); +} + +class YatWebViewPageBody extends StatefulWidget{ + YatWebViewPageBody(this.yatViewModel, this.mode); + + final YatMode mode; + final YatViewModel yatViewModel; + + @override + YatWebViewPageBodyState createState() => + YatWebViewPageBodyState(yatViewModel, mode); +} + +class YatWebViewPageBodyState extends State { + YatWebViewPageBodyState(this.yatViewModel, this.mode) { switch (mode) { case YatMode.create: url = _baseUrl + _createSuffix; @@ -20,7 +52,7 @@ class YatWebViewPage extends BasePage { } } - static const _baseUrl = 'https://y.at'; + static const _baseUrl = 'https://yat.fyi'; static const _signInSuffix = '/sign-in'; static const _createSuffix = '/create'; @@ -28,18 +60,53 @@ class YatWebViewPage extends BasePage { final YatViewModel yatViewModel; String url; + WebViewController _webViewController; + GlobalKey _webViewkey; + Timer _timer; @override - String get title => 'Yat'; + void initState() { + super.initState(); + _webViewkey = GlobalKey(); + if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView(); + _fetchYatInfo(); + } @override - Color get backgroundDarkColor => Colors.white; + void dispose() { + super.dispose(); + _timer?.cancel(); + } @override - Color get titleColor => Palette.darkBlueCraiola; + Widget build(BuildContext context) { + return WebView( + key: _webViewkey, + initialUrl: url, + javascriptMode: JavascriptMode.unrestricted, + onWebViewCreated: (WebViewController controller) => + setState(() => _webViewController = controller)); + } - @override - Widget body(BuildContext context) => WebView( - initialUrl: url, - javascriptMode: JavascriptMode.unrestricted); + void _fetchYatInfo() { + final keyword = 'dashboard'; + _timer?.cancel(); + _timer = Timer.periodic(Duration(seconds: 1), (timer) async { + + try { + if (_webViewController == null) { + return; + } + + final url = await _webViewController.currentUrl(); + print('URL = $url'); + if (url.contains(keyword)) { + timer.cancel(); + await yatViewModel.fetchCartInfo(); + } + } catch (e) { + print(e); + } + }); + } } \ No newline at end of file diff --git a/lib/view_model/yat_view_model.dart b/lib/view_model/yat_view_model.dart index 68b08469a..b6939154b 100644 --- a/lib/view_model/yat_view_model.dart +++ b/lib/view_model/yat_view_model.dart @@ -4,6 +4,7 @@ import 'package:mobx/mobx.dart'; import 'dart:convert'; import 'package:cake_wallet/yat/yat_exception.dart'; import 'package:http/http.dart'; +import 'package:cake_wallet/.secrets.g.dart' as secrets; part 'yat_view_model.g.dart'; @@ -15,23 +16,24 @@ abstract class YatViewModelBase with Store { final YatStore yatStore; Future fetchCartInfo() async { - const _apiKey = ''; // FIXME - - final url = 'https://api.y.at/cart'; + final url = 'https://a.y.at/cart'; + final _apiKey = secrets.yatApiToken; final response = await get( url, headers: { - 'Accept': '*/*', - 'Authorization,X-Api-Key': _apiKey + 'Accept':'*/*', + 'X-Api-Key': _apiKey } ); - if (response.statusCode != 200) { + print('RESPONSE = ${response.body.toString()}'); + + /*if (response.statusCode != 200) { throw YatException(text: response.body.toString()); } final responseJSON = json.decode(response.body) as Map; - yatStore.yatAddress = responseJSON[''] as String; // FIXME + yatStore.yatAddress = responseJSON[''] as String;*/ // FIXME } } \ No newline at end of file diff --git a/lib/yat/yat_record.dart b/lib/yat/yat_record.dart index aa5275e95..80d9e234d 100644 --- a/lib/yat/yat_record.dart +++ b/lib/yat/yat_record.dart @@ -3,24 +3,16 @@ import 'package:cake_wallet/yat/yat_exception.dart'; import 'package:http/http.dart'; Future fetchYatAddress(String emojiId) async { - const _apiKey = ''; // FIXME - const _requestURL = 'https://api.y.at/emoji_id/'; + const _requestURL = 'https://a.y.at/emoji_id/'; final url = _requestURL + emojiId; - - final response = await get( - url, - headers: { - 'Accept': '*/*', - 'Authorization': 'Bearer $_apiKey' - } - ); + final response = await get(url); if (response.statusCode != 200) { throw YatException(text: response.body.toString()); } final responseJSON = json.decode(response.body) as Map; - final yatAddress = responseJSON[''] as String; // FIXME + final yatAddress = responseJSON['result'][2]['data'] as String; return yatAddress; } \ No newline at end of file