diff --git a/lib/bloc/home_bloc.dart b/lib/bloc/home_bloc.dart index 39bd1e2..f32e710 100644 --- a/lib/bloc/home_bloc.dart +++ b/lib/bloc/home_bloc.dart @@ -13,10 +13,10 @@ class HomeBloc extends BlocBase { APIServices apiServices = APIServices(); final allDevicesAliasMap = - StreamController>>.broadcast(); - StreamSink>> get sinkAllDevicesAliasMap => + StreamController>?>.broadcast(); + StreamSink>?> get sinkAllDevicesAliasMap => allDevicesAliasMap.sink; - Stream>> get streamAllDevicesAliasMap => + Stream>?> get streamAllDevicesAliasMap => allDevicesAliasMap.stream; // final allDevicesAliasJoinedMap = @@ -47,7 +47,6 @@ class HomeBloc extends BlocBase { Stream?> get streamAliasDevices => aliasDevices.stream; void getOwnerAndJoinedDevices(BuildContext context) async { - await apiServices.execute(context, () async { List devices = await apiServices.getDashBoardDevices().handleApiError(); List publicDevices = []; @@ -56,6 +55,8 @@ class HomeBloc extends BlocBase { publicDevices.add(device); } } + // getDeviceStatusAliasMap(publicDevices); + sinkAllDevicesAliasMap.add(null); sinkAliasDevices.add(publicDevices); if (!context.mounted) return; getOwnerDeviceState(context, publicDevices); diff --git a/lib/feature/home/home_screen.dart b/lib/feature/home/home_screen.dart index 1f86756..c16868d 100644 --- a/lib/feature/home/home_screen.dart +++ b/lib/feature/home/home_screen.dart @@ -33,10 +33,17 @@ class _HomeScreenState extends State { super.initState(); homeBloc = BlocProvider.of(context); const duration = Duration(seconds: 10); - getAllDevicesTimer = - Timer.periodic(duration, (Timer t) => homeBloc.getOwnerAndJoinedDevices(context)); + WidgetsBinding.instance.addPostFrameCallback((_) { + // Code ở đây chạy sau khi giao diện render xong + getAllDevicesTimer = + Timer.periodic(duration, (Timer t) => homeBloc.getOwnerAndJoinedDevices(context)); + // Ví dụ: gọi API, scroll tới vị trí nào đó, v.v. + }); + } + + @override void dispose() { getAllDevicesTimer?.cancel(); @@ -53,7 +60,6 @@ class _HomeScreenState extends State { homeBloc.getOwnerAndJoinedDevices(context); return const SharedLoadingAnimation(); }else{ - log("Goi else"); homeBloc.getOwnerDeviceState(context, aliasDevicesSnapshot.data ?? []); homeBloc.getDeviceStatusAliasMap(aliasDevicesSnapshot.data ?? []); checkSettingDevice(aliasDevicesSnapshot.data ?? []); @@ -210,7 +216,7 @@ class _HomeScreenState extends State { ), ), ), - StreamBuilder>>( + StreamBuilder>?>( stream: homeBloc.streamAllDevicesAliasMap, builder: (context, allDevicesAliasMapSnapshot) { if(allDevicesAliasMapSnapshot.data == null){ diff --git a/lib/feature/home/shared/alert_card.dart b/lib/feature/home/shared/alert_card.dart index cdceb22..cb5486b 100644 --- a/lib/feature/home/shared/alert_card.dart +++ b/lib/feature/home/shared/alert_card.dart @@ -117,6 +117,9 @@ Future notificationCard(BuildContext context, String notificationType, ), ], ), + SizedBox( + height: context.lowValue, + ), device.isOwner! ? Align( alignment: Alignment.centerRight, @@ -152,20 +155,6 @@ Future notificationCard(BuildContext context, String notificationType, ), ), ), - OutlinedButton( - style: const ButtonStyle( - backgroundColor: - WidgetStatePropertyAll(Colors.blueAccent), - ), - onPressed: () { - context.pushNamed(AppRoutes.DEVICE_DETAIL.name, - pathParameters: {'thingID': device.thingId!}); - }, - child: Text( - appLocalization(context).detail_message, - style: const TextStyle(color: Colors.white), - ), - ), ], ), ], diff --git a/lib/feature/home/shared/warning_card.dart b/lib/feature/home/shared/warning_card.dart index dd5f39f..c669cca 100644 --- a/lib/feature/home/shared/warning_card.dart +++ b/lib/feature/home/shared/warning_card.dart @@ -35,7 +35,7 @@ Future warningCard(BuildContext context, APIServices apiServices, } if (device.state! == 3) { backgroundColor = Colors.grey; - textColor = Colors.black; + textColor = Colors.white; message = appLocalization(context).in_progress_message; } else if (device.state! == 2) { backgroundColor = const Color.fromARGB(255, 6, 138, 72); @@ -44,32 +44,18 @@ Future warningCard(BuildContext context, APIServices apiServices, } else if (device.state! == 1) { backgroundColor = const Color.fromARGB(255, 250, 63, 63); textColor = Colors.white; + if(device.isOwner == true){ + message = appLocalization(context).button_fake_fire_message; + }else{ + message = appLocalization(context).smoke_detecting_message_lowercase; + } } else { backgroundColor = Colors.black; textColor = Colors.white; message = appLocalization(context).disconnect_message_uppercase; } - 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( + return Card( child: Padding( padding: context.paddingLow, child: Column( @@ -158,39 +144,40 @@ Future warningCard(BuildContext context, APIServices apiServices, 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), - device.isOwner! - ? Expanded( + device.isOwner == true + ? 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( @@ -268,111 +255,47 @@ Future warningCard(BuildContext context, APIServices apiServices, ), ), ), - ) - : Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - ClipPath( - clipper: SharedRocketContainer(), - child: Container( - padding: EdgeInsets.all(context.lowValue), - height: context.mediumValue, - width: context.dynamicWidth(0.22), - decoration: BoxDecoration( - color: Colors.green[300], - ), - child: Text( - appLocalization(context).interfamily_page_name, - ), - ), - ), - 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 { - await apiServices.execute(context, - () 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), - ), - ), - ), - ), - ], ), - ], - ), + ], + ) + : Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + ClipPath( + clipper: SharedRocketContainer(), + child: Container( + padding: EdgeInsets.all(context.lowValue), + height: context.mediumValue, + width: context.dynamicWidth(0.22), + decoration: BoxDecoration( + color: Colors.green[300], + ), + child: Text( + appLocalization(context).interfamily_page_name, + ), + ), + ), + SizedBox(width: context.mediumValue), + Expanded( + child: Align( + alignment: Alignment.centerRight, + child: OutlinedButton( + style: ButtonStyle( + backgroundColor: + WidgetStatePropertyAll(backgroundColor)), + onPressed: () async {}, + child: Text( + message, + style: TextStyle(color: textColor), + ), + ), + ), + ), + ], + ), ], ), ), - ), - ); + ); + }