From 677a7c4d4a8ad095d41d98f9e7b80fd780d78b7c Mon Sep 17 00:00:00 2001 From: anhtunz Date: Mon, 26 May 2025 14:08:30 +0700 Subject: [PATCH] chore(HomeScreen): fix context unmounted --- lib/bloc/inter_family_bloc.dart | 1 - .../device_detail/device_detail_screen.dart | 1 + lib/feature/home/home_screen.dart | 120 +++++++++--------- .../inter_family/groups/groups_screen.dart | 2 +- lib/product/lang/l10n/app_en.arb | 2 + lib/product/lang/l10n/app_vi.arb | 2 + 6 files changed, 69 insertions(+), 59 deletions(-) diff --git a/lib/bloc/inter_family_bloc.dart b/lib/bloc/inter_family_bloc.dart index 2d7731d..389afe8 100644 --- a/lib/bloc/inter_family_bloc.dart +++ b/lib/bloc/inter_family_bloc.dart @@ -45,7 +45,6 @@ class InterFamilyBloc extends BlocBase { try { final data = jsonDecode(body); - // Kiểm tra nếu data là một Map và có chứa key "items" if (data is Map && data.containsKey("items") && data["items"] is List) { List items = data["items"]; groups = Group.fromJsonDynamicList(items); diff --git a/lib/feature/devices/device_detail/device_detail_screen.dart b/lib/feature/devices/device_detail/device_detail_screen.dart index 6a6c0e9..f27f3ae 100644 --- a/lib/feature/devices/device_detail/device_detail_screen.dart +++ b/lib/feature/devices/device_detail/device_detail_screen.dart @@ -69,6 +69,7 @@ class _DetailDeviceScreenState extends State { @override void dispose() { getDeviceDetailTimer?.cancel(); + super.dispose(); } BoxDecoration boxDecoration = BoxDecoration( diff --git a/lib/feature/home/home_screen.dart b/lib/feature/home/home_screen.dart index f0a66a1..4d035be 100644 --- a/lib/feature/home/home_screen.dart +++ b/lib/feature/home/home_screen.dart @@ -230,42 +230,45 @@ class _HomeScreenState extends State { length: 2, child: Column( children: [ - const TabBar( + TabBar( tabs: [ - Tab(text: 'Owner Devices'), - Tab(text: 'Joined Devices'), + Tab(text: appLocalization(context).over_view_owner_devices), + Tab(text: appLocalization(context).over_view_joined_devices), ], labelColor: Colors.blue, unselectedLabelColor: Colors.grey, indicatorColor: Colors.blue, ), - TabBarView( - children: [ - OverviewCard( - isOwner: true, - total: data['all']?.length ?? 0, - active: data['online']?.length ?? 0, - inactive: - data['offline']?.length ?? 0, - warning: data['warn']?.length ?? 0, - unused: - data['not-use']?.length ?? 0), - OverviewCard( - isOwner: false, - total: - dataJoined['all']?.length ?? 0, - active: - dataJoined['online']?.length ?? - 0, - inactive: - dataJoined['offline']?.length ?? - 0, - warning: - dataJoined['warn']?.length ?? 0, - unused: - dataJoined['not-use']?.length ?? - 0), - ], + SizedBox( + height: context.dynamicHeight(0.6), + child: TabBarView( + children: [ + OverviewCard( + isOwner: true, + total: data['all']?.length ?? 0, + active: data['online']?.length ?? 0, + inactive: + data['offline']?.length ?? 0, + warning: data['warn']?.length ?? 0, + unused: + data['not-use']?.length ?? 0), + OverviewCard( + isOwner: false, + total: + dataJoined['all']?.length ?? 0, + active: + dataJoined['online']?.length ?? + 0, + inactive: + dataJoined['offline']?.length ?? + 0, + warning: + dataJoined['warn']?.length ?? 0, + unused: + dataJoined['not-use']?.length ?? + 0,), + ], + ), ), ], ), @@ -302,40 +305,43 @@ class _HomeScreenState extends State { } void getOwnerDeviceState(List allDevices) async { - List ownerDevices = []; + ownerDevicesState.clear(); + ownerDevicesStatus.clear(); + + if (!mounted) return; + homeBloc.sinkOwnerDevicesStatus.add(ownerDevicesStatus); + + int count = 0; for (var device in allDevices) { - if (device.isOwner!) { - ownerDevices.add(device); + if (device.isOwner != true) continue; + + if (!mounted) return; + Map sensorMap = DeviceUtils.instance + .getDeviceSensors(context, device.status?.sensors ?? []); + + if (device.state == 1 || device.state == 3) { + ownerDevicesStatus["state"] ??= []; + ownerDevicesStatus["state"]!.add(device); + if (!mounted) return; + homeBloc.sinkOwnerDevicesStatus.add(ownerDevicesStatus); + count++; } - } - if (ownerDevicesState.isEmpty || - ownerDevicesState.length < devices.length) { - ownerDevicesState.clear(); - ownerDevicesStatus.clear(); - homeBloc.sinkOwnerDevicesStatus.add(ownerDevicesStatus); - int count = 0; - for (var device in ownerDevices) { - Map sensorMap = DeviceUtils.instance - .getDeviceSensors(context, device.status?.sensors ?? []); - if (device.state == 1 || device.state == 3) { - ownerDevicesStatus["state"] ??= []; - ownerDevicesStatus["state"]!.add(device); + + final noDataMessage = appLocalization(context).no_data_message; + if (sensorMap['sensorBattery'] != noDataMessage) { + if (double.parse(sensorMap['sensorBattery']) <= 20) { + ownerDevicesStatus['battery'] ??= []; + ownerDevicesStatus['battery']!.add(device); + if (!mounted) return; homeBloc.sinkOwnerDevicesStatus.add(ownerDevicesStatus); count++; } - if (sensorMap['sensorBattery'] != - appLocalization(context).no_data_message) { - if (double.parse(sensorMap['sensorBattery']) <= 20) { - ownerDevicesStatus['battery'] ??= []; - ownerDevicesStatus['battery']!.add(device); - homeBloc.sinkOwnerDevicesStatus.add(ownerDevicesStatus); - count++; - } - } - notificationCount = count; - homeBloc.sinkCountNotification.add(notificationCount); } } + + notificationCount = count; + if (!mounted) return; + homeBloc.sinkCountNotification.add(notificationCount); } void getDeviceStatusAliasMap(List devices) { diff --git a/lib/feature/inter_family/groups/groups_screen.dart b/lib/feature/inter_family/groups/groups_screen.dart index 755db00..c4078d1 100644 --- a/lib/feature/inter_family/groups/groups_screen.dart +++ b/lib/feature/inter_family/groups/groups_screen.dart @@ -34,7 +34,7 @@ class _GroupsScreenState extends State { void initState() { super.initState(); interFamilyBloc = BlocProvider.of(context); - const duration = Duration(seconds: 10); + const duration = Duration(seconds: 5); getAllGroupsTimer = Timer.periodic( duration, (Timer t) => interFamilyBloc.getAllGroup(widget.role), diff --git a/lib/product/lang/l10n/app_en.arb b/lib/product/lang/l10n/app_en.arb index 733c864..c49a784 100644 --- a/lib/product/lang/l10n/app_en.arb +++ b/lib/product/lang/l10n/app_en.arb @@ -22,6 +22,8 @@ "let_PCCC_handle_message": "Let the Fire Prevention and Fighting Team handle it!", "overview_message": "Overview", "total_nof_devices_message": "Total number of devices", + "over_view_owner_devices":"Owner Devices", + "over_view_joined_devices":"Joined Devices", "active_devices_message": "Active", "inactive_devices_message": "Inactive", "warning_devices_message": "Warning", diff --git a/lib/product/lang/l10n/app_vi.arb b/lib/product/lang/l10n/app_vi.arb index b45c522..98b0336 100644 --- a/lib/product/lang/l10n/app_vi.arb +++ b/lib/product/lang/l10n/app_vi.arb @@ -21,6 +21,8 @@ "confirm_fake_fire_sure_message": "Tôi chắc chắn", "let_PCCC_handle_message": "Hãy để Đội PCCC xử lý!", "overview_message": "Tổng quan", + "over_view_owner_devices":"Thiết bị sở hữu", + "over_view_joined_devices":"Thiết bị tham gia", "total_nof_devices_message": "Tổng số", "active_devices_message": "Bình thường", "inactive_devices_message": "Đang tắt",