Refactor HTTPInterceptor and add CustomSnackBar helper

Refactor HTTPInterceptor to handle error responses and add a CustomSnackBar
helper to display snack bars. This will improve error handling and user
feedback in the application.
This commit is contained in:
Mohammad Salameh
2024-04-15 12:02:34 +03:00
parent 590c70a7d8
commit cfc395e210
10 changed files with 287 additions and 188 deletions

View File

@ -31,12 +31,9 @@ class DevicesCubit extends Cubit<DevicesState> {
}
}
}
bool _isClosed = false;
static DevicesCubit? _instance;
static DevicesCubit getInstance() {
print('device cubit instance found : ${_instance != null}');
print('selected space : ${HomeCubit.getInstance().selectedSpace != null}');
return _instance ??= DevicesCubit._();
}
@ -44,7 +41,6 @@ class DevicesCubit extends Cubit<DevicesState> {
@override
Future<void> close() {
_isClosed = true;
_instance = null;
return super.close();
}
@ -272,56 +268,37 @@ class DevicesCubit extends Cubit<DevicesState> {
emitSafe(DeviceControlError('Failed to control the device'));
}
});
} on DioException catch (e) {
emitSafe(DeviceControlError(ServerFailure.fromDioError(e).errMessage));
} on ServerFailure catch (failure) {
emitSafe(DeviceControlError(failure.errMessage));
}
}
fetchGroups(int spaceId) async {
if (_isClosed) return;
try {
emitSafe(DevicesCategoriesLoading());
allCategories = await DevicesAPI.fetchGroups(spaceId);
emitSafe(DevicesCategoriesSuccess());
} on DioException catch (error) {
emitSafe(
DevicesCategoriesError(ServerFailure.fromDioError(error).errMessage),
);
}
emitSafe(DevicesCategoriesLoading());
allCategories = await DevicesAPI.fetchGroups(spaceId);
emitSafe(DevicesCategoriesSuccess());
}
fetchDevicesByRoomId(int? roomId) async {
if (_isClosed) return;
if (roomId == null) return;
try {
emitSafe(GetDevicesLoading());
int roomIndex = HomeCubit.getInstance()
.selectedSpace!
.rooms!
.indexWhere((element) => element.id == roomId);
HomeCubit.getInstance().selectedSpace!.rooms![roomIndex].devices =
await SpacesAPI.getDevicesByRoomId(roomId);
//get status for each device
for (var device in HomeCubit.getInstance()
.selectedSpace!
.rooms![roomIndex]
.devices!) {
getDevicesStatues(device.id!, roomIndex);
}
emitSafe(GetDevicesSuccess());
} on DioException catch (error) {
emitSafe(
GetDevicesError(ServerFailure.fromDioError(error).errMessage),
);
emitSafe(GetDevicesLoading());
int roomIndex = HomeCubit.getInstance()
.selectedSpace!
.rooms!
.indexWhere((element) => element.id == roomId);
HomeCubit.getInstance().selectedSpace!.rooms![roomIndex].devices =
await SpacesAPI.getDevicesByRoomId(roomId);
//get status for each device
for (var device
in HomeCubit.getInstance().selectedSpace!.rooms![roomIndex].devices!) {
getDevicesStatues(device.id!, roomIndex);
}
emitSafe(GetDevicesSuccess());
}
getDevicesStatues(String deviceId, int roomIndex, {String? code}) async {
if (_isClosed) return;
try {
emitSafe(GetDeviceStatusLoading(code: code));
int deviceIndex = HomeCubit.getInstance()
@ -345,9 +322,9 @@ class DevicesCubit extends Cubit<DevicesState> {
.devices![deviceIndex]
.status = statuses;
emitSafe(GetDeviceStatusSuccess(code: code));
} on DioException catch (error) {
} on ServerFailure catch (failure) {
emitSafe(
GetDeviceStatusError(ServerFailure.fromDioError(error).errMessage),
GetDeviceStatusError(failure.errMessage),
);
}
}