chore(HomeScreen): fix context unmounted

This commit is contained in:
anhtunz
2025-05-26 14:08:30 +07:00
parent 01ae020374
commit 677a7c4d4a
6 changed files with 69 additions and 59 deletions

View File

@@ -45,7 +45,6 @@ class InterFamilyBloc extends BlocBase {
try { try {
final data = jsonDecode(body); 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) { if (data is Map && data.containsKey("items") && data["items"] is List) {
List<dynamic> items = data["items"]; List<dynamic> items = data["items"];
groups = Group.fromJsonDynamicList(items); groups = Group.fromJsonDynamicList(items);

View File

@@ -69,6 +69,7 @@ class _DetailDeviceScreenState extends State<DetailDeviceScreen> {
@override @override
void dispose() { void dispose() {
getDeviceDetailTimer?.cancel(); getDeviceDetailTimer?.cancel();
super.dispose();
} }
BoxDecoration boxDecoration = BoxDecoration( BoxDecoration boxDecoration = BoxDecoration(

View File

@@ -230,42 +230,45 @@ class _HomeScreenState extends State<HomeScreen> {
length: 2, length: 2,
child: Column( child: Column(
children: [ children: [
const TabBar( TabBar(
tabs: [ tabs: [
Tab(text: 'Owner Devices'), Tab(text: appLocalization(context).over_view_owner_devices),
Tab(text: 'Joined Devices'), Tab(text: appLocalization(context).over_view_joined_devices),
], ],
labelColor: Colors.blue, labelColor: Colors.blue,
unselectedLabelColor: Colors.grey, unselectedLabelColor: Colors.grey,
indicatorColor: Colors.blue, indicatorColor: Colors.blue,
), ),
TabBarView( SizedBox(
children: [ height: context.dynamicHeight(0.6),
OverviewCard( child: TabBarView(
isOwner: true, children: [
total: data['all']?.length ?? 0, OverviewCard(
active: data['online']?.length ?? 0, isOwner: true,
inactive: total: data['all']?.length ?? 0,
data['offline']?.length ?? 0, active: data['online']?.length ?? 0,
warning: data['warn']?.length ?? 0, inactive:
unused: data['offline']?.length ?? 0,
data['not-use']?.length ?? 0), warning: data['warn']?.length ?? 0,
OverviewCard( unused:
isOwner: false, data['not-use']?.length ?? 0),
total: OverviewCard(
dataJoined['all']?.length ?? 0, isOwner: false,
active: total:
dataJoined['online']?.length ?? dataJoined['all']?.length ?? 0,
0, active:
inactive: dataJoined['online']?.length ??
dataJoined['offline']?.length ?? 0,
0, inactive:
warning: dataJoined['offline']?.length ??
dataJoined['warn']?.length ?? 0, 0,
unused: warning:
dataJoined['not-use']?.length ?? dataJoined['warn']?.length ?? 0,
0), unused:
], dataJoined['not-use']?.length ??
0,),
],
),
), ),
], ],
), ),
@@ -302,40 +305,43 @@ class _HomeScreenState extends State<HomeScreen> {
} }
void getOwnerDeviceState(List<DeviceWithAlias> allDevices) async { void getOwnerDeviceState(List<DeviceWithAlias> allDevices) async {
List<DeviceWithAlias> ownerDevices = []; ownerDevicesState.clear();
ownerDevicesStatus.clear();
if (!mounted) return;
homeBloc.sinkOwnerDevicesStatus.add(ownerDevicesStatus);
int count = 0;
for (var device in allDevices) { for (var device in allDevices) {
if (device.isOwner!) { if (device.isOwner != true) continue;
ownerDevices.add(device);
if (!mounted) return;
Map<String, dynamic> 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 || final noDataMessage = appLocalization(context).no_data_message;
ownerDevicesState.length < devices.length) { if (sensorMap['sensorBattery'] != noDataMessage) {
ownerDevicesState.clear(); if (double.parse(sensorMap['sensorBattery']) <= 20) {
ownerDevicesStatus.clear(); ownerDevicesStatus['battery'] ??= [];
homeBloc.sinkOwnerDevicesStatus.add(ownerDevicesStatus); ownerDevicesStatus['battery']!.add(device);
int count = 0; if (!mounted) return;
for (var device in ownerDevices) {
Map<String, dynamic> sensorMap = DeviceUtils.instance
.getDeviceSensors(context, device.status?.sensors ?? []);
if (device.state == 1 || device.state == 3) {
ownerDevicesStatus["state"] ??= [];
ownerDevicesStatus["state"]!.add(device);
homeBloc.sinkOwnerDevicesStatus.add(ownerDevicesStatus); homeBloc.sinkOwnerDevicesStatus.add(ownerDevicesStatus);
count++; 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<DeviceWithAlias> devices) { void getDeviceStatusAliasMap(List<DeviceWithAlias> devices) {

View File

@@ -34,7 +34,7 @@ class _GroupsScreenState extends State<GroupsScreen> {
void initState() { void initState() {
super.initState(); super.initState();
interFamilyBloc = BlocProvider.of(context); interFamilyBloc = BlocProvider.of(context);
const duration = Duration(seconds: 10); const duration = Duration(seconds: 5);
getAllGroupsTimer = Timer.periodic( getAllGroupsTimer = Timer.periodic(
duration, duration,
(Timer t) => interFamilyBloc.getAllGroup(widget.role), (Timer t) => interFamilyBloc.getAllGroup(widget.role),

View File

@@ -22,6 +22,8 @@
"let_PCCC_handle_message": "Let the Fire Prevention and Fighting Team handle it!", "let_PCCC_handle_message": "Let the Fire Prevention and Fighting Team handle it!",
"overview_message": "Overview", "overview_message": "Overview",
"total_nof_devices_message": "Total number of devices", "total_nof_devices_message": "Total number of devices",
"over_view_owner_devices":"Owner Devices",
"over_view_joined_devices":"Joined Devices",
"active_devices_message": "Active", "active_devices_message": "Active",
"inactive_devices_message": "Inactive", "inactive_devices_message": "Inactive",
"warning_devices_message": "Warning", "warning_devices_message": "Warning",

View File

@@ -21,6 +21,8 @@
"confirm_fake_fire_sure_message": "Tôi chắc chắn", "confirm_fake_fire_sure_message": "Tôi chắc chắn",
"let_PCCC_handle_message": "Hãy để Đội PCCC xử lý!", "let_PCCC_handle_message": "Hãy để Đội PCCC xử lý!",
"overview_message": "Tổng quan", "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ố", "total_nof_devices_message": "Tổng số",
"active_devices_message": "Bình thường", "active_devices_message": "Bình thường",
"inactive_devices_message": "Đang tắt", "inactive_devices_message": "Đang tắt",