From 54982ba777b20ecb53dce75e23feff9500e99c9c Mon Sep 17 00:00:00 2001 From: Mohammad Salameh Date: Mon, 22 Apr 2024 11:49:21 +0300 Subject: [PATCH] Configured ceiling presence sensor interface components configured CPS UI to check for the state and the connectivity of the device. reflected the data from the API to the UI --- .../ceiling_sensor_interface.dart | 43 ++++--- .../presence_sensors/parameters_list.dart | 110 ++++++++++++------ .../presence_sensors/presence_indicator.dart | 14 ++- .../wall_sensor_interface.dart | 5 +- .../view/widgets/room_page_switch.dart | 2 +- 5 files changed, 116 insertions(+), 58 deletions(-) diff --git a/lib/features/devices/view/widgets/presence_sensors/ceiling_sensor_interface.dart b/lib/features/devices/view/widgets/presence_sensors/ceiling_sensor_interface.dart index 590c234..238cdc3 100644 --- a/lib/features/devices/view/widgets/presence_sensors/ceiling_sensor_interface.dart +++ b/lib/features/devices/view/widgets/presence_sensors/ceiling_sensor_interface.dart @@ -21,6 +21,10 @@ class CeilingSensorInterface extends StatelessWidget { final DeviceModel ceilingSensor; @override Widget build(BuildContext context) { + String state = ceilingSensor.status + .firstWhere((element) => element.code == "presence_state") + .value + .toString(); return AnnotatedRegion( value: SystemUiOverlayStyle( statusBarColor: ColorsManager.primaryColor.withOpacity(0.5), @@ -62,6 +66,14 @@ class CeilingSensorInterface extends StatelessWidget { InkWell( onTap: () { if ((ceilingSensor.isOnline ?? false) == false) { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar( + content: Text( + 'Device is offline', + ), + backgroundColor: Colors.red, + ), + ); return; } String controlCode = 'sensitivity'; @@ -91,28 +103,31 @@ class CeilingSensorInterface extends StatelessWidget { ); }, child: SvgPicture.asset( - Assets.presenceSensorAssetsPresenceSensorMotion, + state == 'presence' + ? Assets.presenceSensorAssetsPresence + : Assets.presenceSensorAssetsPresenceSensorMotion, width: 100, height: 100, - colorFilter: ColorFilter.mode( - (ceilingSensor.isOnline ?? false) - ? ColorsManager.primaryColor - : Colors.grey.withOpacity(0.9), - BlendMode.srcIn, - ), + // colorFilter: ColorFilter.mode( + // (ceilingSensor.isOnline ?? false) + // ? ColorsManager.primaryColor + // : Colors.grey.withOpacity(0.9), + // BlendMode.srcIn, + // ), ), ), const SizedBox( height: 10, ), BodyMedium( - text: (ceilingSensor.isOnline ?? false) - ? StringHelpers.toTitleCase(ceilingSensor.status - .firstWhere((element) => - element.code == 'presence_state') - .value - .toString()) - : "Offline", + text: StringHelpers.toTitleCase(state), + // (ceilingSensor.isOnline ?? false) + // ? StringHelpers.toTitleCase(ceilingSensor.status + // .firstWhere((element) => + // element.code == 'presence_state') + // .value + // .toString()) + // : "Offline", style: context.bodyMedium.copyWith( fontWeight: FontsManager.bold, ), diff --git a/lib/features/devices/view/widgets/presence_sensors/parameters_list.dart b/lib/features/devices/view/widgets/presence_sensors/parameters_list.dart index 621ec5b..f155064 100644 --- a/lib/features/devices/view/widgets/presence_sensors/parameters_list.dart +++ b/lib/features/devices/view/widgets/presence_sensors/parameters_list.dart @@ -210,49 +210,43 @@ class ParametersList extends StatelessWidget { ); } +//{"result":{"productId":"awarhusb","productType":"WPS","status":[{"code":"presence_state","value":"none"},{"code":"far_detection","value":75},{"code":"presence_time","value":0},{"code":"motion_sensitivity_value","value":5},{"code":"motionless_sensitivity","value":5},{"code":"dis_current","value":214},{"code":"illuminance_value","value":231},{"code":"indicator","value":true}]},"success":true} final List> wallSensorButtons = const [ { 'icon': Assets.presenceSensorAssetsTime, 'title': 'Presence Time', - 'value': 0, - 'unit': 'min', - 'page': null, + 'code': 'presence_time', }, { 'icon': Assets.presenceSensorAssetsDistance, 'title': 'Current Distance', - 'value': 279, - 'unit': 'cm', - 'dialog': null, + 'code': 'dis_current', }, { 'icon': Assets.presenceSensorAssetsIlluminanceValue, 'title': 'Illuminance Value', - 'value': 0, - 'unit': 'Lux', - 'page': null, + 'code': 'illuminance_value', }, { 'icon': Assets.presenceSensorAssetsEmpty, 'title': 'Nobody Time', - 'value': 10, - 'unit': 'sec', - 'dialog': null, + 'code': null, + //TODO: Implement the nobody time }, { 'icon': Assets.presenceSensorAssetsIndicator, 'title': 'Indicator', - 'page': null, + 'code': 'indicator', }, { 'icon': Assets.presenceSensorAssetsRecord, 'title': 'Presence Record', - 'page': null, + 'code': null }, { 'icon': Assets.presenceSensorAssetsIlluminanceRecord, 'title': 'Illuminance Record', - 'page': null, + 'code': null }, ]; } @@ -262,6 +256,18 @@ Widget listItem( BuildContext context, DeviceModel wallSensor, ) { + String? unit; + dynamic value; + if (wallSensorButton['code'] != null) { + if (wallSensor.status + .any((element) => element.code == wallSensorButton['code'] as String)) { + unit = unitsMap[wallSensorButton['code'] as String]; + value = wallSensor.status + .firstWhere( + (element) => element.code == wallSensorButton['code'] as String) + .value; + } + } return DefaultContainer( margin: const EdgeInsets.only(bottom: 5), padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 20), @@ -283,30 +289,49 @@ Widget listItem( const SizedBox( width: 25, ), - BodyMedium(text: wallSensorButton['title'] as String), - if (wallSensorButton['value'] != null) const Spacer(), - if (wallSensorButton['value'] != null) - BodyMedium( - text: '${wallSensorButton['value']}${wallSensorButton['unit']}', - style: context.bodyMedium.copyWith(color: ColorsManager.greyColor), - ), - if (wallSensorButton['value'] != null) - if (wallSensorButton['title'] == 'Indicator') - Expanded( - child: Row( - mainAxisAlignment: MainAxisAlignment.end, - children: [ - Transform.scale( - scale: .8, - child: CupertinoSwitch( - value: false, - onChanged: (value) {}, - applyTheme: true, - )), - ], - ), + BodyMedium( + text: wallSensorButton['title'] as String, + ), + if (wallSensorButton['code'] != null) const Spacer(), + if (wallSensorButton['code'] != null) + if (wallSensorButton['title'] != 'Indicator') + BodyMedium( + text: '${value ?? 'N/A'}${unit ?? ''}', + style: + context.bodyMedium.copyWith(color: ColorsManager.greyColor), ), - // CustomSwitch(device: wallSensor), + if (wallSensorButton['title'] == 'Indicator') + Expanded( + child: Row( + mainAxisAlignment: MainAxisAlignment.end, + children: [ + Transform.scale( + scale: .8, + child: CupertinoSwitch( + value: value ?? false, + onChanged: (value) { + if (wallSensor.isOnline ?? false) { + DevicesCubit.getInstance().deviceControl( + DeviceControlModel( + deviceId: wallSensor.id, + code: 'indicator', + value: value, + ), + wallSensor.id ?? ''); + } else { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar( + content: Text('Device is offline'), + backgroundColor: Colors.red, + ), + ); + } + }, + applyTheme: true, + )), + ], + ), + ), if (wallSensorButton['title'] == 'Nobody Time') const Row( mainAxisAlignment: MainAxisAlignment.end, @@ -322,3 +347,12 @@ Widget listItem( ), ); } + +Map unitsMap = { + 'presence_time': 's', + 'dis_current': 'cm', + 'illuminance_value': 'lux', + 'far_detection': 'cm', + 'motion_sensitivity_value': '', + 'motionless_sensitivity': '', +}; diff --git a/lib/features/devices/view/widgets/presence_sensors/presence_indicator.dart b/lib/features/devices/view/widgets/presence_sensors/presence_indicator.dart index 39c4d42..ad21528 100644 --- a/lib/features/devices/view/widgets/presence_sensors/presence_indicator.dart +++ b/lib/features/devices/view/widgets/presence_sensors/presence_indicator.dart @@ -1,17 +1,23 @@ part of "wall_sensor_interface.dart"; class PresenceIndicator extends StatelessWidget { - const PresenceIndicator({super.key}); - + const PresenceIndicator({super.key, required this.wallSensor}); + final DeviceModel wallSensor; @override Widget build(BuildContext context) { + String state = wallSensor.status + .firstWhere((element) => element.code == "presence_state") + .value + .toString(); return Expanded( flex: 6, child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ SvgPicture.asset( - Assets.presenceSensorAssetsPresence, + state == 'presence' + ? Assets.presenceSensorAssetsPresence + : Assets.presenceSensorAssetsPresenceSensorMotion, width: 100, height: 100, ), @@ -19,7 +25,7 @@ class PresenceIndicator extends StatelessWidget { height: 10, ), BodyMedium( - text: 'Presence', + text: StringHelpers.toTitleCase(state), style: context.bodyMedium.copyWith(fontWeight: FontsManager.bold), ), ], diff --git a/lib/features/devices/view/widgets/presence_sensors/wall_sensor_interface.dart b/lib/features/devices/view/widgets/presence_sensors/wall_sensor_interface.dart index a9d6d0d..5c1ee4f 100644 --- a/lib/features/devices/view/widgets/presence_sensors/wall_sensor_interface.dart +++ b/lib/features/devices/view/widgets/presence_sensors/wall_sensor_interface.dart @@ -12,6 +12,7 @@ import 'package:syncrow_app/features/shared_widgets/text_widgets/body_small.dart import 'package:syncrow_app/features/shared_widgets/text_widgets/title_medium.dart'; import 'package:syncrow_app/generated/assets.dart'; import 'package:syncrow_app/utils/context_extension.dart'; +import 'package:syncrow_app/utils/helpers/misc_string_helpers.dart'; import 'package:syncrow_app/utils/resource_manager/color_manager.dart'; import 'package:syncrow_app/utils/resource_manager/constants.dart'; import 'package:syncrow_app/utils/resource_manager/font_manager.dart'; @@ -59,7 +60,9 @@ class WallMountedInterface extends StatelessWidget { ), child: Column( children: [ - const PresenceIndicator(), + PresenceIndicator( + wallSensor: wallSensor, + ), ParametersList(wallSensor: wallSensor), ], ), diff --git a/lib/features/devices/view/widgets/room_page_switch.dart b/lib/features/devices/view/widgets/room_page_switch.dart index ed21e5c..49588ab 100644 --- a/lib/features/devices/view/widgets/room_page_switch.dart +++ b/lib/features/devices/view/widgets/room_page_switch.dart @@ -12,7 +12,7 @@ import 'package:syncrow_app/features/devices/model/device_model.dart'; import 'package:syncrow_app/features/devices/view/widgets/ACs/ac_interface.dart'; import 'package:syncrow_app/features/devices/view/widgets/lights/light_interface.dart'; -import 'package:syncrow_app/features/devices/view/widgets/presence_sensor/wall_sensor_interface.dart'; +import 'package:syncrow_app/features/devices/view/widgets/presence_sensors/wall_sensor_interface.dart'; import 'package:syncrow_app/features/devices/view/widgets/presence_sensors/ceiling_sensor_interface.dart';