Handle device default dashboard

This commit is contained in:
Igor Kulikov
2021-05-27 16:31:18 +03:00
parent 5e536ab217
commit 4516647fa8
18 changed files with 325 additions and 189 deletions

View File

@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
@@ -49,6 +50,7 @@ class _DashboardState extends TbContextState<Dashboard, _DashboardState> {
useShouldOverrideUrlLoading: true,
mediaPlaybackRequiresUserGesture: false,
javaScriptEnabled: true,
// useOnDownloadStart: true
),
android: AndroidInAppWebViewOptions(
useHybridComposition: false,
@@ -96,127 +98,144 @@ class _DashboardState extends TbContextState<Dashboard, _DashboardState> {
}
return true;
},
child: Stack(
children: [
InAppWebView(
key: webViewKey,
initialUrlRequest: URLRequest(url: Uri.parse(_dashboardUrl)),
initialOptions: options,
onWebViewCreated: (webViewController) {
webViewController.addJavaScriptHandler(handlerName: "tbMobileDashboardStateNameHandler", callback: (args) async {
log.debug("Invoked tbMobileDashboardStateNameHandler: $args");
webViewLoading.value = false;
if (args.isNotEmpty && args[0] is String) {
if (widget._titleCallback != null) {
widget._titleCallback!(args[0]);
}
}
});
webViewController.addJavaScriptHandler(handlerName: "tbMobileHandler", callback: (args) async {
log.debug("Invoked tbMobileHandler: $args");
return await widgetActionHandler.handleWidgetMobileAction(args, webViewController);
});
_controller.complete(webViewController);
},
shouldOverrideUrlLoading: (controller, navigationAction) async {
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
await launch(
uriString,
);
// and cancel the request
return NavigationActionPolicy.CANCEL;
}
}
return NavigationActionPolicy.ALLOW;
},
onUpdateVisitedHistory: (controller, url, androidIsReload) async {
if (url != null) {
String newStateId = url.pathSegments.last;
log.debug('onUpdateVisitedHistory: $newStateId');
if (newStateId == 'profile') {
webViewLoading.value = true;
await controller.goBack();
await navigateTo('/profile');
webViewLoading.value = false;
return;
} else if (newStateId == 'login') {
webViewLoading.value = true;
await controller.pauseTimers();
await controller.stopLoading();
await tbClient.logout();
return;
} else if (['devices', 'assets', 'dashboards'].contains(newStateId)) {
var controller = await _controller.future;
await controller.goBack();
navigateTo('/$newStateId');
return;
} else {
if (url.pathSegments.length > 1) {
var segmentName = url.pathSegments[url.pathSegments.length-2];
if (segmentName == 'dashboards' && widget._home != true) {
webViewLoading.value = true;
var targetPath = _createDashboardNavigationPath(newStateId, fullscreen: widget._fullscreen);
await navigateTo(targetPath, replace: true);
return;
} else if (segmentName == 'dashboard') {
_currentDashboardId = newStateId;
_currentDashboardState = url.queryParameters['state'];
return;
child: SafeArea(
child: Stack(
children: [
InAppWebView(
key: webViewKey,
initialUrlRequest: URLRequest(url: Uri.parse(_dashboardUrl)),
initialOptions: options,
onWebViewCreated: (webViewController) {
webViewController.addJavaScriptHandler(handlerName: "tbMobileDashboardStateNameHandler", callback: (args) async {
log.debug("Invoked tbMobileDashboardStateNameHandler: $args");
webViewLoading.value = false;
if (args.isNotEmpty && args[0] is String) {
if (widget._titleCallback != null) {
widget._titleCallback!(args[0]);
}
}
});
webViewController.addJavaScriptHandler(handlerName: "tbMobileHandler", callback: (args) async {
log.debug("Invoked tbMobileHandler: $args");
return await widgetActionHandler.handleWidgetMobileAction(args, webViewController);
});
_controller.complete(webViewController);
},
shouldOverrideUrlLoading: (controller, navigationAction) async {
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
await launch(
uriString,
);
// and cancel the request
return NavigationActionPolicy.CANCEL;
}
}
webViewLoading.value = true;
if (widget._home == true) {
await navigateTo('/home', replace: true);
return Platform.isIOS ? NavigationActionPolicy.ALLOW : NavigationActionPolicy.CANCEL;
},
onUpdateVisitedHistory: (controller, url, androidIsReload) async {
if (url != null) {
String newStateId = url.pathSegments.last;
log.debug('onUpdateVisitedHistory: $newStateId');
if (newStateId == 'profile') {
webViewLoading.value = true;
await controller.goBack();
await navigateTo('/profile');
webViewLoading.value = false;
return;
} else if (newStateId == 'login') {
webViewLoading.value = true;
await controller.pauseTimers();
await controller.stopLoading();
await tbClient.logout();
return;
} else if (['devices', 'assets', 'dashboards'].contains(newStateId)) {
var controller = await _controller.future;
await controller.goBack();
navigateTo('/$newStateId');
return;
} else {
if (url.pathSegments.length > 1) {
var segmentName = url.pathSegments[url.pathSegments.length-2];
if (segmentName == 'dashboards' && widget._home != true) {
webViewLoading.value = true;
var targetPath = _createDashboardNavigationPath(newStateId, fullscreen: widget._fullscreen);
await navigateTo(targetPath, replace: true);
return;
} else if (segmentName == 'dashboard') {
_currentDashboardId = newStateId;
_currentDashboardState = url.queryParameters['state'];
return;
}
}
webViewLoading.value = true;
if (widget._home == true) {
await navigateTo('/home', replace: true);
} else {
var targetPath = _createDashboardNavigationPath(_currentDashboardId, state: _currentDashboardState, fullscreen: widget._fullscreen);
await navigateTo(targetPath, replace: true);
}
}
}
},
onConsoleMessage: (controller, consoleMessage) {
log.debug('[JavaScript console] ${consoleMessage.messageLevel}: ${consoleMessage.message}');
},
onLoadStart: (controller, url) async {
log.debug('onLoadStart: $url');
// await _setTokens(controller.webStorage.localStorage);
},
onLoadStop: (controller, url) async {
log.debug('onLoadStop: $url');
// await _setTokens(controller.webStorage.localStorage);
},
androidOnPermissionRequest: (controller, origin, resources) async {
log.debug('androidOnPermissionRequest origin: $origin, resources: $resources');
return PermissionRequestResponse(
resources: resources,
action: PermissionRequestResponseAction.GRANT);
},
/* onDownloadStart: (controller, url) async {
log.debug("onDownloadStart $url");
final taskId = await FlutterDownloader.enqueue(
url: url.toString(),
savedDir: (await getExternalStorageDirectory())!.path,
showNotification: true,
openFileFromNotification: true,
);
} */
),
ValueListenableBuilder(
valueListenable: webViewLoading,
builder: (BuildContext context, bool loading, child) {
if (!loading) {
return SizedBox.shrink();
} else {
var targetPath = _createDashboardNavigationPath(_currentDashboardId, state: _currentDashboardState, fullscreen: widget._fullscreen);
await navigateTo(targetPath, replace: true);
return Container(
decoration: BoxDecoration(color: Colors.white),
child: Center(
child: RefreshProgressIndicator()
),
);
}
}
}
},
onConsoleMessage: (controller, consoleMessage) {
log.debug('[JavaScript console] ${consoleMessage.messageLevel}: ${consoleMessage.message}');
},
onLoadStart: (controller, url) async {
log.debug('onLoadStart: $url');
// await _setTokens(controller.webStorage.localStorage);
},
onLoadStop: (controller, url) async {
log.debug('onLoadStop: $url');
// await _setTokens(controller.webStorage.localStorage);
},
),
ValueListenableBuilder(
valueListenable: webViewLoading,
builder: (BuildContext context, bool loading, child) {
if (!loading) {
return SizedBox.shrink();
} else {
return Container(
decoration: BoxDecoration(color: Colors.white),
child: Center(
child: RefreshProgressIndicator()
),
);
}
}
)
]
)
],
)
)
);
}

