From 6d805ddfd70b07a2bffa22e7f7a1b8bab73dba69 Mon Sep 17 00:00:00 2001 From: ashrafzarkanisala Date: Wed, 18 Sep 2024 14:33:01 +0300 Subject: [PATCH] push one gang switch --- .../bloc/wall_light_switch_bloc.dart | 52 ++++++++++++++++--- .../bloc/wall_light_switch_event.dart | 12 ++--- .../view/wall_light_batch_control.dart | 12 ++++- 3 files changed, 60 insertions(+), 16 deletions(-) diff --git a/lib/pages/device_managment/one_gang_switch/bloc/wall_light_switch_bloc.dart b/lib/pages/device_managment/one_gang_switch/bloc/wall_light_switch_bloc.dart index 19517c92..ef25e7ac 100644 --- a/lib/pages/device_managment/one_gang_switch/bloc/wall_light_switch_bloc.dart +++ b/lib/pages/device_managment/one_gang_switch/bloc/wall_light_switch_bloc.dart @@ -14,6 +14,7 @@ class WallLightSwitchBloc on(_onFetchDeviceStatus); on(_onControl); on(_onFetchBatchStatus); + on(_onBatchControl); } late WallLightStatusModel deviceStatus; @@ -49,30 +50,47 @@ class WallLightSwitchBloc value: event.value, oldValue: oldValue, emit: emit, + isBatch: false, ); } Future _runDebounce({ - required String deviceId, + required dynamic deviceId, required String code, required bool value, required bool oldValue, required Emitter emit, + required bool isBatch, }) async { + late String id; + + if (deviceId is List) { + id = deviceId.first; + } else { + id = deviceId; + } + if (_timer != null) { _timer!.cancel(); } _timer = Timer(const Duration(milliseconds: 500), () async { try { - final status = await DevicesManagementApi() - .deviceControl(deviceId, Status(code: code, value: value)); + late bool response; - if (!status) { - _revertValueAndEmit(deviceId, code, oldValue, emit); + if (isBatch) { + response = await DevicesManagementApi() + .deviceBatchControl(deviceId, code, value); + } else { + response = await DevicesManagementApi() + .deviceControl(deviceId, Status(code: code, value: value)); + } + + if (!response) { + _revertValueAndEmit(id, code, oldValue, emit); } } catch (e) { - _revertValueAndEmit(deviceId, code, oldValue, emit); + _revertValueAndEmit(id, code, oldValue, emit); } }); } @@ -103,9 +121,9 @@ class WallLightSwitchBloc emit(WallLightSwitchLoading()); try { final status = - await DevicesManagementApi().getDeviceStatus(event.deviceId); + await DevicesManagementApi().getBatchStatus(event.devicesIds); deviceStatus = - WallLightStatusModel.fromJson(event.deviceId, status.status); + WallLightStatusModel.fromJson(event.devicesIds.first, status.status); emit(WallLightSwitchStatusLoaded(deviceStatus)); } catch (e) { emit(WallLightSwitchError(e.toString())); @@ -117,4 +135,22 @@ class WallLightSwitchBloc _timer?.cancel(); return super.close(); } + + FutureOr _onBatchControl(WallLightSwitchBatchControl event, + Emitter emit) async { + final oldValue = _getValueByCode(event.code); + + _updateLocalValue(event.code, event.value); + + emit(WallLightSwitchStatusLoaded(deviceStatus)); + + await _runDebounce( + deviceId: event.devicesIds, + code: event.code, + value: event.value, + oldValue: oldValue, + emit: emit, + isBatch: true, + ); + } } diff --git a/lib/pages/device_managment/one_gang_switch/bloc/wall_light_switch_event.dart b/lib/pages/device_managment/one_gang_switch/bloc/wall_light_switch_event.dart index 59842692..88c86c97 100644 --- a/lib/pages/device_managment/one_gang_switch/bloc/wall_light_switch_event.dart +++ b/lib/pages/device_managment/one_gang_switch/bloc/wall_light_switch_event.dart @@ -27,22 +27,22 @@ class WallLightSwitchControl extends WallLightSwitchEvent { } class WallLightSwitchFetchBatchEvent extends WallLightSwitchEvent { - final String deviceId; + final List devicesIds; - WallLightSwitchFetchBatchEvent(this.deviceId); + WallLightSwitchFetchBatchEvent(this.devicesIds); @override - List get props => [deviceId]; + List get props => [devicesIds]; } class WallLightSwitchBatchControl extends WallLightSwitchEvent { - final List deviceId; + final List devicesIds; final String code; final bool value; WallLightSwitchBatchControl( - {required this.deviceId, required this.code, required this.value}); + {required this.devicesIds, required this.code, required this.value}); @override - List get props => [deviceId, code, value]; + List get props => [devicesIds, code, value]; } diff --git a/lib/pages/device_managment/one_gang_switch/view/wall_light_batch_control.dart b/lib/pages/device_managment/one_gang_switch/view/wall_light_batch_control.dart index ffdcfa85..0c58c6f5 100644 --- a/lib/pages/device_managment/one_gang_switch/view/wall_light_batch_control.dart +++ b/lib/pages/device_managment/one_gang_switch/view/wall_light_batch_control.dart @@ -19,7 +19,7 @@ class WallLightBatchControlView extends StatelessWidget Widget build(BuildContext context) { return BlocProvider( create: (context) => WallLightSwitchBloc(deviceId: deviceIds.first) - ..add(WallLightSwitchFetchBatchEvent(deviceIds.first)), + ..add(WallLightSwitchFetchBatchEvent(deviceIds)), child: BlocBuilder( builder: (context, state) { if (state is WallLightSwitchLoading) { @@ -63,7 +63,15 @@ class WallLightBatchControlView extends StatelessWidget code: 'switch_1', deviceId: deviceIds.first, label: 'Wall Light', - onChange: (value) {}, + onChange: (value) { + context.read().add( + WallLightSwitchBatchControl( + devicesIds: deviceIds, + code: 'switch_1', + value: value, + ), + ); + }, ), FirmwareUpdateWidget( deviceId: deviceIds.first,