diff --git a/lib/pages/device_managment/garage_door/bloc/garage_door_bloc.dart b/lib/pages/device_managment/garage_door/bloc/garage_door_bloc.dart index 10ccbca4..bc67b081 100644 --- a/lib/pages/device_managment/garage_door/bloc/garage_door_bloc.dart +++ b/lib/pages/device_managment/garage_door/bloc/garage_door_bloc.dart @@ -19,7 +19,7 @@ class GarageDoorBloc extends Bloc { GarageDoorBloc({required this.deviceId}) : super(GarageDoorInitialState()) { on(_fetchGarageDoorStatus); - on(_toggleGarageDoor); + on(_garageDoorControlEvent); on(_addSchedule); on(_updateSchedule); on(_deleteSchedule); @@ -45,23 +45,6 @@ class GarageDoorBloc extends Bloc { } } - void _toggleGarageDoor(ToggleGarageDoorEvent event, Emitter emit) async { - final oldValue = deviceStatus.switch1; - _updateLocalValue('switch_1', event.isOpen); - emit(GarageDoorLoadedState(status: deviceStatus)); - final success = await _runDeBouncer( - deviceId: event.deviceId, - code: 'switch_1', - value: event.isOpen, - oldValue: oldValue, - emit: emit, - isBatch: false, - ); - if (!success) { - _revertValue('switch_1', oldValue, emit); - } - } - Future _addSchedule(AddGarageDoorScheduleEvent event, Emitter emit) async { try { ScheduleEntry newSchedule = ScheduleEntry( @@ -146,26 +129,6 @@ class GarageDoorBloc extends Bloc { } } - void _increaseDelay(IncreaseGarageDoorDelayEvent event, Emitter emit) async { - try { - deviceStatus = deviceStatus.copyWith(delay: deviceStatus.delay + Duration(minutes: 10)); - emit(GarageDoorLoadedState(status: deviceStatus)); - } catch (e) { - emit(GarageDoorErrorState(message: e.toString())); - } - } - - void _decreaseDelay(DecreaseGarageDoorDelayEvent event, Emitter emit) async { - try { - if (deviceStatus.delay.inMinutes > 10) { - deviceStatus = deviceStatus.copyWith(delay: deviceStatus.delay - Duration(minutes: 10)); - } - emit(GarageDoorLoadedState(status: deviceStatus)); - } catch (e) { - emit(GarageDoorErrorState(message: e.toString())); - } - } - Future _updateSelectedTime(UpdateSelectedTimeEvent event, Emitter emit) async { final currentState = state; if (currentState is GarageDoorLoadedState) { @@ -244,6 +207,49 @@ class GarageDoorBloc extends Bloc { } } + void _increaseDelay(IncreaseGarageDoorDelayEvent event, Emitter emit) async { + // if (deviceStatus.countdown1 != 0) { + try { + deviceStatus = deviceStatus.copyWith(delay: deviceStatus.delay + Duration(minutes: 10)); + emit(GarageDoorLoadedState(status: deviceStatus)); + add(GarageDoorControlEvent(deviceId: event.deviceId, value: deviceStatus.delay.inSeconds, code: 'countdown_1')); + } catch (e) { + emit(GarageDoorErrorState(message: e.toString())); + } + // } + } + + void _decreaseDelay(DecreaseGarageDoorDelayEvent event, Emitter emit) async { + // if (deviceStatus.countdown1 != 0) { + try { + if (deviceStatus.delay.inMinutes > 10) { + deviceStatus = deviceStatus.copyWith(delay: deviceStatus.delay - Duration(minutes: 10)); + } + emit(GarageDoorLoadedState(status: deviceStatus)); + add(GarageDoorControlEvent(deviceId: event.deviceId, value: deviceStatus.delay.inSeconds, code: 'countdown_1')); + } catch (e) { + emit(GarageDoorErrorState(message: e.toString())); + } + //} + } + + void _garageDoorControlEvent(GarageDoorControlEvent event, Emitter emit) async { + final oldValue = event.code == 'countdown_1' ? deviceStatus.countdown1 : deviceStatus.switch1; + _updateLocalValue(event.code, event.value); + emit(GarageDoorLoadedState(status: deviceStatus)); + final success = await _runDeBouncer( + deviceId: event.deviceId, + code: event.code, + value: event.value, + oldValue: oldValue, + emit: emit, + isBatch: false, + ); + if (!success) { + _revertValue(event.code, oldValue, emit); + } + } + void _revertValue(String code, dynamic oldValue, Emitter emit) { switch (code) { case 'switch_1': @@ -251,6 +257,11 @@ class GarageDoorBloc extends Bloc { deviceStatus = deviceStatus.copyWith(switch1: oldValue); } break; + case 'countdown_1': + if (oldValue is int) { + deviceStatus = deviceStatus.copyWith(countdown1: oldValue, delay: Duration(seconds: oldValue)); + } + break; // Add other cases if needed default: break; @@ -268,6 +279,11 @@ class GarageDoorBloc extends Bloc { deviceStatus = deviceStatus.copyWith(switch1: value); } break; + case 'countdown_1': + if (value is int) { + deviceStatus = deviceStatus.copyWith(countdown1: value, delay: Duration(seconds: value)); + } + break; // Add other cases if needed default: break; diff --git a/lib/pages/device_managment/garage_door/bloc/garage_door_event.dart b/lib/pages/device_managment/garage_door/bloc/garage_door_event.dart index 754ae140..ee07713a 100644 --- a/lib/pages/device_managment/garage_door/bloc/garage_door_event.dart +++ b/lib/pages/device_managment/garage_door/bloc/garage_door_event.dart @@ -19,15 +19,15 @@ class GarageDoorInitialEvent extends GarageDoorEvent { List get props => [deviceId]; } -class ToggleGarageDoorEvent extends GarageDoorEvent { +class GarageDoorControlEvent extends GarageDoorEvent { final String deviceId; - final bool isOpen; + final dynamic value; final String code; - const ToggleGarageDoorEvent({required this.deviceId, required this.isOpen, required this.code}); + const GarageDoorControlEvent({required this.deviceId, required this.value, required this.code}); @override - List get props => [deviceId, isOpen]; + List get props => [deviceId, value]; } class AddGarageDoorScheduleEvent extends GarageDoorEvent { diff --git a/lib/pages/device_managment/garage_door/view/garage_door_control_view.dart b/lib/pages/device_managment/garage_door/view/garage_door_control_view.dart index 8e6201f7..41e6baee 100644 --- a/lib/pages/device_managment/garage_door/view/garage_door_control_view.dart +++ b/lib/pages/device_managment/garage_door/view/garage_door_control_view.dart @@ -64,7 +64,7 @@ class GarageDoorControlView extends StatelessWidget with HelperResponsiveLayout icon: status.switch1 ? Assets.openedDoor : Assets.closedDoor, onTap: () { context.read().add( - ToggleGarageDoorEvent(deviceId: status.uuid, isOpen: !status.switch1, code: 'switch_1'), + GarageDoorControlEvent(deviceId: status.uuid, value: !status.switch1, code: 'switch_1'), ); }, status: status.switch1, @@ -94,20 +94,23 @@ class GarageDoorControlView extends StatelessWidget with HelperResponsiveLayout label: '', labelWidget: Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, - crossAxisAlignment: CrossAxisAlignment.end, + crossAxisAlignment: CrossAxisAlignment.center, children: [ IconButton( onPressed: () { - context.read().add(DecreaseGarageDoorDelayEvent(deviceId: deviceId)); + // if (status.countdown1 != 0) { + context.read().add(DecreaseGarageDoorDelayEvent(deviceId: status.uuid)); + // } }, icon: const Icon( Icons.remove, size: 28, color: ColorsManager.greyColor, ), + padding: EdgeInsets.zero, ), Text( - '${status.delay.inHours.toString().padLeft(2, '0')}', + status.delay.inHours.toString().padLeft(2, '0'), style: context.textTheme.titleLarge!.copyWith( color: ColorsManager.dialogBlueTitle, fontWeight: FontWeight.bold, @@ -118,7 +121,7 @@ class GarageDoorControlView extends StatelessWidget with HelperResponsiveLayout style: context.textTheme.bodySmall!.copyWith(color: ColorsManager.blackColor), ), Text( - '${(status.delay.inMinutes % 60).toString().padLeft(2, '0')}', + (status.delay.inMinutes % 60).toString().padLeft(2, '0'), style: context.textTheme.titleLarge!.copyWith( color: ColorsManager.dialogBlueTitle, fontWeight: FontWeight.bold, @@ -130,21 +133,29 @@ class GarageDoorControlView extends StatelessWidget with HelperResponsiveLayout ), IconButton( onPressed: () { - context.read().add(IncreaseGarageDoorDelayEvent(deviceId: deviceId)); + // if (status.countdown1 != 0) { + context.read().add(IncreaseGarageDoorDelayEvent(deviceId: status.uuid)); + // } }, icon: const Icon( Icons.add, size: 28, color: ColorsManager.greyColor, ), + padding: EdgeInsets.zero, ), ], ), - value: false, - code: 'switch_1', + value: status.countdown1 != 0 ? true : false, + code: 'countdown_1', deviceId: status.uuid, icon: Assets.doorDelay, - onChange: (value) {}, + onChange: (value) { + context.read().add( + GarageDoorControlEvent( + deviceId: status.uuid, value: value ? status.delay.inSeconds : 0, code: 'countdown_1'), + ); + }, ), IconNameStatusContainer( isFullIcon: false,