diff --git a/lib/bloc/device_logs_bloc.dart b/lib/bloc/device_logs_bloc.dart index cc78b3d..2f586a5 100644 --- a/lib/bloc/device_logs_bloc.dart +++ b/lib/bloc/device_logs_bloc.dart @@ -1,12 +1,11 @@ import 'dart:async'; import 'dart:convert'; -import 'dart:developer'; -import 'package:sfm_app/feature/devices/device_model.dart'; -import 'package:sfm_app/product/base/bloc/base_bloc.dart'; -import 'package:sfm_app/product/constant/app/app_constants.dart'; -import 'package:sfm_app/product/services/api_services.dart'; -import 'package:sfm_app/product/utils/date_time_utils.dart'; +import '../feature/devices/device_model.dart'; +import '../product/base/bloc/base_bloc.dart'; +import '../product/constant/app/app_constants.dart'; +import '../product/services/api_services.dart'; +import '../product/utils/date_time_utils.dart'; import '../product/utils/device_utils.dart'; import '../feature/device_log/device_logs_model.dart'; diff --git a/lib/feature/device_log/device_logs_screen.dart b/lib/feature/device_log/device_logs_screen.dart index b93e2cd..6f01ee9 100644 --- a/lib/feature/device_log/device_logs_screen.dart +++ b/lib/feature/device_log/device_logs_screen.dart @@ -1,4 +1,3 @@ -import 'dart:developer'; import 'package:dropdown_button2/dropdown_button2.dart'; import 'package:flutter/material.dart'; diff --git a/lib/feature/home/home_screen.dart b/lib/feature/home/home_screen.dart index e317aad..7726ddb 100644 --- a/lib/feature/home/home_screen.dart +++ b/lib/feature/home/home_screen.dart @@ -94,61 +94,59 @@ class _HomeScreenState extends State { if (snapshot.data?['state'] != null || snapshot.data?['battery'] != null) { return Row( - mainAxisAlignment: MainAxisAlignment.start, - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - if (snapshot.data?['state'] != null) - ...snapshot.data!['state']! - .map( - (item) => FutureBuilder( - future: warningCard( - context, apiServices, item), - builder: - (context, warningCardSnapshot) { - if (warningCardSnapshot.hasData) { - return ConstrainedBox( - constraints: const BoxConstraints( - maxWidth: 400, - maxHeight: 260, - ), - child: warningCardSnapshot.data!, - ); - } else { - return const SizedBox.shrink(); - } - }, + mainAxisAlignment: MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + if (snapshot.data?['state'] != null) + ...snapshot.data!['state']! + .map( + (item) => FutureBuilder( + future: warningCard( + context, apiServices, item), + builder: (context, warningCardSnapshot) { + if (warningCardSnapshot.hasData) { + return ConstrainedBox( + constraints: const BoxConstraints( + maxWidth: 400, + maxHeight: 260, + ), + child: warningCardSnapshot.data!, + ); + } else { + return const SizedBox.shrink(); + } + }, + ), + ) + .toList(), + if (snapshot.data?['battery'] != null) + ...snapshot.data!['battery']! + .map( + (batteryItem) => FutureBuilder( + future: notificationCard( + context, + "lowBattery", + "Cảnh báo pin yếu", + batteryItem ), - ) - .toList(), - if (snapshot.data?['battery'] != null) - ...snapshot.data!['battery']! - .map( - (batteryItem) => FutureBuilder( - future: notificationCard( - context, - "lowBattery", - "Cảnh báo pin yếu", - batteryItem.name!, - batteryItem.areaPath!, - ), - builder: - (context, warningCardSnapshot) { - if (warningCardSnapshot.hasData) { - return ConstrainedBox( - constraints: const BoxConstraints( - maxWidth: 400, - maxHeight: 260, - ), - child: warningCardSnapshot.data!, - ); - } else { - return const SizedBox.shrink(); - } - }, - ), - ) - .toList(), - ]); + builder: (context, warningCardSnapshot) { + if (warningCardSnapshot.hasData) { + return ConstrainedBox( + constraints: const BoxConstraints( + maxWidth: 400, + maxHeight: 260, + ), + child: warningCardSnapshot.data!, + ); + } else { + return const SizedBox.shrink(); + } + }, + ), + ) + .toList(), + ], + ); } else { return Padding( padding: context.paddingMedium, @@ -355,13 +353,14 @@ class _HomeScreenState extends State { } } } - checkSettingdevice(allDevicesAliasJoined); + // checkSettingdevice(allDevicesAliasJoined); homeBloc.sinkAllDevicesAlias.add(allDevicesAlias); homeBloc.sinkAllDevicesAliasJoined.add(allDevicesAliasJoined); } void checkSettingdevice(List devices) async { if (isFunctionCall) { + log("Ham check setting da duoc goi"); } else { String? response = await apiServices.getAllSettingsNotificationOfDevices(); @@ -380,7 +379,7 @@ class _HomeScreenState extends State { await apiServices.setupDeviceNotification( device.thingId!, device.name!); } else { - log("All devives are in the notification settings list."); + log("All devices are in the notification settings list."); } } } else { diff --git a/lib/feature/home/shared/alert_card.dart b/lib/feature/home/shared/alert_card.dart index b5551cc..4ef5067 100644 --- a/lib/feature/home/shared/alert_card.dart +++ b/lib/feature/home/shared/alert_card.dart @@ -1,6 +1,9 @@ // ignore_for_file: use_build_context_synchronously import 'package:flutter/material.dart'; +import 'package:go_router/go_router.dart'; +import 'package:sfm_app/feature/home/device_alias_model.dart'; +import '../../../product/constant/enums/app_route_enums.dart'; import '../../../product/constant/image/image_constants.dart'; import '../../../product/extention/context_extention.dart'; import '../../../product/services/language_services.dart'; @@ -8,14 +11,13 @@ import '../../../product/utils/device_utils.dart'; import '../../../product/constant/icon/icon_constants.dart'; -Future notificationCard( - BuildContext context, - String notiticationType, - String notificationTitle, - String notificationDevicename, - String notificationLocation) async { - String location = await DeviceUtils.instance - .getFullDeviceLocation(context, notificationLocation); +Future notificationCard(BuildContext context, String notiticationType, + String notificationTitle, DeviceWithAlias device) async { + String location = ""; + if (device.areaPath != "") { + location = await DeviceUtils.instance + .getFullDeviceLocation(context, device.areaPath!); + } String path = ""; DateTime time = DateTime.now(); if (notiticationType == "lowBattery") { @@ -50,7 +52,7 @@ Future notificationCard( SizedBox(height: context.lowValue), SizedBox( child: Text( - "${appLocalization(context).device_title} $notificationDevicename", + "${appLocalization(context).device_title} ${device.isOwner! ? device.name : device.alias}", style: const TextStyle( fontWeight: FontWeight.bold, fontSize: 18, @@ -113,8 +115,12 @@ Future notificationCard( alignment: Alignment.centerRight, child: OutlinedButton( style: const ButtonStyle( - backgroundColor: WidgetStatePropertyAll(Colors.blueAccent)), - onPressed: () {}, + backgroundColor: WidgetStatePropertyAll(Colors.blueAccent), + ), + onPressed: () { + context.pushNamed(AppRoutes.DEVICE_DETAIL.name, + pathParameters: {'thingID': device.thingId!}); + }, child: Text( appLocalization(context).detail_message, style: const TextStyle( diff --git a/lib/feature/home/shared/warning_card.dart b/lib/feature/home/shared/warning_card.dart index db44de8..c6784e8 100644 --- a/lib/feature/home/shared/warning_card.dart +++ b/lib/feature/home/shared/warning_card.dart @@ -3,6 +3,7 @@ import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; import 'package:maps_launcher/maps_launcher.dart'; +import 'package:badges/badges.dart' as badges; import '../device_alias_model.dart'; import '../../../product/constant/icon/icon_constants.dart'; import '../../../product/constant/image/image_constants.dart'; @@ -12,30 +13,33 @@ import '../../../product/services/language_services.dart'; import '../../../product/utils/device_utils.dart'; import '../../../product/shared/shared_snack_bar.dart'; -Future warningCard( - BuildContext context, APIServices apiServices, DeviceWithAlias item) async { +Future warningCard(BuildContext context, APIServices apiServices, + DeviceWithAlias device) async { Color backgroundColor = Colors.blue; Color textColor = Colors.white; String message = ""; - String fullLocation = - await DeviceUtils.instance.getFullDeviceLocation(context, item.areaPath!); + String fullLocation = ""; + if (device.areaPath != "") { + fullLocation = await DeviceUtils.instance + .getFullDeviceLocation(context, device.areaPath!); + } String time = ""; - for (var sensor in item.status!.sensors!) { + for (var sensor in device.status!.sensors!) { if (sensor.name! == "11") { DateTime dateTime = DateTime.fromMillisecondsSinceEpoch((sensor.time!) * 1000); time = DateFormat('yyyy-MM-dd HH:mm:ss').format(dateTime); } } - if (item.state! == 3) { + if (device.state! == 3) { backgroundColor = Colors.grey; textColor = Colors.black; message = appLocalization(context).in_progress_message; - } else if (item.state! == 2) { + } else if (device.state! == 2) { backgroundColor = const Color.fromARGB(255, 6, 138, 72); textColor = const Color.fromARGB(255, 255, 255, 255); message = appLocalization(context).gf_in_firefighting_message; - } else if (item.state! == 1) { + } else if (device.state! == 1) { backgroundColor = const Color.fromARGB(255, 250, 63, 63); textColor = Colors.white; message = appLocalization(context).button_fake_fire_message; @@ -44,204 +48,224 @@ Future warningCard( textColor = Colors.white; message = appLocalization(context).disconnect_message_uppercase; } - return Card( - // color: Color.fromARGB(255, 208, 212, 217), - child: Padding( - padding: context.paddingLow, - child: Column( - mainAxisAlignment: MainAxisAlignment.start, - crossAxisAlignment: CrossAxisAlignment.start, - mainAxisSize: MainAxisSize.min, - children: [ - Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - Column( - mainAxisAlignment: MainAxisAlignment.start, - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - SizedBox( - child: Text( - appLocalization(context).smoke_detecting_message, - style: const TextStyle( - letterSpacing: 1, - fontWeight: FontWeight.bold, - color: Colors.red, - fontSize: 18, + return badges.Badge( + badgeAnimation: const badges.BadgeAnimation.fade(), + position: badges.BadgePosition.bottomStart(bottom: 15, start: 10), + badgeContent: device.isOwner! + ? null + : Text( + appLocalization(context).interfamily_page_name, + style: TextStyle( + fontSize: 20, + color: Theme.of(context).colorScheme.onSurface, + fontWeight: FontWeight.bold), + ), + badgeStyle: badges.BadgeStyle( + shape: badges.BadgeShape.square, + borderRadius: BorderRadius.circular(10), + borderSide: const BorderSide(color: Colors.white, width: 2), + badgeColor: Theme.of(context).colorScheme.surfaceDim, + padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2), + ), + child: Card( + child: Padding( + padding: context.paddingLow, + child: Column( + mainAxisAlignment: MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.start, + mainAxisSize: MainAxisSize.min, + children: [ + Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Column( + mainAxisAlignment: MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + SizedBox( + child: Text( + appLocalization(context).smoke_detecting_message, + style: const TextStyle( + letterSpacing: 1, + fontWeight: FontWeight.bold, + color: Colors.red, + fontSize: 18, + ), + ), + ), + SizedBox(height: context.lowValue), + SizedBox( + child: Text( + "${appLocalization(context).device_title}: ${device.isOwner! ? device.name : device.alias}", + style: const TextStyle( + fontWeight: FontWeight.bold, + fontSize: 18, + ), + maxLines: 2, + overflow: TextOverflow.ellipsis, + softWrap: true, + textAlign: TextAlign.start, + ), + ), + ], + ), + SizedBox( + height: context.dynamicWidth(0.15), + width: context.dynamicWidth(0.15), + child: Image.asset( + ImageConstants.instance.getImage("fire_warning")), + ) + ], + ), + SizedBox(height: context.lowValue), + Row( + children: [ + IconConstants.instance + .getMaterialIcon(Icons.location_on_outlined), + SizedBox( + width: context.lowValue, + ), + Expanded( + child: Text( + fullLocation, + style: const TextStyle(fontSize: 15), + maxLines: 2, + overflow: TextOverflow.ellipsis, + softWrap: true, + textAlign: TextAlign.start, + ), + ), + ], + ), + SizedBox(height: context.lowValue), + Row( + children: [ + IconConstants.instance.getMaterialIcon(Icons.schedule), + SizedBox( + width: context.lowValue, + ), + Expanded( + child: Text( + time, + style: const TextStyle(fontSize: 15), + maxLines: 2, + overflow: TextOverflow.ellipsis, + softWrap: true, + textAlign: TextAlign.start, + ), + ), + ], + ), + SizedBox( + height: context.lowValue, + ), + Row( + mainAxisAlignment: MainAxisAlignment.start, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + IconButton.outlined( + onPressed: () async => {}, + // displayListOfFireStationPhoneNumbers(testDevice), + icon: IconConstants.instance.getMaterialIcon(Icons.call), + iconSize: 25, + style: ButtonStyle( + backgroundColor: + WidgetStateProperty.all(Colors.blue[300]!), + ), + ), + const SizedBox(width: 10), + IconButton.outlined( + onPressed: () async { + String markerLabel = "Destination"; + MapsLauncher.launchCoordinates( + double.parse(device.settings!.latitude!), + double.parse(device.settings!.longitude!), + markerLabel); + }, + icon: const Icon(Icons.directions), + iconSize: 25, + style: ButtonStyle( + backgroundColor: + WidgetStateProperty.all(Colors.blue[300]!), + ), + ), + SizedBox(width: context.mediumValue), + Expanded( + child: Align( + alignment: Alignment.centerRight, + child: OutlinedButton( + style: ButtonStyle( + backgroundColor: + WidgetStatePropertyAll(backgroundColor)), + onPressed: () async { + if (message == + appLocalization(context).button_fake_fire_message) { + await showDialog( + context: context, + builder: (context) => AlertDialog( + icon: const Icon(Icons.warning), + iconColor: Colors.red, + title: Text(appLocalization(context) + .confirm_fake_fire_message), + content: Text(appLocalization(context) + .confirm_fake_fire_body), + actions: [ + TextButton( + onPressed: () async { + int statusCode = await apiServices + .confirmFakeFireByUser(device.thingId!); + if (statusCode == 200) { + showNoIconTopSnackBar( + context, + appLocalization(context) + .notification_confirm_fake_fire_success, + Colors.green, + Colors.white); + } else { + showNoIconTopSnackBar( + context, + appLocalization(context) + .notification_confirm_fake_fire_failed, + Colors.red, + Colors.red); + } + Navigator.of(context).pop(); + }, + child: Text( + appLocalization(context) + .confirm_fake_fire_sure_message, + style: + const TextStyle(color: Colors.red)), + ), + TextButton( + onPressed: () { + Navigator.of(context).pop(); + }, + child: Text(appLocalization(context) + .cancel_button_content), + ), + ], + ), + ); + } else { + showNoIconTopSnackBar( + context, + appLocalization(context).let_PCCC_handle_message, + Colors.orange, + Colors.white); + } + }, + child: Text( + message, + style: TextStyle(color: textColor), ), ), ), - SizedBox(height: context.lowValue), - SizedBox( - child: Text( - "${appLocalization(context).device_title}: ${item.name}", - style: const TextStyle( - fontWeight: FontWeight.bold, - fontSize: 18, - ), - maxLines: 2, - overflow: TextOverflow.ellipsis, - softWrap: true, - textAlign: TextAlign.start, - ), - ), - ], - ), - SizedBox( - height: context.dynamicWidth(0.15), - width: context.dynamicWidth(0.15), - child: Image.asset( - ImageConstants.instance.getImage("fire_warning")), - ) - ], - ), - SizedBox(height: context.lowValue), - Row( - children: [ - IconConstants.instance - .getMaterialIcon(Icons.location_on_outlined), - SizedBox( - width: context.lowValue, - ), - Expanded( - child: Text( - fullLocation, - style: const TextStyle(fontSize: 15), - maxLines: 2, - overflow: TextOverflow.ellipsis, - softWrap: true, - textAlign: TextAlign.start, ), - ), - ], - ), - SizedBox(height: context.lowValue), - Row( - children: [ - IconConstants.instance.getMaterialIcon(Icons.schedule), - SizedBox( - width: context.lowValue, - ), - Expanded( - child: Text( - time, - style: const TextStyle(fontSize: 15), - maxLines: 2, - overflow: TextOverflow.ellipsis, - softWrap: true, - textAlign: TextAlign.start, - ), - ), - ], - ), - SizedBox( - height: context.lowValue, - ), - Row( - mainAxisAlignment: MainAxisAlignment.start, - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - IconButton.outlined( - onPressed: () async => {}, - // displayListOfFireStationPhoneNumbers(testDevice), - icon: IconConstants.instance.getMaterialIcon(Icons.call), - iconSize: 25, - style: ButtonStyle( - backgroundColor: - WidgetStateProperty.all(Colors.blue[300]!), - ), - ), - const SizedBox(width: 10), - IconButton.outlined( - onPressed: () async { - String markerLabel = "Destination"; - MapsLauncher.launchCoordinates( - double.parse(item.settings!.latitude!), - double.parse(item.settings!.longitude!), - markerLabel); - }, - icon: const Icon(Icons.directions), - iconSize: 25, - style: ButtonStyle( - backgroundColor: - WidgetStateProperty.all(Colors.blue[300]!), - ), - ), - SizedBox(width: context.mediumValue), - Expanded( - child: Align( - alignment: Alignment.centerRight, - child: OutlinedButton( - style: ButtonStyle( - backgroundColor: - WidgetStatePropertyAll(backgroundColor)), - onPressed: () async { - if (message == - appLocalization(context).button_fake_fire_message) { - await showDialog( - context: context, - builder: (context) => AlertDialog( - icon: const Icon(Icons.warning), - iconColor: Colors.red, - title: Text(appLocalization(context) - .confirm_fake_fire_message), - content: Text(appLocalization(context) - .confirm_fake_fire_body), - actions: [ - TextButton( - onPressed: () async { - int statusCode = await apiServices - .confirmFakeFireByUser(item.thingId!); - if (statusCode == 200) { - showNoIconTopSnackBar( - context, - appLocalization(context) - .notification_confirm_fake_fire_success, - Colors.green, - Colors.white); - } else { - showNoIconTopSnackBar( - context, - appLocalization(context) - .notification_confirm_fake_fire_failed, - Colors.red, - Colors.red); - } - Navigator.of(context).pop(); - }, - child: Text( - appLocalization(context) - .confirm_fake_fire_sure_message, - style: const TextStyle(color: Colors.red)), - ), - TextButton( - onPressed: () { - Navigator.of(context).pop(); - }, - child: Text(appLocalization(context) - .cancel_button_content), - ), - ], - ), - ); - } else { - showNoIconTopSnackBar( - context, - appLocalization(context).let_PCCC_handle_message, - Colors.orange, - Colors.white); - } - }, - child: Text( - message, - style: TextStyle(color: textColor), - ), - ), - ), - ), - ], - ), - ], + ], + ), + ], + ), ), ), ); diff --git a/lib/product/utils/device_utils.dart b/lib/product/utils/device_utils.dart index b62bcf1..473a97a 100644 --- a/lib/product/utils/device_utils.dart +++ b/lib/product/utils/device_utils.dart @@ -329,4 +329,5 @@ class DeviceUtils { return Colors.red; } } + }