View File

@@ -2,6 +2,8 @@ import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:thingsboard_app/constants/assets_path.dart';
import 'package:thingsboard_app/core/context/tb_context.dart';
import 'package:thingsboard_app/core/context/tb_context_widget.dart';
import 'package:thingsboard_app/core/entity/entities_base.dart';
import 'package:thingsboard_client/thingsboard_client.dart';
@@ -41,61 +43,8 @@ mixin DashboardsBase on EntitiesBase<DashboardInfo, PageLink> {
EntityCardSettings entityGridCardSettings(DashboardInfo dashboard) => EntityCardSettings(dropShadow: true); //dashboard.image != null);
@override
Widget buildEntityGridCard(BuildContext context, DashboardInfo entity) {
var hasImage = entity.image != null;
Widget image;
if (hasImage) {
var uriData = UriData.parse(entity.image!);
image = Image.memory(uriData.contentAsBytes());
} else {
image = Image.asset(ThingsboardImage.dashboardPlaceholder);
}
return
ClipRRect(
borderRadius: BorderRadius.circular(6),
child: Stack(
children: [
Positioned.fill(
child: FittedBox(
fit: BoxFit.cover,
child: image,
)
),
hasImage ? Positioned.fill(
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color(0x00000000),
Color(0xb7000000)
],
stops: [0.4219, 1]
)
)
),
) : Container(),
Positioned(
bottom: 16,
left: 16,
right: 16,
child: AutoSizeText(entity.title,
textAlign: TextAlign.center,
maxLines: 2,
minFontSize: 8,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: hasImage ? Colors.white : Color(0xFF282828),
fontWeight: FontWeight.w500,
fontSize: 14,
height: 20 / 14
),
)
)
],
)
);
Widget buildEntityGridCard(BuildContext context, DashboardInfo dashboard) {
return DashboardGridCard(tbContext, dashboard: dashboard);
}
Widget _buildEntityListCard(BuildContext context, DashboardInfo dashboard, bool listWidgetCard) {
@@ -173,3 +122,91 @@ mixin DashboardsBase on EntitiesBase<DashboardInfo, PageLink> {
}
}
class DashboardGridCard extends TbContextWidget<DashboardGridCard, _DashboardGridCardState> {
final DashboardInfo dashboard;
DashboardGridCard(TbContext tbContext, {required this.dashboard}) : super(tbContext);
@override
_DashboardGridCardState createState() => _DashboardGridCardState();
}
class _DashboardGridCardState extends TbContextState<DashboardGridCard, _DashboardGridCardState> {
_DashboardGridCardState(): super();
@override
void initState() {
super.initState();
}
@override
void didUpdateWidget(DashboardGridCard oldWidget) {
super.didUpdateWidget(oldWidget);
}
@override
Widget build(BuildContext context) {
var hasImage = widget.dashboard.image != null;
Widget image;
BoxFit imageFit;
if (hasImage) {
var uriData = UriData.parse(widget.dashboard.image!);
image = Image.memory(uriData.contentAsBytes());
imageFit = BoxFit.contain;
} else {
image = Image.asset(ThingsboardImage.dashboardPlaceholder);
imageFit = BoxFit.cover;
}
return
ClipRRect(
borderRadius: BorderRadius.circular(6),
child: Stack(
children: [
Positioned.fill(
child: FittedBox(
fit: imageFit,
child: image,
)
),
hasImage ? Positioned.fill(
child: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
colors: [
Color(0x00000000),
Color(0xb7000000)
],
stops: [0.4219, 1]
)
)
),
) : Container(),
Positioned(
bottom: 16,
left: 16,
right: 16,
child: AutoSizeText(widget.dashboard.title,
textAlign: TextAlign.center,
maxLines: 2,
minFontSize: 8,
overflow: TextOverflow.ellipsis,
style: TextStyle(
color: hasImage ? Colors.white : Color(0xFF282828),
fontWeight: FontWeight.w500,
fontSize: 14,
height: 20 / 14
),
)
)
],
)
);
}
}

