Fix(bugs):

Can't logout
ui display when user doesn't has device
This commit is contained in:
anhtunz
2025-05-26 11:58:19 +07:00
parent f80e234b1d
commit 01ae020374
10 changed files with 417 additions and 278 deletions

View File

@@ -40,7 +40,8 @@ class _HomeScreenState extends State<HomeScreen> {
homeBloc = BlocProvider.of(context);
getOwnerAndJoinedDevices();
const duration = Duration(seconds: 10);
getAllDevicesTimer = Timer.periodic(duration, (Timer t) => getOwnerAndJoinedDevices());
getAllDevicesTimer =
Timer.periodic(duration, (Timer t) => getOwnerAndJoinedDevices());
}
@override
@@ -81,127 +82,199 @@ class _HomeScreenState extends State<HomeScreen> {
child: StreamBuilder<Map<String, List<DeviceWithAlias>>>(
stream: homeBloc.streamOwnerDevicesStatus,
builder: (context, snapshot) {
if (snapshot.data?['state'] != null || snapshot.data?['battery'] != null) {
return ConstrainedBox(
constraints: BoxConstraints(minWidth: MediaQuery.of(context).size.width),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
if (snapshot.data?['state'] != null)
...snapshot.data!['state']!
.map(
(item) => SizedBox(
width: context.dynamicWidth(0.95),
child: FutureBuilder<Widget>(
future: warningCard(context, apiServices, item),
builder: (context, warningCardSnapshot) {
if (warningCardSnapshot.hasData) {
return warningCardSnapshot.data!;
} else {
return const SizedBox.shrink();
}
},
),
),
)
.toList(),
if (snapshot.data?['battery'] != null)
...snapshot.data!['battery']!
.map(
(batteryItem) => SizedBox(
width: context.dynamicWidth(0.95),
child: FutureBuilder<Widget>(
future: notificationCard(
context, "lowBattery", appLocalization(context).low_battery_message, batteryItem),
builder: (context, warningCardSnapshot) {
if (warningCardSnapshot.hasData) {
return warningCardSnapshot.data!;
} else {
return const SizedBox.shrink();
}
},
),
),
)
.toList(),
],
),
);
} else {
return Padding(
padding: context.paddingMedium,
child: Center(
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(
Icons.check_circle_outline_rounded,
size: 40,
color: Colors.green,
),
SizedBox(width: context.lowValue),
Text(
appLocalization(context).notification_description,
maxLines: 2,
overflow: TextOverflow.ellipsis,
softWrap: true,
textAlign: TextAlign.start,
),
],
return AnimatedSwitcher(
duration: context.lowDuration,
transitionBuilder:
(Widget child, Animation<double> animation) {
final offsetAnimation = Tween<Offset>(
begin: const Offset(0.0, 0.2),
end: Offset.zero,
).animate(CurvedAnimation(
parent: animation,
curve: Curves.easeInOut,
));
return FadeTransition(
opacity: animation,
child: SlideTransition(
position: offsetAnimation,
child: child,
),
),
);
}
);
},
child: snapshot.data?['state'] != null ||
snapshot.data?['battery'] != null
? ConstrainedBox(
key: const ValueKey('data'),
constraints: BoxConstraints(
minWidth:
MediaQuery.of(context).size.width),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
if (snapshot.data?['state'] != null)
...snapshot.data!['state']!
.map(
(item) => SizedBox(
width: context.dynamicWidth(0.95),
child: FutureBuilder<Widget>(
future: warningCard(
context, apiServices, item),
builder: (context,
warningCardSnapshot) {
if (warningCardSnapshot
.hasData) {
return warningCardSnapshot
.data!;
} else {
return const SizedBox
.shrink();
}
},
),
),
)
.toList(),
if (snapshot.data?['battery'] != null)
...snapshot.data!['battery']!
.map(
(batteryItem) => SizedBox(
width: context.dynamicWidth(0.95),
child: FutureBuilder<Widget>(
future: notificationCard(
context,
"lowBattery",
appLocalization(context)
.low_battery_message,
batteryItem,
),
builder: (context,
warningCardSnapshot) {
if (warningCardSnapshot
.hasData) {
return warningCardSnapshot
.data!;
} else {
return const SizedBox
.shrink();
}
},
),
),
)
.toList(),
],
),
)
: Padding(
key: const ValueKey('no_data'),
padding: context.paddingMedium,
child: Center(
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(
Icons.check_circle_outline_rounded,
size: 40,
color: Colors.green,
),
SizedBox(width: context.lowValue),
Text(
appLocalization(context)
.notification_description,
maxLines: 2,
overflow: TextOverflow.ellipsis,
softWrap: true,
textAlign: TextAlign.start,
),
],
),
),
),
);
},
),
),
),
StreamBuilder<bool?>(
stream: homeBloc.streamHasJoinedDevice,
initialData: null,
builder: (context, hasJoinedDeviceSnapshot) {
return StreamBuilder<Map<String, List<DeviceWithAlias>>>(
stream: homeBloc.streamAllDevicesAliasMap,
builder: (context, allDevicesAliasMapSnapshot) {
return StreamBuilder<Map<String, List<DeviceWithAlias>>>(
stream: homeBloc.streamAllDevicesAliasJoinedMap,
builder: (context, allDevicesAliasJoinedMapSnapshot) {
if (hasJoinedDeviceSnapshot.data == null) {
return const CircularProgressIndicator();
} else {
final data = allDevicesAliasMapSnapshot.data!;
final dataJoined =
allDevicesAliasJoinedMapSnapshot.data!;
if (hasJoinedDeviceSnapshot.data == false) {
if (!allDevicesAliasMapSnapshot.hasData ||
allDevicesAliasMapSnapshot.data == null) {
return const Center(
child: CircularProgressIndicator());
}
StreamBuilder<Map<String, List<DeviceWithAlias>>>(
stream: homeBloc.streamAllDevicesAliasMap,
builder: (context, allDevicesAliasMapSnapshot) {
if (!allDevicesAliasMapSnapshot.hasData ||
allDevicesAliasMapSnapshot.data == null) {
return const Center(child: CircularProgressIndicator());
}
final data = allDevicesAliasMapSnapshot.data!;
return 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);
}),
SizedBox(height: context.lowValue),
StreamBuilder<Map<String, List<DeviceWithAlias>>>(
stream: homeBloc.streamAllDevicesAliasJoinedMap,
builder: (context, allDevicesAliasJoinedMapSnapshot) {
if (!allDevicesAliasJoinedMapSnapshot.hasData ||
allDevicesAliasJoinedMapSnapshot.data == null) {
return const Center(child: CircularProgressIndicator());
}
final data = allDevicesAliasJoinedMapSnapshot.data!;
final total = data['all']?.length ?? 0;
final active = data['online']?.length ?? 0;
final inactive = data['offline']?.length ?? 0;
final warning = data['warn']?.length ?? 0;
final unused = data['not-use']?.length ?? 0;
if (total == 0 && active == 0 && inactive == 0 && warning == 0 && unused == 0) {
return const SizedBox.shrink();
}
return OverviewCard(
isOwner: false,
total: total,
active: active,
inactive: inactive,
warning: warning,
unused: unused,
return 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);
} else {
return DefaultTabController(
length: 2,
child: Column(
children: [
const TabBar(
tabs: [
Tab(text: 'Owner Devices'),
Tab(text: '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),
],
),
],
),
);
}
}
},
);
},
);
},
),
@@ -217,9 +290,15 @@ class _HomeScreenState extends State<HomeScreen> {
final data = jsonDecode(response);
List<dynamic> result = data["items"];
devices = DeviceWithAlias.fromJsonDynamicList(result);
getOwnerDeviceState(devices);
checkSettingDevice(devices);
getDeviceStatusAliasMap(devices);
List<DeviceWithAlias> publicDevices = [];
for (var device in devices) {
if (device.visibility == "PUBLIC") {
publicDevices.add(device);
}
}
getOwnerDeviceState(publicDevices);
checkSettingDevice(publicDevices);
getDeviceStatusAliasMap(publicDevices);
}
void getOwnerDeviceState(List<DeviceWithAlias> allDevices) async {
@@ -229,21 +308,23 @@ class _HomeScreenState extends State<HomeScreen> {
ownerDevices.add(device);
}
}
if (ownerDevicesState.isEmpty || ownerDevicesState.length < devices.length) {
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 ?? []);
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);
count++;
}
if (sensorMap['sensorBattery'] != appLocalization(context).no_data_message) {
if (sensorMap['sensorBattery'] !=
appLocalization(context).no_data_message) {
if (double.parse(sensorMap['sensorBattery']) <= 20) {
ownerDevicesStatus['battery'] ??= [];
ownerDevicesStatus['battery']!.add(device);
@@ -260,7 +341,7 @@ class _HomeScreenState extends State<HomeScreen> {
void getDeviceStatusAliasMap(List<DeviceWithAlias> devices) {
allDevicesAliasMap.clear();
allDevicesAliasJoinedMap.clear();
bool check = false;
for (var key in ['all', 'online', 'offline', 'warning', 'not-use']) {
allDevicesAliasMap[key] = [];
allDevicesAliasJoinedMap[key] = [];
@@ -282,6 +363,7 @@ class _HomeScreenState extends State<HomeScreen> {
allDevicesAliasMap['not-use']!.add(device);
}
} else {
check = true;
allDevicesAliasJoinedMap['all']!.add(device);
if (device.state == 0 || device.state == 1) {
allDevicesAliasJoinedMap['online']!.add(device);
@@ -297,7 +379,7 @@ class _HomeScreenState extends State<HomeScreen> {
}
}
}
homeBloc.sinkHasJoinedDevice.add(check);
homeBloc.sinkAllDevicesAliasMap.add(allDevicesAliasMap);
homeBloc.sinkAllDevicesAliasJoinedMap.add(allDevicesAliasJoinedMap);
}
@@ -306,7 +388,8 @@ class _HomeScreenState extends State<HomeScreen> {
if (isFunctionCall) {
log("Ham check setting da duoc goi");
} else {
String? response = await apiServices.getAllSettingsNotificationOfDevices();
String? response =
await apiServices.getAllSettingsNotificationOfDevices();
if (response != "") {
final data = jsonDecode(response);
final result = data['data'];
@@ -314,11 +397,13 @@ class _HomeScreenState extends State<HomeScreen> {
List<DeviceNotificationSettings> list =
DeviceNotificationSettings.mapFromJson(result).values.toList();
// log("List: $list");
Set<String> thingIdsInList = list.map((device) => device.thingId!).toSet();
Set<String> thingIdsInList =
list.map((device) => device.thingId!).toSet();
for (var device in devices) {
if (!thingIdsInList.contains(device.thingId)) {
log("Device with Thing ID ${device.thingId} is not in the notification settings list.");
await apiServices.setupDeviceNotification(device.thingId!, device.name!);
await apiServices.setupDeviceNotification(
device.thingId!, device.name!);
} else {
log("All devices are in the notification settings list.");
}