From 314044dde4b364852b5b7b601ae31703c6f32d59 Mon Sep 17 00:00:00 2001 From: PuPha Date: Tue, 17 Oct 2023 18:05:12 +0700 Subject: [PATCH 1/2] Fix open github docs for postcard --- lib/screen/github_doc.dart | 15 ++++++------- .../postcard_explain.dart | 20 ++++++++++-------- lib/service/navigation_service.dart | 21 ++++++++++++++++--- lib/util/constants.dart | 7 +++++++ lib/util/string_ext.dart | 12 +++++++++++ lib/view/artwork_common_widget.dart | 5 +++++ 6 files changed, 59 insertions(+), 21 deletions(-) diff --git a/lib/screen/github_doc.dart b/lib/screen/github_doc.dart index 6e2e1b22a..35bd470bc 100644 --- a/lib/screen/github_doc.dart +++ b/lib/screen/github_doc.dart @@ -5,10 +5,12 @@ // that can be found in the LICENSE file. // -import 'package:autonomy_flutter/screen/app_router.dart'; +import 'package:autonomy_flutter/common/injector.dart'; +import 'package:autonomy_flutter/service/navigation_service.dart'; import 'package:autonomy_flutter/util/constants.dart'; import 'package:autonomy_flutter/util/dio_interceptors.dart'; import 'package:autonomy_flutter/util/dio_util.dart'; +import 'package:autonomy_flutter/util/string_ext.dart'; import 'package:autonomy_flutter/util/style.dart'; import 'package:autonomy_flutter/view/back_appbar.dart'; import 'package:autonomy_flutter/view/responsive.dart'; @@ -32,6 +34,7 @@ class GithubDocPage extends StatefulWidget { class _GithubDocPageState extends State { late String document; late String title; + final _navigationService = injector(); final dio = baseDio(BaseOptions( baseUrl: "https://raw.githubusercontent.com", @@ -83,14 +86,8 @@ class _GithubDocPageState extends State { styleSheet: markDownLightStyle(context), onTapLink: (text, href, title) async { if (href == null) return; - if (!(await canLaunchUrlString(href))) { - if (!mounted) return; - Navigator.of(context).pushNamed(AppRouter.githubDocPage, - arguments: { - "prefix": widget.payload["prefix"] ?? '', - "document": href, - "title": "" - }); + if (href.isAutonomyDocumentLink) { + _navigationService.openAutonomyDocument(href, title); } else { launchUrlString(href); } diff --git a/lib/screen/interactive_postcard/postcard_explain.dart b/lib/screen/interactive_postcard/postcard_explain.dart index ff0ccdd2c..6dcd56989 100644 --- a/lib/screen/interactive_postcard/postcard_explain.dart +++ b/lib/screen/interactive_postcard/postcard_explain.dart @@ -1,5 +1,6 @@ import 'package:autonomy_flutter/common/injector.dart'; import 'package:autonomy_flutter/service/configuration_service.dart'; +import 'package:autonomy_flutter/service/navigation_service.dart'; import 'package:autonomy_flutter/service/postcard_service.dart'; import 'package:autonomy_flutter/util/constants.dart'; import 'package:autonomy_flutter/util/distance_formater.dart'; @@ -14,7 +15,6 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_svg/svg.dart'; import 'package:nft_collection/models/asset_token.dart'; -import 'package:url_launcher/url_launcher.dart'; import 'package:video_player/video_player.dart'; class PostcardExplain extends StatefulWidget { @@ -28,6 +28,7 @@ class PostcardExplain extends StatefulWidget { } class _PostcardExplainState extends State { + final _navigationService = injector(); final VideoPlayerController _controller = VideoPlayerController.asset("assets/videos/postcard_explain.mp4"); final VideoPlayerController _colouringController = @@ -224,14 +225,15 @@ class _PostcardExplainState extends State { text: "by_continuing".tr(), ), TextSpan( - text: "terms_and_conditions".tr(), - style: termsConditionsStyle.copyWith( - decoration: TextDecoration.underline), - recognizer: TapGestureRecognizer() - ..onTap = () => launchUrl( - Uri.parse(MOMA_TERMS_CONDITIONS_URL), - mode: LaunchMode.externalApplication), - ), + text: "terms_and_conditions".tr(), + style: termsConditionsStyle.copyWith( + decoration: TextDecoration.underline), + recognizer: TapGestureRecognizer() + ..onTap = () { + _navigationService.openAutonomyDocument( + MOMA_TERMS_CONDITIONS_URL, + "terms_and_conditions".tr()); + }), const TextSpan( text: ".", ), diff --git a/lib/service/navigation_service.dart b/lib/service/navigation_service.dart index 101ae9b96..1abad3814 100644 --- a/lib/service/navigation_service.dart +++ b/lib/service/navigation_service.dart @@ -20,14 +20,13 @@ import 'package:autonomy_flutter/util/constants.dart'; import 'package:autonomy_flutter/util/error_handler.dart'; import 'package:autonomy_flutter/util/inapp_notifications.dart'; import 'package:autonomy_flutter/util/log.dart'; +import 'package:autonomy_flutter/util/string_ext.dart'; import 'package:autonomy_flutter/util/style.dart'; import 'package:autonomy_flutter/util/ui_helper.dart'; import 'package:autonomy_theme/autonomy_theme.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/material.dart'; -import 'package:nft_collection/models/asset_token.dart'; - -// ignore: implementation_imports +import 'package:nft_collection/models/asset_token.dart'; // ignore: implementation_imports import 'package:overlay_support/src/overlay_state_finder.dart'; class NavigationService { @@ -361,4 +360,20 @@ class NavigationService { await UIHelper.showPostcardQRExpired(navigatorKey.currentContext!); } } + + Future openAutonomyDocument(String href, String title) async { + if (navigatorKey.currentContext != null && + navigatorKey.currentState?.mounted == true) { + final uri = Uri.parse(href.autonomyRawDocumentLink); + final document = uri.pathSegments.last; + final prefix = + uri.pathSegments.sublist(0, uri.pathSegments.length - 1).join("/"); + await Navigator.of(navigatorKey.currentContext!) + .pushNamed(AppRouter.githubDocPage, arguments: { + "prefix": "/$prefix/", + "document": document, + "title": title, + }); + } + } } diff --git a/lib/util/constants.dart b/lib/util/constants.dart index e5fcf6439..aafa71782 100644 --- a/lib/util/constants.dart +++ b/lib/util/constants.dart @@ -177,6 +177,13 @@ const TV_APP_STORE_URL = const MOMA_TERMS_CONDITIONS_URL = "https://github.com/bitmark-inc/autonomy-apps/blob/main/docs/postcard_terms.md"; +const AUTONOMY_DOCUMENT_PREFIX = "https://github.com/bitmark-inc"; + +const AUTONOMY_RAW_DOCUMENT_PREFIX = + "https://raw.githubusercontent.com/bitmark-inc"; + +const MARKDOWN_EXT = ".md"; + const String POSTCARD_SIGN_PREFIX = "Tezos Signed Message:"; const CONNECT_FAILED_DURATION = Duration(seconds: 10); diff --git a/lib/util/string_ext.dart b/lib/util/string_ext.dart index 2cc54a93c..d5cc177d2 100644 --- a/lib/util/string_ext.dart +++ b/lib/util/string_ext.dart @@ -6,6 +6,7 @@ // import 'package:autonomy_flutter/common/environment.dart'; +import 'package:autonomy_flutter/util/constants.dart'; extension StringExtension on String { String capitalize() { @@ -82,4 +83,15 @@ extension StringExtension on String { return splitted.length > 1 && splitted[1] == Environment.postcardContractAddress; } + + bool get isAutonomyDocumentLink { + return (startsWith(AUTONOMY_DOCUMENT_PREFIX) || + startsWith(AUTONOMY_RAW_DOCUMENT_PREFIX)) && + endsWith(MARKDOWN_EXT); + } + + String get autonomyRawDocumentLink { + return replaceFirst(AUTONOMY_DOCUMENT_PREFIX, AUTONOMY_RAW_DOCUMENT_PREFIX) + .replaceFirst("/blob/", "/"); + } } diff --git a/lib/view/artwork_common_widget.dart b/lib/view/artwork_common_widget.dart index 5eb44218e..48d4a5e67 100644 --- a/lib/view/artwork_common_widget.dart +++ b/lib/view/artwork_common_widget.dart @@ -9,6 +9,7 @@ import 'package:autonomy_flutter/service/configuration_service.dart'; import 'package:autonomy_flutter/service/customer_support_service.dart'; import 'package:autonomy_flutter/service/feralfile_service.dart'; import 'package:autonomy_flutter/service/metric_client_service.dart'; +import 'package:autonomy_flutter/service/navigation_service.dart'; import 'package:autonomy_flutter/util/address_utils.dart'; import 'package:autonomy_flutter/util/asset_token_ext.dart'; import 'package:autonomy_flutter/util/au_icons.dart'; @@ -1864,6 +1865,10 @@ class _PostcardRightsViewState extends State { styleSheet: markDownPostcardRightStyle(context), onTapLink: (text, href, title) async { if (href == null) return; + if (href.isAutonomyDocumentLink) { + injector() + .openAutonomyDocument(href, title); + } launchUrl(Uri.parse(href), mode: LaunchMode.externalApplication); }, From 26064cf1606a562edf0b7f0c2f3836eca095f742 Mon Sep 17 00:00:00 2001 From: PuPha Date: Wed, 18 Oct 2023 11:47:50 +0700 Subject: [PATCH 2/2] Update --- lib/view/artwork_common_widget.dart | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/view/artwork_common_widget.dart b/lib/view/artwork_common_widget.dart index 48d4a5e67..989f19d42 100644 --- a/lib/view/artwork_common_widget.dart +++ b/lib/view/artwork_common_widget.dart @@ -1867,10 +1867,11 @@ class _PostcardRightsViewState extends State { if (href == null) return; if (href.isAutonomyDocumentLink) { injector() - .openAutonomyDocument(href, title); + .openAutonomyDocument(href, text); + } else { + launchUrl(Uri.parse(href), + mode: LaunchMode.externalApplication); } - launchUrl(Uri.parse(href), - mode: LaunchMode.externalApplication); }, ), const SizedBox(height: 23.0),