From 7729273a8015a9cd07470e916855b003a5672f9b Mon Sep 17 00:00:00 2001 From: Igor Kulikov Date: Mon, 8 Nov 2021 17:46:41 +0200 Subject: [PATCH] Improve navigation from dashboard --- lib/modules/dashboard/dashboard.dart | 68 ++++++++++++++++------------ 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/lib/modules/dashboard/dashboard.dart b/lib/modules/dashboard/dashboard.dart index 88bcff2..2fd2a3a 100644 --- a/lib/modules/dashboard/dashboard.dart +++ b/lib/modules/dashboard/dashboard.dart @@ -230,6 +230,33 @@ class _DashboardState extends TbContextState { await controller.postWebMessage(message: webMessage, targetOrigin: Uri.parse('*')); } + Future tryLocalNavigation(String? path) async { + log.debug("path: $path"); + if (path != null) { + var parts = path.split("/"); + if ([ + 'profile', + 'devices', + 'assets', + 'dashboards', + 'dashboard', + 'customers', + 'auditLogs' + ].contains(parts[0])) { + if ((parts[0] == 'dashboard' || parts[0] == 'dashboards') && parts.length > 1) { + var dashboardId = parts[1]; + await navigateToDashboard(dashboardId); + } else if (parts[0] != 'dashboard') { + var targetPath = '/$path'; + if (parts[0] == 'devices' && widget._home != true) { + targetPath = '/devicesPage'; + } + await navigateTo(targetPath); + } + } + } + } + @override Widget build(BuildContext context) { return WillPopScope( @@ -290,22 +317,7 @@ class _DashboardState extends TbContextState { } log.debug("path: $path"); log.debug("params: $params"); - if (path != null) { - if ([ - 'profile', - 'devices', - 'assets', - 'dashboards', - 'customers', - 'auditLogs' - ].contains(path)) { - var targetPath = '/$path'; - if (path == 'devices' && widget._home != true) { - targetPath = '/devicesPage'; - } - navigateTo(targetPath); - } - } + tryLocalNavigation(path); } }); webViewController.addJavaScriptHandler(handlerName: "tbMobileHandler", callback: (args) async { @@ -317,25 +329,23 @@ class _DashboardState extends TbContextState { var uri = navigationAction.request.url!; var uriString = uri.toString(); log.debug('shouldOverrideUrlLoading $uriString'); - if (![ - "http", - "https", - "file", - "chrome", - "data", - "javascript", - "about" - ].contains(uri.scheme)) { - if (await canLaunch(uriString)) { - // Launch the App + if (Platform.isAndroid || Platform.isIOS && navigationAction.iosWKNavigationType == IOSWKNavigationType.LINK_ACTIVATED) { + if (uriString.startsWith(ThingsboardAppConstants.thingsBoardApiEndpoint)) { + var target = uriString.substring(ThingsboardAppConstants.thingsBoardApiEndpoint.length); + if (!target.startsWith("?accessToken")) { + if (target.startsWith("/")) { + target = target.substring(1); + } + await tryLocalNavigation(target); + return NavigationActionPolicy.CANCEL; + } + } else if (await canLaunch(uriString)) { await launch( uriString, ); - // and cancel the request return NavigationActionPolicy.CANCEL; } } - return Platform.isIOS ? NavigationActionPolicy.ALLOW : NavigationActionPolicy.CANCEL; }, onUpdateVisitedHistory: (controller, url, androidIsReload) async {