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 {
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<dynamic> items = data["items"];
groups = Group.fromJsonDynamicList(items);

View File

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

View File

@@ -230,42 +230,45 @@ class _HomeScreenState extends State<HomeScreen> {
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<HomeScreen> {
}
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) {
if (device.isOwner!) {
ownerDevices.add(device);
if (device.isOwner != true) continue;
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 ||
ownerDevicesState.length < devices.length) {
ownerDevicesState.clear();
ownerDevicesStatus.clear();
homeBloc.sinkOwnerDevicesStatus.add(ownerDevicesStatus);
int count = 0;
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);
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<DeviceWithAlias> devices) {

View File

@@ -34,7 +34,7 @@ class _GroupsScreenState extends State<GroupsScreen> {
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),

View File

@@ -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",

View File

@@ -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",