diff --git a/lib/pages/device_managment/two_gang_switch/bloc/two_gang_switch_bloc.dart b/lib/pages/device_managment/two_gang_switch/bloc/two_gang_switch_bloc.dart index 7db9d56c..7a15a68c 100644 --- a/lib/pages/device_managment/two_gang_switch/bloc/two_gang_switch_bloc.dart +++ b/lib/pages/device_managment/two_gang_switch/bloc/two_gang_switch_bloc.dart @@ -12,6 +12,7 @@ class TwoGangSwitchBloc extends Bloc { on(_onFetchDeviceStatus); on(_onControl); on(_onFetchBatchStatus); + on(_onBatchControl); } late TwoGangStatusModel deviceStatus; @@ -46,30 +47,46 @@ class TwoGangSwitchBloc extends Bloc { 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 (isBatch) { + response = await DevicesManagementApi() + .deviceBatchControl(deviceId, code, value); + } else { + response = await DevicesManagementApi() + .deviceControl(deviceId, Status(code: code, value: value)); + } - if (!status) { - _revertValueAndEmit(deviceId, code, oldValue, emit); + if (!response) { + _revertValueAndEmit(id, code, oldValue, emit); } } catch (e) { - _revertValueAndEmit(deviceId, code, oldValue, emit); + _revertValueAndEmit(id, code, oldValue, emit); } }); } @@ -106,8 +123,9 @@ class TwoGangSwitchBloc extends Bloc { emit(TwoGangSwitchLoading()); try { final status = - await DevicesManagementApi().getDeviceStatus(event.deviceId); - deviceStatus = TwoGangStatusModel.fromJson(event.deviceId, status.status); + await DevicesManagementApi().getBatchStatus(event.devicesIds); + deviceStatus = + TwoGangStatusModel.fromJson(event.devicesIds.first, status.status); emit(TwoGangSwitchStatusLoaded(deviceStatus)); } catch (e) { emit(TwoGangSwitchError(e.toString())); @@ -119,4 +137,22 @@ class TwoGangSwitchBloc extends Bloc { _timer?.cancel(); return super.close(); } + + FutureOr _onBatchControl( + TwoGangSwitchBatchControl event, Emitter emit) async { + final oldValue = _getValueByCode(event.code); + + _updateLocalValue(event.code, event.value); + + emit(TwoGangSwitchStatusLoaded(deviceStatus)); + + await _runDebounce( + deviceId: event.deviceId, + code: event.code, + value: event.value, + oldValue: oldValue, + emit: emit, + isBatch: true, + ); + } } diff --git a/lib/pages/device_managment/two_gang_switch/bloc/two_gang_switch_event.dart b/lib/pages/device_managment/two_gang_switch/bloc/two_gang_switch_event.dart index 2bacfc92..d5b9a01d 100644 --- a/lib/pages/device_managment/two_gang_switch/bloc/two_gang_switch_event.dart +++ b/lib/pages/device_managment/two_gang_switch/bloc/two_gang_switch_event.dart @@ -27,12 +27,12 @@ class TwoGangSwitchControl extends TwoGangSwitchEvent { } class TwoGangSwitchFetchBatchEvent extends TwoGangSwitchEvent { - final String deviceId; + final List devicesIds; - TwoGangSwitchFetchBatchEvent(this.deviceId); + TwoGangSwitchFetchBatchEvent(this.devicesIds); @override - List get props => [deviceId]; + List get props => [devicesIds]; } class TwoGangSwitchBatchControl extends TwoGangSwitchEvent { diff --git a/lib/pages/device_managment/two_gang_switch/view/wall_light_batch_control.dart b/lib/pages/device_managment/two_gang_switch/view/wall_light_batch_control.dart index 323bc3f8..1e417dfa 100644 --- a/lib/pages/device_managment/two_gang_switch/view/wall_light_batch_control.dart +++ b/lib/pages/device_managment/two_gang_switch/view/wall_light_batch_control.dart @@ -19,7 +19,7 @@ class TwoGangBatchControlView extends StatelessWidget Widget build(BuildContext context) { return BlocProvider( create: (context) => TwoGangSwitchBloc(deviceId: deviceIds.first) - ..add(TwoGangSwitchFetchBatchEvent(deviceIds.first)), + ..add(TwoGangSwitchFetchBatchEvent(deviceIds)), child: BlocBuilder( builder: (context, state) { if (state is TwoGangSwitchLoading) { @@ -62,14 +62,26 @@ class TwoGangBatchControlView extends StatelessWidget code: 'switch_1', deviceId: deviceIds.first, label: 'Wall Light', - onChange: (value) {}, + onChange: (value) { + context.read().add(TwoGangSwitchBatchControl( + deviceId: deviceIds, + code: 'switch_1', + value: value, + )); + }, ), ToggleWidget( value: status.switch2, code: 'switch_2', deviceId: deviceIds.first, label: 'Ceiling Light', - onChange: (value) {}, + onChange: (value) { + context.read().add(TwoGangSwitchBatchControl( + deviceId: deviceIds, + code: 'switch_2', + value: value, + )); + }, ), FirmwareUpdateWidget( deviceId: deviceIds.first,