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

@@ -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) {