fix the issue in mobile to have indicator depend on time of unlock and unlock on pressing with lock animation

This commit is contained in:
raf-dev1
2025-06-23 13:55:52 +03:00
parent a4f56effbb
commit 069a8e16db
3 changed files with 221 additions and 135 deletions
@@ -149,7 +149,6 @@ class SmartDoorBloc extends Bloc<SmartDoorEvent, SmartDoorState> {
_streamSubscription = null;
return super.close();
}
_doorLockUpdated(DoorLockUpdated event, Emitter<SmartDoorState> emit) {
unlockRequest = deviceStatus.unlockRequest;
@@ -254,7 +253,10 @@ class SmartDoorBloc extends Bloc<SmartDoorEvent, SmartDoorState> {
}
void _updateLock(UpdateLockEvent event, Emitter<SmartDoorState> emit) async {
emit(LoadingNewSate(smartDoorModel: deviceStatus));
// emit(LoadingNewSate(smartDoorModel: deviceStatus));
final oldValue = deviceStatus.normalOpenSwitch;
deviceStatus = deviceStatus.copyWith(normalOpenSwitch: !oldValue);
emit(UpdateState(smartDoorModel: deviceStatus));
try {
// final response = await DevicesAPI.controlDevice(
// DeviceControlModel(deviceId: deviceId, code: 'normal_open_switch', value: !event.value),
@@ -262,10 +264,36 @@ class SmartDoorBloc extends Bloc<SmartDoorEvent, SmartDoorState> {
final response = await DevicesAPI.openDoorLock(deviceId);
if (response) {
deviceStatus.normalOpenSwitch = !event.value;
if (!response) {
_revertValueAndEmit(deviceId, 'normal_open_switch', oldValue, emit);
}
} catch (_) {}
} catch (_) {
_revertValueAndEmit(deviceId, 'normal_open_switch', oldValue, emit);
}
}
void _revertValueAndEmit(String deviceId, String code, dynamic oldValue,
Emitter<SmartDoorState> emit) {
_updateLocalValue(code, oldValue);
emit(UpdateState(smartDoorModel: deviceStatus));
emit(const FailedState(errorMessage: 'Failed to control the device.'));
}
void _updateLocalValue(String code, dynamic value) {
switch (code) {
case 'normal_open_switch':
if (value is bool) {
deviceStatus = deviceStatus.copyWith(normalOpenSwitch: value);
}
break;
case 'reverse_lock':
if (value is bool) {
deviceStatus = deviceStatus.copyWith(reverseLock: value);
}
break;
default:
break;
}
emit(UpdateState(smartDoorModel: deviceStatus));
}