Update
Logging data Try-catch function
This commit is contained in:
@@ -1,16 +1,13 @@
|
||||
// ignore_for_file: use_build_context_synchronously
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/widgets.dart';
|
||||
import '../feature/devices/device_model.dart';
|
||||
import '../product/base/bloc/base_bloc.dart';
|
||||
import '../product/services/api_services.dart';
|
||||
import '../product/services/language_services.dart';
|
||||
import '../product/shared/shared_snack_bar.dart';
|
||||
import '../product/utils/response_status_utils.dart';
|
||||
|
||||
import '../feature/inter_family/group_detail/group_detail_model.dart';
|
||||
|
||||
class DetailGroupBloc extends BlocBase {
|
||||
@@ -30,86 +27,117 @@ class DetailGroupBloc extends BlocBase {
|
||||
@override
|
||||
void dispose() {}
|
||||
|
||||
Future<void> getGroupDetail(BuildContext context,String groupID) async {
|
||||
try {
|
||||
Future<void> getGroupDetail(BuildContext context, String groupID) async {
|
||||
await apiServices.execute(context, () async {
|
||||
List<DeviceOfGroup> warningDevices = [];
|
||||
GroupDetail group = await apiServices.getGroupDetail(groupID);
|
||||
sinkDetailGroup.add(group);
|
||||
if (group.devices != null) {
|
||||
for (var device in group.devices!) {
|
||||
if (device.state == 1) {
|
||||
warningDevices.add(device);
|
||||
}
|
||||
GroupDetail group = await apiServices.getGroupDetail(groupID);
|
||||
sinkDetailGroup.add(group);
|
||||
if (group.devices != null) {
|
||||
for (var device in group.devices!) {
|
||||
if (device.state == 1) {
|
||||
warningDevices.add(device);
|
||||
}
|
||||
sinkWarningDevice.add(warningDevices);
|
||||
}
|
||||
} catch (e) {
|
||||
if (!context.mounted) return;
|
||||
showErrorTopSnackBarCustom(
|
||||
context, e.toString());
|
||||
}
|
||||
|
||||
sinkWarningDevice.add(warningDevices);
|
||||
}
|
||||
});
|
||||
// try {
|
||||
// List<DeviceOfGroup> warningDevices = [];
|
||||
// GroupDetail group = await apiServices.getGroupDetail(groupID);
|
||||
// sinkDetailGroup.add(group);
|
||||
// if (group.devices != null) {
|
||||
// for (var device in group.devices!) {
|
||||
// if (device.state == 1) {
|
||||
// warningDevices.add(device);
|
||||
// }
|
||||
// }
|
||||
// sinkWarningDevice.add(warningDevices);
|
||||
// }
|
||||
// } catch (e) {
|
||||
// if (!context.mounted) return;
|
||||
// showErrorTopSnackBarCustom(context, e.toString());
|
||||
// }
|
||||
}
|
||||
|
||||
Future<void> approveUserToGroup(BuildContext context, String groupID,
|
||||
String userID, String userName) async {
|
||||
try {
|
||||
await apiServices.execute(context, () async {
|
||||
Map<String, dynamic> body = {"group_id": groupID, "user_id": userID};
|
||||
int statusCode = await apiServices.approveGroup(body);
|
||||
showSnackBarResponseByStatusCode(context, statusCode,
|
||||
"Đã duyệt $userName vào nhóm!", "Duyệt $userName thất bại!");
|
||||
} catch (e) {
|
||||
if (!context.mounted) return;
|
||||
showErrorTopSnackBarCustom(
|
||||
context, e.toString());
|
||||
}
|
||||
});
|
||||
// try {
|
||||
// Map<String, dynamic> body = {"group_id": groupID, "user_id": userID};
|
||||
// int statusCode = await apiServices.approveGroup(body);
|
||||
// showSnackBarResponseByStatusCode(context, statusCode,
|
||||
// "Đã duyệt $userName vào nhóm!", "Duyệt $userName thất bại!");
|
||||
// } catch (e) {
|
||||
// if (!context.mounted) return;
|
||||
// showErrorTopSnackBarCustom(context, e.toString());
|
||||
// }
|
||||
}
|
||||
|
||||
Future<void> deleteOrUnapproveUser(BuildContext context, String groupID,
|
||||
String userID, String userName) async {
|
||||
try {
|
||||
await apiServices.execute(context, () async {
|
||||
int statusCode = await apiServices.deleteUserInGroup(groupID, userID);
|
||||
showSnackBarResponseByStatusCode(context, statusCode,
|
||||
"Đã xóa người dùng $userName", "Xóa người dùng $userName thất bại");
|
||||
} catch (e) {
|
||||
if (!context.mounted) return;
|
||||
showErrorTopSnackBarCustom(
|
||||
context, e.toString());
|
||||
}
|
||||
});
|
||||
// try {
|
||||
// int statusCode = await apiServices.deleteUserInGroup(groupID, userID);
|
||||
// showSnackBarResponseByStatusCode(context, statusCode,
|
||||
// "Đã xóa người dùng $userName", "Xóa người dùng $userName thất bại");
|
||||
// } catch (e) {
|
||||
// if (!context.mounted) return;
|
||||
// showErrorTopSnackBarCustom(context, e.toString());
|
||||
// }
|
||||
}
|
||||
|
||||
Future<void> deleteDevice(BuildContext context, String groupID,
|
||||
String thingID, String deviceName) async {
|
||||
try {
|
||||
await apiServices.execute(context, () async {
|
||||
int statusCode = await apiServices.deleteDeviceInGroup(groupID, thingID);
|
||||
showSnackBarResponseByStatusCode(context, statusCode,
|
||||
"Đã xóa thiết bị $deviceName", "Xóa thiết bị $deviceName thất bại");
|
||||
} catch (e) {
|
||||
if (!context.mounted) return;
|
||||
showErrorTopSnackBarCustom(
|
||||
context, e.toString());
|
||||
}
|
||||
});
|
||||
// try {
|
||||
// int statusCode = await apiServices.deleteDeviceInGroup(groupID, thingID);
|
||||
// showSnackBarResponseByStatusCode(context, statusCode,
|
||||
// "Đã xóa thiết bị $deviceName", "Xóa thiết bị $deviceName thất bại");
|
||||
// } catch (e) {
|
||||
// if (!context.mounted) return;
|
||||
// showErrorTopSnackBarCustom(context, e.toString());
|
||||
// }
|
||||
}
|
||||
|
||||
Future<void> leaveGroup(
|
||||
BuildContext context, String groupID, String userID) async {
|
||||
try {
|
||||
await apiServices.execute(context, () async {
|
||||
int statusCode = await apiServices.deleteUserInGroup(groupID, userID);
|
||||
showSnackBarResponseByStatusCode(
|
||||
context,
|
||||
statusCode,
|
||||
appLocalization(context).notification_leave_group_success,
|
||||
appLocalization(context).notification_leave_group_failed);
|
||||
} catch (e) {
|
||||
if (!context.mounted) return;
|
||||
showErrorTopSnackBarCustom(
|
||||
context, e.toString());
|
||||
}
|
||||
});
|
||||
// try {
|
||||
// int statusCode = await apiServices.deleteUserInGroup(groupID, userID);
|
||||
// showSnackBarResponseByStatusCode(
|
||||
// context,
|
||||
// statusCode,
|
||||
// appLocalization(context).notification_leave_group_success,
|
||||
// appLocalization(context).notification_leave_group_failed);
|
||||
// } catch (e) {
|
||||
// if (!context.mounted) return;
|
||||
// showErrorTopSnackBarCustom(context, e.toString());
|
||||
// }
|
||||
}
|
||||
|
||||
Future<void> updateDeviceNameInGroup(
|
||||
BuildContext context, String thingID, String newAlias) async {
|
||||
try {
|
||||
await apiServices.execute(context, () async {
|
||||
Map<String, dynamic> body = {"thing_id": thingID, "alias": newAlias};
|
||||
int statusCode = await apiServices.updateDeviceAlias(body);
|
||||
showSnackBarResponseByStatusCode(
|
||||
@@ -118,26 +146,29 @@ class DetailGroupBloc extends BlocBase {
|
||||
appLocalization(context).notification_update_device_success,
|
||||
appLocalization(context).notification_update_device_failed,
|
||||
);
|
||||
} catch (e) {
|
||||
if (!context.mounted) return;
|
||||
showErrorTopSnackBarCustom(
|
||||
context, e.toString());
|
||||
}
|
||||
});
|
||||
// try {
|
||||
// Map<String, dynamic> body = {"thing_id": thingID, "alias": newAlias};
|
||||
// int statusCode = await apiServices.updateDeviceAlias(body);
|
||||
// showSnackBarResponseByStatusCode(
|
||||
// context,
|
||||
// statusCode,
|
||||
// appLocalization(context).notification_update_device_success,
|
||||
// appLocalization(context).notification_update_device_failed,
|
||||
// );
|
||||
// } catch (e) {
|
||||
// if (!context.mounted) return;
|
||||
// showErrorTopSnackBarCustom(context, e.toString());
|
||||
// }
|
||||
}
|
||||
|
||||
Future<List<Device>> getOwnerDevices(BuildContext context) async {
|
||||
List<Device> allDevices = [];
|
||||
try {
|
||||
Future<List<Device>> getOwnerDevices(BuildContext context) {
|
||||
return apiServices.execute(context, () async {
|
||||
final originalDevices = await apiServices.getOwnerDevices();
|
||||
allDevices = originalDevices.where((device) => device.visibility == "PUBLIC").toList();
|
||||
|
||||
return allDevices;
|
||||
} catch (e) {
|
||||
if (context.mounted) {
|
||||
showErrorTopSnackBarCustom(context, e.toString());
|
||||
}
|
||||
return allDevices;
|
||||
}
|
||||
return originalDevices
|
||||
.where((device) => device.visibility == "PUBLIC")
|
||||
.toList();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> addDeviceToGroup(
|
||||
@@ -145,7 +176,7 @@ class DetailGroupBloc extends BlocBase {
|
||||
Map<String, dynamic> body = {
|
||||
"thing_id": thingID,
|
||||
};
|
||||
try {
|
||||
await apiServices.execute(context, () async {
|
||||
int statusCode = await apiServices.addDeviceToGroup(groupID, body);
|
||||
showSnackBarResponseByStatusCode(
|
||||
context,
|
||||
@@ -153,10 +184,18 @@ class DetailGroupBloc extends BlocBase {
|
||||
appLocalization(context).notification_add_device_success,
|
||||
appLocalization(context).notification_add_device_failed,
|
||||
);
|
||||
} catch (e) {
|
||||
if (!context.mounted) return;
|
||||
showErrorTopSnackBarCustom(
|
||||
context, e.toString());
|
||||
}
|
||||
});
|
||||
// try {
|
||||
// int statusCode = await apiServices.addDeviceToGroup(groupID, body);
|
||||
// showSnackBarResponseByStatusCode(
|
||||
// context,
|
||||
// statusCode,
|
||||
// appLocalization(context).notification_add_device_success,
|
||||
// appLocalization(context).notification_add_device_failed,
|
||||
// );
|
||||
// } catch (e) {
|
||||
// if (!context.mounted) return;
|
||||
// showErrorTopSnackBarCustom(context, e.toString());
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user