From 76b3b6a4324bf82648678771658e71a33808f9a1 Mon Sep 17 00:00:00 2001 From: ashrafzarkanisala Date: Tue, 27 Aug 2024 18:49:59 +0300 Subject: [PATCH] fix selection for the devices --- lib/pages/common/custom_table.dart | 7 +-- .../device_managment/ac/bloc/ac_bloc.dart | 6 +-- .../device_managment/ac/helper/ac_helper.dart | 35 -------------- .../bloc/device_managment_bloc.dart | 32 +++++++++++-- .../bloc/device_managment_state.dart | 9 ++++ .../widgets/device_managment_body.dart | 46 +++++++++++-------- 6 files changed, 71 insertions(+), 64 deletions(-) delete mode 100644 lib/pages/device_managment/ac/helper/ac_helper.dart diff --git a/lib/pages/common/custom_table.dart b/lib/pages/common/custom_table.dart index 5aa555f1..fd64608a 100644 --- a/lib/pages/common/custom_table.dart +++ b/lib/pages/common/custom_table.dart @@ -47,13 +47,10 @@ class _DynamicTableState extends State { void _toggleRowSelection(int index) { setState(() { - for (int i = 0; i < _selected.length; i++) { - _selected[i] = false; - } - _selected[index] = true; + _selected[index] = !_selected[index]; if (widget.onRowSelected != null) { - widget.onRowSelected!(index, true, widget.data[index]); + widget.onRowSelected!(index, _selected[index], widget.data[index]); } }); } diff --git a/lib/pages/device_managment/ac/bloc/ac_bloc.dart b/lib/pages/device_managment/ac/bloc/ac_bloc.dart index aacb17b8..5d147439 100644 --- a/lib/pages/device_managment/ac/bloc/ac_bloc.dart +++ b/lib/pages/device_managment/ac/bloc/ac_bloc.dart @@ -34,7 +34,7 @@ class AcBloc extends Bloc { FutureOr _onAcControl(AcControl event, Emitter emit) async { final oldValue = _getValueByCode(event.code); - _updateLocalValue(event.code, event.value); + _updateLocalValue(event.code, event.value, emit); emit(ACStatusLoaded(deviceStatus)); @@ -75,12 +75,12 @@ class AcBloc extends Bloc { void _revertValueAndEmit( String deviceId, String code, dynamic oldValue, Emitter emit) { - _updateLocalValue(code, oldValue); + _updateLocalValue(code, oldValue, emit); emit(ACStatusLoaded(deviceStatus)); emit(const AcsFailedState(error: 'Failed to control the device.')); } - void _updateLocalValue(String code, dynamic value) { + void _updateLocalValue(String code, dynamic value, Emitter emit) { switch (code) { case 'switch': if (value is bool) { diff --git a/lib/pages/device_managment/ac/helper/ac_helper.dart b/lib/pages/device_managment/ac/helper/ac_helper.dart deleted file mode 100644 index 1614f2b2..00000000 --- a/lib/pages/device_managment/ac/helper/ac_helper.dart +++ /dev/null @@ -1,35 +0,0 @@ -// import 'package:flutter/cupertino.dart'; -// import 'package:syncrow_web/pages/device_managment/ac/control_list/ac_mode.dart'; -// import 'package:syncrow_web/pages/device_managment/ac/control_list/ac_toggle.dart'; -// import 'package:syncrow_web/pages/device_managment/ac/control_list/current_temp.dart'; -// import 'package:syncrow_web/utils/constants/assets.dart'; - -// mixin ACHelper { -// Widget acHelperControlWidgets({ -// required dynamic value, -// required String code, -// required String deviceId, -// }) { -// switch (code) { -// case 'switch': -// return AcToggle(value: value, code: code, deviceId: deviceId); -// case 'temp_current': -// return CurrentTemp(value: value, code: 'temp_set', deviceId: deviceId); -// case 'temp_set': -// return SizedBox(); -// case 'mode': -// return AcMode(value: value, code: code, deviceId: deviceId); -// case 'level': -// return SizedBox(); -// case 'child_lock': -// return AcToggle( -// value: value, -// code: code, -// deviceId: deviceId, -// icon: Assets.childLock, -// description: 'Child Lock'); -// default: -// return const SizedBox(); -// } -// } -// } diff --git a/lib/pages/device_managment/all_devices/bloc/device_managment_bloc.dart b/lib/pages/device_managment/all_devices/bloc/device_managment_bloc.dart index 8f9b9bc3..edb02cb9 100644 --- a/lib/pages/device_managment/all_devices/bloc/device_managment_bloc.dart +++ b/lib/pages/device_managment/all_devices/bloc/device_managment_bloc.dart @@ -13,7 +13,7 @@ class DeviceManagementBloc int _onlineCount = 0; int _offlineCount = 0; int _lowBatteryCount = 0; - AllDevicesModel? _selectedDevice; + List _selectedDevices = []; DeviceManagementBloc() : super(DeviceManagementInitial()) { on(_onFetchDevices); @@ -75,7 +75,33 @@ class DeviceManagementBloc void _onSelectDevice( SelectDevice event, Emitter emit) { - _selectedDevice = event.selectedDevice; + if (_selectedDevices.contains(event.selectedDevice)) { + _selectedDevices.remove(event.selectedDevice); + } else { + _selectedDevices.add(event.selectedDevice); + } + + bool isControlButtonEnabled = _selectedDevices.length == 1; + + if (state is DeviceManagementLoaded) { + emit(DeviceManagementLoaded( + devices: _devices, + selectedIndex: _selectedIndex, + onlineCount: _onlineCount, + offlineCount: _offlineCount, + lowBatteryCount: _lowBatteryCount, + selectedDevice: isControlButtonEnabled ? _selectedDevices.first : null, + )); + } else if (state is DeviceManagementFiltered) { + emit(DeviceManagementFiltered( + filteredDevices: (state as DeviceManagementFiltered).filteredDevices, + selectedIndex: _selectedIndex, + onlineCount: _onlineCount, + offlineCount: _offlineCount, + lowBatteryCount: _lowBatteryCount, + selectedDevice: isControlButtonEnabled ? _selectedDevices.first : null, + )); + } } void _calculateDeviceCounts() { @@ -135,5 +161,5 @@ class DeviceManagementBloc } } - AllDevicesModel? get selectedDevice => _selectedDevice; + List get selectedDevices => _selectedDevices; } diff --git a/lib/pages/device_managment/all_devices/bloc/device_managment_state.dart b/lib/pages/device_managment/all_devices/bloc/device_managment_state.dart index 48166afb..9a6e2f41 100644 --- a/lib/pages/device_managment/all_devices/bloc/device_managment_state.dart +++ b/lib/pages/device_managment/all_devices/bloc/device_managment_state.dart @@ -66,3 +66,12 @@ class DeviceManagementFiltered extends DeviceManagementState { selectedDevice ]; } + +class DeviceSelectionUpdated extends DeviceManagementState { + final List selectedDevices; + + const DeviceSelectionUpdated(this.selectedDevices); + + @override + List get props => [selectedDevices]; +} diff --git a/lib/pages/device_managment/all_devices/widgets/device_managment_body.dart b/lib/pages/device_managment/all_devices/widgets/device_managment_body.dart index 00c56238..3fb8b300 100644 --- a/lib/pages/device_managment/all_devices/widgets/device_managment_body.dart +++ b/lib/pages/device_managment/all_devices/widgets/device_managment_body.dart @@ -26,6 +26,7 @@ class DeviceManagementBody extends StatelessWidget with HelperResponsiveLayout { int onlineCount = 0; int offlineCount = 0; int lowBatteryCount = 0; + bool isControlButtonEnabled = false; if (state is DeviceManagementLoaded) { devicesToShow = state.devices; @@ -33,12 +34,14 @@ class DeviceManagementBody extends StatelessWidget with HelperResponsiveLayout { onlineCount = state.onlineCount; offlineCount = state.offlineCount; lowBatteryCount = state.lowBatteryCount; + isControlButtonEnabled = state.selectedDevice != null; } else if (state is DeviceManagementFiltered) { devicesToShow = state.filteredDevices; selectedIndex = state.selectedIndex; onlineCount = state.onlineCount; offlineCount = state.offlineCount; lowBatteryCount = state.lowBatteryCount; + isControlButtonEnabled = state.selectedDevice != null; } final tabs = [ @@ -77,20 +80,28 @@ class DeviceManagementBody extends StatelessWidget with HelperResponsiveLayout { decoration: containerDecoration, child: Center( child: DefaultButton( - onPressed: () { - final selectedDevice = context - .read() - .selectedDevice; - if (selectedDevice != null) { - showDialog( - context: context, - builder: (context) => - DeviceControlDialog(device: selectedDevice), - ); - } - }, + onPressed: isControlButtonEnabled + ? () { + final selectedDevice = context + .read() + .selectedDevices + .first; + showDialog( + context: context, + builder: (context) => DeviceControlDialog( + device: selectedDevice), + ); + } + : null, borderRadius: 9, - child: const Text('Control'), + child: Text( + 'Control', + style: TextStyle( + color: isControlButtonEnabled + ? Colors.white + : Colors.grey, + ), + ), ), ), ), @@ -106,11 +117,10 @@ class DeviceManagementBody extends StatelessWidget with HelperResponsiveLayout { child: DynamicTable( cellDecoration: containerDecoration, onRowSelected: (index, isSelected, row) { - if (isSelected) { - context - .read() - .add(SelectDevice(devicesToShow[index])); - } + final selectedDevice = devicesToShow[index]; + context + .read() + .add(SelectDevice(selectedDevice)); }, withCheckBox: true, size: context.screenSize,