feat(api_service): Update try-catch funtion and handle exception
update(loading_animation): Update loading animation using Lottie
This commit is contained in:
anhtunz
2025-06-09 14:29:43 +07:00
parent 477646ab9d
commit 3a8fa3633c
44 changed files with 1659 additions and 1065 deletions

View File

@@ -3,6 +3,9 @@ import 'dart:convert';
import 'dart:developer';
import 'package:flutter/material.dart';
import '../../product/shared/shared_component_loading_animation.dart';
import '../../product/shared/shared_loading_animation.dart';
import '../../product/shared/shared_snack_bar.dart';
import 'shared/alert_card.dart';
import 'shared/warning_card.dart';
import '../../product/utils/device_utils.dart';
@@ -206,7 +209,7 @@ class _HomeScreenState extends State<HomeScreen> {
stream: homeBloc.streamAllDevicesAliasJoinedMap,
builder: (context, allDevicesAliasJoinedMapSnapshot) {
if (hasJoinedDeviceSnapshot.data == null) {
return const CircularProgressIndicator();
return const SharedComponentLoadingAnimation();
} else {
final data = allDevicesAliasMapSnapshot.data!;
final dataJoined =
@@ -219,54 +222,71 @@ class _HomeScreenState extends State<HomeScreen> {
}
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);
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,
showUnused: false,
);
} else {
return DefaultTabController(
length: 2,
child: Column(
// mainAxisSize: MainAxisSize.min,
// crossAxisAlignment: CrossAxisAlignment.start,
children: [
TabBar(
tabs: [
Tab(text: appLocalization(context).over_view_owner_devices),
Tab(text: appLocalization(context).over_view_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,
),
SizedBox(
height: context.dynamicHeight(0.6),
ConstrainedBox(
constraints: BoxConstraints(
maxHeight: context.dynamicHeight(0.5),
minHeight: context.dynamicHeight(0.3),
),
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),
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,
showUnused: false,
),
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,),
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,
showUnused: false,
showActive: false,
showInactive: false,
),
],
),
),
@@ -289,19 +309,21 @@ class _HomeScreenState extends State<HomeScreen> {
}
void getOwnerAndJoinedDevices() async {
String response = await apiServices.getDashBoardDevices();
final data = jsonDecode(response);
List<dynamic> result = data["items"];
devices = DeviceWithAlias.fromJsonDynamicList(result);
List<DeviceWithAlias> publicDevices = [];
for (var device in devices) {
if (device.visibility == "PUBLIC") {
publicDevices.add(device);
try {
devices = await apiServices.getDashBoardDevices();
List<DeviceWithAlias> publicDevices = [];
for (var device in devices) {
if (device.visibility == "PUBLIC") {
publicDevices.add(device);
}
}
getOwnerDeviceState(publicDevices);
checkSettingDevice(publicDevices);
getDeviceStatusAliasMap(publicDevices);
} catch (e) {
if (!mounted) return;
showErrorTopSnackBarCustom(context, e.toString());
}
getOwnerDeviceState(publicDevices);
checkSettingDevice(publicDevices);
getDeviceStatusAliasMap(publicDevices);
}
void getOwnerDeviceState(List<DeviceWithAlias> allDevices) async {
@@ -391,33 +413,36 @@ class _HomeScreenState extends State<HomeScreen> {
}
void checkSettingDevice(List<DeviceWithAlias> devices) async {
if (isFunctionCall) {
log("Ham check setting da duoc goi");
} else {
String? response =
await apiServices.getAllSettingsNotificationOfDevices();
if (response != "") {
final data = jsonDecode(response);
final result = data['data'];
// log("Data ${DeviceNotificationSettings.mapFromJson(jsonDecode(data)).values.toList()}");
try {
if (isFunctionCall) {
log("Ham check setting da duoc goi");
} else {
List<DeviceNotificationSettings> list =
DeviceNotificationSettings.mapFromJson(result).values.toList();
await apiServices.getAllSettingsNotificationOfDevices();
// log("List: $list");
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!);
try {
await apiServices.setupDeviceNotification(
device.thingId!, device.name!);
} catch (e) {
if (!mounted) return;
showErrorTopSnackBarCustom(
context, e.toString());
}
} else {
log("All devices are in the notification settings list.");
}
}
} else {
log("apiServices: getAllSettingsNotificationofDevices error!");
}
isFunctionCall = true;
} catch (e) {
if (!mounted) return;
showErrorTopSnackBarCustom(context, e.toString());
}
isFunctionCall = true;
}
}