mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2026-10-23 08:07:56 +00:00
Refactor emit calls to use emitSafe method
Replace direct calls to emit with emitSafe method to ensure emitting states only when the cubit is not closed. This helps prevent errors when emitting states after the cubit is closed.
This commit is contained in:
@@ -39,11 +39,18 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
// return _instance!;
|
||||
// }
|
||||
|
||||
// @override
|
||||
// Future<void> close() {
|
||||
// _isClosed = true;
|
||||
// return super.close();
|
||||
// }
|
||||
@override
|
||||
Future<void> close() {
|
||||
_isClosed = true;
|
||||
return super.close();
|
||||
}
|
||||
|
||||
void emitSafe(DevicesState newState) {
|
||||
final cubit = this;
|
||||
if (!cubit.isClosed) {
|
||||
cubit.emit(newState);
|
||||
}
|
||||
}
|
||||
|
||||
static DevicesCubit get(context) => BlocProvider.of(context);
|
||||
|
||||
@@ -57,14 +64,14 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
allCategories![i].isSelected = false;
|
||||
}
|
||||
}
|
||||
emit(DevicesCategoryChanged());
|
||||
emitSafe(DevicesCategoryChanged());
|
||||
}
|
||||
|
||||
unselectAllCategories() {
|
||||
for (var category in allCategories!) {
|
||||
category.isSelected = false;
|
||||
}
|
||||
emit(DevicesCategoryChanged());
|
||||
emitSafe(DevicesCategoryChanged());
|
||||
}
|
||||
|
||||
Widget? get chosenCategoryView {
|
||||
@@ -108,14 +115,14 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
for (var device in category.devices!) {
|
||||
if (device.isSelected) {
|
||||
category.isSelected = false;
|
||||
emit(DeviceSelected());
|
||||
emitSafe(DeviceSelected());
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
device.isSelected = !device.isSelected;
|
||||
emit(DeviceSelected());
|
||||
emitSafe(DeviceSelected());
|
||||
}
|
||||
|
||||
DeviceModel? getSelectedDevice() {
|
||||
@@ -149,7 +156,7 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
}
|
||||
updateDevicesStatus(category);
|
||||
|
||||
emit(CategorySwitchChanged());
|
||||
emitSafe(CategorySwitchChanged());
|
||||
}
|
||||
|
||||
turnOnOffDevice(DeviceModel device) {
|
||||
@@ -162,7 +169,7 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
}
|
||||
});
|
||||
updateDevicesStatus(category);
|
||||
emit(DeviceSwitchChanged());
|
||||
emitSafe(DeviceSwitchChanged());
|
||||
}
|
||||
|
||||
updateDevicesStatus(DevicesCategoryModel category) {
|
||||
@@ -173,15 +180,15 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
//check if there any ac have a different status than the initial ==> turn off the universal switch
|
||||
if (ac.isOnline != tempStatus) {
|
||||
category.devicesStatus = null;
|
||||
emit(DeviceSwitchChanged());
|
||||
emitSafe(DeviceSwitchChanged());
|
||||
return;
|
||||
}
|
||||
category.devicesStatus = tempStatus;
|
||||
emit(DeviceSwitchChanged());
|
||||
emitSafe(DeviceSwitchChanged());
|
||||
}
|
||||
} else {
|
||||
category.devicesStatus = null;
|
||||
emit(DeviceSwitchChanged());
|
||||
emitSafe(DeviceSwitchChanged());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -194,7 +201,7 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
}
|
||||
changeCategorySwitchValue(category);
|
||||
updateDevicesStatus(category);
|
||||
emit(CategorySwitchChanged());
|
||||
emitSafe(CategorySwitchChanged());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -207,7 +214,7 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
}
|
||||
changeCategorySwitchValue(category);
|
||||
updateDevicesStatus(category);
|
||||
emit(CategorySwitchChanged());
|
||||
emitSafe(CategorySwitchChanged());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -217,7 +224,7 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
for (var device in category.devices!) {
|
||||
if (device.isOnline ?? false) {
|
||||
category.devicesStatus = false;
|
||||
emit(CategorySwitchChanged());
|
||||
emitSafe(CategorySwitchChanged());
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -235,16 +242,16 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
}
|
||||
Navigator.popUntil(context, (route) => route.isFirst);
|
||||
|
||||
emit(DevicesCategoryChanged());
|
||||
emitSafe(DevicesCategoryChanged());
|
||||
}
|
||||
|
||||
deviceControl(DeviceControlModel control) async {
|
||||
emit(DeviceControlLoading());
|
||||
emitSafe(DeviceControlLoading());
|
||||
try {
|
||||
await DevicesAPI.controlDevice(control);
|
||||
emit(DeviceControlSuccess());
|
||||
emitSafe(DeviceControlSuccess());
|
||||
} on DioException catch (e) {
|
||||
emit(DeviceControlError(ServerFailure.fromDioError(e).errMessage));
|
||||
emitSafe(DeviceControlError(ServerFailure.fromDioError(e).errMessage));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -252,11 +259,11 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
if (_isClosed) return;
|
||||
|
||||
try {
|
||||
emit(DevicesCategoriesLoading());
|
||||
emitSafe(DevicesCategoriesLoading());
|
||||
allCategories = await DevicesAPI.fetchGroups(spaceId);
|
||||
emit(DevicesCategoriesSuccess());
|
||||
emitSafe(DevicesCategoriesSuccess());
|
||||
} on DioException catch (error) {
|
||||
emit(
|
||||
emitSafe(
|
||||
DevicesCategoriesError(ServerFailure.fromDioError(error).errMessage),
|
||||
);
|
||||
}
|
||||
@@ -266,7 +273,7 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
if (_isClosed) return;
|
||||
|
||||
try {
|
||||
emit(GetDevicesLoading());
|
||||
emitSafe(GetDevicesLoading());
|
||||
int roomIndex = HomeCubit.getInstance()
|
||||
.selectedSpace!
|
||||
.rooms!
|
||||
@@ -274,9 +281,9 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
HomeCubit.getInstance().selectedSpace!.rooms![roomIndex].devices =
|
||||
await SpacesAPI.getDevicesByRoomId(roomId);
|
||||
|
||||
emit(GetDevicesSuccess());
|
||||
emitSafe(GetDevicesSuccess());
|
||||
} on DioException catch (error) {
|
||||
emit(
|
||||
emitSafe(
|
||||
GetDevicesError(ServerFailure.fromDioError(error).errMessage),
|
||||
);
|
||||
}
|
||||
@@ -303,17 +310,17 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
// setLightingMode(DeviceModel light, LightMode mode) {
|
||||
// light.lightingMode =
|
||||
// lightModes.entries.firstWhere((element) => element.value == mode).key;
|
||||
// emit(LightModeChanged(mode));
|
||||
// emitSafe(LightModeChanged(mode));
|
||||
// }
|
||||
//
|
||||
// toggleLight(DeviceModel light) {
|
||||
// light.isOnline != null ? light.isOnline = !light.isOnline! : light.isOnline = true;
|
||||
// emit(LightToggled(light));
|
||||
// emitSafe(LightToggled(light));
|
||||
// }
|
||||
//
|
||||
// setColor(DeviceModel light, int color) {
|
||||
// light.color = color;
|
||||
// emit(LightColorChanged(color));
|
||||
// emitSafe(LightColorChanged(color));
|
||||
// }
|
||||
//
|
||||
// int getBrightness(DeviceModel light) {
|
||||
@@ -324,7 +331,7 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
// value = (value / 5).ceil() * 5;
|
||||
// if (value != light.brightness) {
|
||||
// light.brightness = value;
|
||||
// emit(LightBrightnessChanged(value));
|
||||
// emitSafe(LightBrightnessChanged(value));
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user