Fixed the three gang performance issue

This commit is contained in:
Abdullah Alassaf
2024-07-14 02:01:36 +03:00
parent ce2f419828
commit a67fd44f32
3 changed files with 206 additions and 128 deletions
@@ -95,6 +95,22 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
void _changeFirstSwitch(ChangeFirstSwitchStatusEvent event, Emitter<ThreeGangState> emit) async {
emit(LoadingNewSate(threeGangModel: deviceStatus));
try {
if (threeGangGroup) {
bool allSwitchesValue = true;
groupThreeGangList.forEach((element) {
if (element.deviceId == event.deviceId) {
element.firstSwitch = !event.value;
}
if (!element.firstSwitch || !element.secondSwitch || !element.thirdSwitch) {
allSwitchesValue = false;
}
});
emit(UpdateGroupState(threeGangList: groupThreeGangList, allSwitches: allSwitchesValue));
} else {
deviceStatus.firstSwitch = !event.value;
emit(UpdateState(threeGangModel: deviceStatus));
}
final response = await DevicesAPI.controlDevice(
DeviceControlModel(
deviceId: threeGangGroup ? event.deviceId : threeGangId,
@@ -102,15 +118,11 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
value: !event.value),
threeGangGroup ? event.deviceId : threeGangId);
if (response['success'] ?? false) {
deviceStatus.firstSwitch = !event.value;
if (!response['success']) {
add(InitialEvent(groupScreen: threeGangGroup));
}
} catch (_) {}
if (threeGangGroup) {
await Future.delayed(const Duration(seconds: 1));
add(const InitialEvent(groupScreen: true));
} else {
emit(UpdateState(threeGangModel: deviceStatus));
} catch (_) {
add(InitialEvent(groupScreen: threeGangGroup));
}
}
@@ -118,6 +130,22 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
ChangeSecondSwitchStatusEvent event, Emitter<ThreeGangState> emit) async {
emit(LoadingNewSate(threeGangModel: deviceStatus));
try {
if (threeGangGroup) {
bool allSwitchesValue = true;
groupThreeGangList.forEach((element) {
if (element.deviceId == event.deviceId) {
element.secondSwitch = !event.value;
}
if (!element.firstSwitch || !element.secondSwitch || !element.thirdSwitch) {
allSwitchesValue = false;
}
});
emit(UpdateGroupState(threeGangList: groupThreeGangList, allSwitches: allSwitchesValue));
} else {
deviceStatus.secondSwitch = !event.value;
emit(UpdateState(threeGangModel: deviceStatus));
}
final response = await DevicesAPI.controlDevice(
DeviceControlModel(
deviceId: threeGangGroup ? event.deviceId : threeGangId,
@@ -125,21 +153,33 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
value: !event.value),
threeGangGroup ? event.deviceId : threeGangId);
if (response['success'] ?? false) {
deviceStatus.secondSwitch = !event.value;
if (!response['success']) {
add(InitialEvent(groupScreen: threeGangGroup));
}
} catch (_) {}
if (threeGangGroup) {
await Future.delayed(const Duration(seconds: 1));
add(const InitialEvent(groupScreen: true));
} else {
emit(UpdateState(threeGangModel: deviceStatus));
} catch (_) {
add(InitialEvent(groupScreen: threeGangGroup));
}
}
void _changeThirdSwitch(ChangeThirdSwitchStatusEvent event, Emitter<ThreeGangState> emit) async {
emit(LoadingNewSate(threeGangModel: deviceStatus));
try {
if (threeGangGroup) {
bool allSwitchesValue = true;
groupThreeGangList.forEach((element) {
if (element.deviceId == event.deviceId) {
element.thirdSwitch = !event.value;
}
if (!element.firstSwitch || !element.secondSwitch || !element.thirdSwitch) {
allSwitchesValue = false;
}
});
emit(UpdateGroupState(threeGangList: groupThreeGangList, allSwitches: allSwitchesValue));
} else {
deviceStatus.thirdSwitch = !event.value;
emit(UpdateState(threeGangModel: deviceStatus));
}
final response = await DevicesAPI.controlDevice(
DeviceControlModel(
deviceId: threeGangGroup ? event.deviceId : threeGangId,
@@ -147,15 +187,11 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
value: !event.value),
threeGangGroup ? event.deviceId : threeGangId);
if (response['success'] ?? false) {
deviceStatus.thirdSwitch = !event.value;
if (!response['success']) {
add(InitialEvent(groupScreen: threeGangGroup));
}
} catch (_) {}
if (threeGangGroup) {
await Future.delayed(const Duration(seconds: 1));
add(const InitialEvent(groupScreen: true));
} else {
emit(UpdateState(threeGangModel: deviceStatus));
} catch (_) {
add(InitialEvent(groupScreen: threeGangGroup));
}
}
@@ -163,52 +199,82 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
emit(LoadingNewSate(threeGangModel: deviceStatus));
try {
deviceStatus.firstSwitch = false;
deviceStatus.secondSwitch = false;
deviceStatus.thirdSwitch = false;
emit(UpdateState(threeGangModel: deviceStatus));
final response = await Future.wait([
DevicesAPI.controlDevice(
DeviceControlModel(deviceId: threeGangId, code: 'switch_1', value: false), threeGangId),
DeviceControlModel(
deviceId: threeGangId, code: 'switch_1', value: deviceStatus.firstSwitch),
threeGangId),
DevicesAPI.controlDevice(
DeviceControlModel(deviceId: threeGangId, code: 'switch_2', value: false), threeGangId),
DeviceControlModel(
deviceId: threeGangId, code: 'switch_2', value: deviceStatus.secondSwitch),
threeGangId),
DevicesAPI.controlDevice(
DeviceControlModel(deviceId: threeGangId, code: 'switch_3', value: false), threeGangId),
DeviceControlModel(
deviceId: threeGangId, code: 'switch_3', value: deviceStatus.thirdSwitch),
threeGangId),
]);
if (response.every((element) => element['success'] ?? false)) {
deviceStatus.firstSwitch = false;
deviceStatus.secondSwitch = false;
deviceStatus.thirdSwitch = false;
if (response.every((element) => !element['success'])) {
await Future.delayed(const Duration(milliseconds: 500));
add(const InitialEvent(groupScreen: false));
}
} catch (_) {}
emit(UpdateState(threeGangModel: deviceStatus));
} catch (_) {
await Future.delayed(const Duration(milliseconds: 500));
add(const InitialEvent(groupScreen: false));
}
}
void _allOn(AllOnEvent event, Emitter<ThreeGangState> emit) async {
emit(LoadingNewSate(threeGangModel: deviceStatus));
try {
deviceStatus.firstSwitch = true;
deviceStatus.secondSwitch = true;
deviceStatus.thirdSwitch = true;
emit(UpdateState(threeGangModel: deviceStatus));
final response = await Future.wait([
DevicesAPI.controlDevice(
DeviceControlModel(deviceId: threeGangId, code: 'switch_1', value: true), threeGangId),
DeviceControlModel(
deviceId: threeGangId, code: 'switch_1', value: deviceStatus.firstSwitch),
threeGangId),
DevicesAPI.controlDevice(
DeviceControlModel(deviceId: threeGangId, code: 'switch_2', value: true), threeGangId),
DeviceControlModel(
deviceId: threeGangId, code: 'switch_2', value: deviceStatus.secondSwitch),
threeGangId),
DevicesAPI.controlDevice(
DeviceControlModel(deviceId: threeGangId, code: 'switch_3', value: true), threeGangId),
DeviceControlModel(
deviceId: threeGangId, code: 'switch_3', value: deviceStatus.thirdSwitch),
threeGangId),
]);
if (response.every((element) => element['success'] ?? false)) {
deviceStatus.firstSwitch = true;
deviceStatus.secondSwitch = true;
deviceStatus.thirdSwitch = true;
if (response.every((element) => !element['success'])) {
await Future.delayed(const Duration(milliseconds: 500));
add(const InitialEvent(groupScreen: false));
}
} catch (_) {}
emit(UpdateState(threeGangModel: deviceStatus));
} catch (_) {
await Future.delayed(const Duration(milliseconds: 500));
add(const InitialEvent(groupScreen: false));
}
}
void _groupAllOn(GroupAllOnEvent event, Emitter<ThreeGangState> emit) async {
emit(LoadingNewSate(threeGangModel: deviceStatus));
try {
for (int i = 0; i < groupThreeGangList.length; i++) {
await Future.wait([
groupThreeGangList[i].firstSwitch = true;
groupThreeGangList[i].secondSwitch = true;
groupThreeGangList[i].thirdSwitch = true;
}
emit(UpdateGroupState(threeGangList: groupThreeGangList, allSwitches: true));
for (int i = 0; i < groupThreeGangList.length; i++) {
final response = await Future.wait([
DevicesAPI.controlDevice(
DeviceControlModel(
deviceId: groupThreeGangList[i].deviceId, code: 'switch_1', value: true),
@@ -222,18 +288,31 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
deviceId: groupThreeGangList[i].deviceId, code: 'switch_3', value: true),
groupThreeGangList[i].deviceId),
]);
if (response.every((element) => !element['success'])) {
await Future.delayed(const Duration(milliseconds: 500));
add(const InitialEvent(groupScreen: true));
break;
}
}
} catch (_) {}
await Future.delayed(const Duration(seconds: 1));
add(const InitialEvent(groupScreen: true));
} catch (_) {
await Future.delayed(const Duration(milliseconds: 500));
add(const InitialEvent(groupScreen: true));
}
}
void _groupAllOff(GroupAllOffEvent event, Emitter<ThreeGangState> emit) async {
emit(LoadingNewSate(threeGangModel: deviceStatus));
try {
for (int i = 0; i < groupThreeGangList.length; i++) {
await Future.wait([
groupThreeGangList[i].firstSwitch = false;
groupThreeGangList[i].secondSwitch = false;
groupThreeGangList[i].thirdSwitch = false;
}
emit(UpdateGroupState(threeGangList: groupThreeGangList, allSwitches: false));
for (int i = 0; i < groupThreeGangList.length; i++) {
final response = await Future.wait([
DevicesAPI.controlDevice(
DeviceControlModel(
deviceId: groupThreeGangList[i].deviceId, code: 'switch_1', value: false),
@@ -247,10 +326,17 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
deviceId: groupThreeGangList[i].deviceId, code: 'switch_3', value: false),
groupThreeGangList[i].deviceId),
]);
if (response.every((element) => !element['success'])) {
await Future.delayed(const Duration(milliseconds: 500));
add(const InitialEvent(groupScreen: true));
break;
}
}
} catch (_) {}
await Future.delayed(const Duration(seconds: 1));
add(const InitialEvent(groupScreen: true));
} catch (_) {
await Future.delayed(const Duration(milliseconds: 500));
add(const InitialEvent(groupScreen: true));
}
}
void _changeSliding(ChangeSlidingSegment event, Emitter<ThreeGangState> emit) async {