View File

@@ -264,11 +264,14 @@ class _DeviceProfileCardState extends TbContextState<DeviceProfileCard, _DeviceP
var entity = widget.deviceProfile;
var hasImage = entity.image != null;
Widget image;
BoxFit imageFit;
if (hasImage) {
var uriData = UriData.parse(entity.image!);
image = Image.memory(uriData.contentAsBytes());
imageFit = BoxFit.contain;
} else {
image = Image.asset(ThingsboardImage.deviceProfilePlaceholder);
imageFit = BoxFit.cover;
}
return
ClipRRect(
@@ -277,7 +280,7 @@ class _DeviceProfileCardState extends TbContextState<DeviceProfileCard, _DeviceP
children: [
Positioned.fill(
child: FittedBox(
fit: BoxFit.cover,
fit: imageFit,
child: image,
)
),

View File

@@ -1,3 +1,5 @@
import 'dart:core';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:intl/intl.dart';
@@ -6,6 +8,7 @@ import 'package:thingsboard_app/core/context/tb_context_widget.dart';
import 'package:thingsboard_app/core/entity/entities_base.dart';
import 'package:thingsboard_app/utils/services/device_profile_cache.dart';
import 'package:thingsboard_app/utils/services/entity_query_api.dart';
import 'package:thingsboard_app/utils/utils.dart';
import 'package:thingsboard_client/thingsboard_client.dart';
mixin DevicesBase on EntitiesBase<EntityData, EntityDataQuery> {
@@ -22,8 +25,18 @@ mixin DevicesBase on EntitiesBase<EntityData, EntityDataQuery> {
}
@override
void onEntityTap(EntityData device) {
navigateTo('/device/${device.entityId.id}');
void onEntityTap(EntityData device) async {
var profile = await DeviceProfileCache.getDeviceProfileInfo(tbClient, device.field('type')!, device.entityId.id!);
if (profile.defaultDashboardId != null) {
var dashboardId = profile.defaultDashboardId!.id!;
var state = Utils.createDashboardEntityState(device.entityId, entityName: device.field('name')!, entityLabel: device.field('label')!);
navigateTo('/dashboard/$dashboardId?title=${device.field('name')!}&state=$state');
} else {
// navigateTo('/device/${device.entityId.id}');
if (tbClient.isTenantAdmin()) {
showWarnNotification('BALALAI');
}
}
}
@override
@@ -126,7 +139,7 @@ class _DeviceCardState extends TbContextState<DeviceCard, _DeviceCardState> {
children: [
Positioned.fill(
child: FittedBox(
fit: BoxFit.cover,
fit: BoxFit.contain,
child: Image.memory(uriData.contentAsBytes()),
)
),
@@ -154,7 +167,9 @@ class _DeviceCardState extends TbContextState<DeviceCard, _DeviceCardState> {
);
}
} else {
return Center(child: RefreshProgressIndicator());
return Center(child: RefreshProgressIndicator(
valueColor: AlwaysStoppedAnimation(Theme.of(tbContext.currentState!.context).colorScheme.primary)
));
}
},
),