diff --git a/lib/pages/common/custom_table.dart b/lib/pages/common/custom_table.dart index 9d2fe949..317f7381 100644 --- a/lib/pages/common/custom_table.dart +++ b/lib/pages/common/custom_table.dart @@ -22,7 +22,7 @@ class DynamicTable extends StatefulWidget { required this.headers, required this.data, required this.size, - this.tableName, + this.tableName, required this.isEmpty, required this.withCheckBox, required this.withSelectAll, @@ -57,13 +57,19 @@ class _DynamicTableState extends State { } void _initializeSelection() { - _selected = List.generate(widget.data.length, (index) { - // Check if the initialSelectedIds contains the deviceUuid - // uuidIndex is the index of the column containing the deviceUuid - final deviceUuid = widget.data[index][widget.uuidIndex]; - return widget.initialSelectedIds != null && - widget.initialSelectedIds!.contains(deviceUuid); - }); + if (widget.data.isEmpty) { + _selected = []; + _selectAll = false; + } else { + _selected = List.generate(widget.data.length, (index) { + // Check if the initialSelectedIds contains the deviceUuid + // uuidIndex is the index of the column containing the deviceUuid + final deviceUuid = widget.data[index][widget.uuidIndex]; + return widget.initialSelectedIds != null && + widget.initialSelectedIds!.contains(deviceUuid); + }); + _selectAll = _selected.every((element) => element == true); + } } void _toggleRowSelection(int index) { @@ -76,7 +82,6 @@ class _DynamicTableState extends State { }); } - void _toggleSelectAll(bool? value) { setState(() { _selectAll = value ?? false; @@ -125,7 +130,9 @@ class _DynamicTableState extends State { ), Text( // no password - widget.tableName=='AccessManagement'? 'No Password ' : 'No Devices', + widget.tableName == 'AccessManagement' + ? 'No Password ' + : 'No Devices', style: Theme.of(context) .textTheme .bodySmall! @@ -178,8 +185,11 @@ class _DynamicTableState extends State { ), ), child: Checkbox( - value: _selected.every((element) => element == true), - onChanged:widget.withSelectAll?_toggleSelectAll:null, + value: widget.data.isNotEmpty && + _selected.every((element) => element == true), + onChanged: widget.withSelectAll && widget.data.isNotEmpty + ? _toggleSelectAll + : null, ), ); } 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 381c7969..37447cc3 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 @@ -5,7 +5,8 @@ import 'package:syncrow_web/services/devices_mang_api.dart'; part 'device_managment_event.dart'; part 'device_managment_state.dart'; -class DeviceManagementBloc extends Bloc { +class DeviceManagementBloc + extends Bloc { int _selectedIndex = 0; List _devices = []; int _onlineCount = 0; @@ -21,9 +22,11 @@ class DeviceManagementBloc extends Bloc(_onSelectedFilterChanged); on(_onSearchDevices); on(_onSelectDevice); + on(_onResetFilters); } - Future _onFetchDevices(FetchDevices event, Emitter emit) async { + Future _onFetchDevices( + FetchDevices event, Emitter emit) async { emit(DeviceManagementLoading()); try { final devices = await DevicesManagementApi().fetchDevices(); @@ -44,9 +47,10 @@ class DeviceManagementBloc extends Bloc emit) async { + void _onFilterDevices( + FilterDevices event, Emitter emit) async { if (_devices.isNotEmpty) { - _filteredDevices = _devices.where((device) { + _filteredDevices = List.from(_devices.where((device) { switch (event.filter) { case 'Online': return device.online == true; @@ -57,27 +61,48 @@ class DeviceManagementBloc extends Bloc emit) { + Future _onResetFilters( + ResetFilters event, Emitter emit) async { + productName = ''; + _selectedDevices.clear(); + _filteredDevices = List.from(_devices); + _selectedIndex = 0; + emit(DeviceManagementLoaded( + devices: _devices, + selectedIndex: 0, + onlineCount: _onlineCount, + offlineCount: _offlineCount, + lowBatteryCount: _lowBatteryCount, + selectedDevice: null, + )); + } + + void _onSelectedFilterChanged( + SelectedFilterChanged event, Emitter emit) { _selectedIndex = event.selectedIndex; add(FilterDevices(_getFilterFromIndex(_selectedIndex))); } - void _onSelectDevice(SelectDevice event, Emitter emit) { + void _onSelectDevice( + SelectDevice event, Emitter emit) { final selectedUuid = event.selectedDevice.uuid; if (_selectedDevices.any((device) => device.uuid == selectedUuid)) { @@ -112,8 +137,10 @@ class DeviceManagementBloc extends Bloc device.online == true).length; _offlineCount = _devices.where((device) => device.online == false).length; - _lowBatteryCount = - _devices.where((device) => device.batteryLevel != null && device.batteryLevel! < 20).length; + _lowBatteryCount = _devices + .where((device) => + device.batteryLevel != null && device.batteryLevel! < 20) + .length; } String _getFilterFromIndex(int index) { @@ -129,7 +156,8 @@ class DeviceManagementBloc extends Bloc emit) { + void _onSearchDevices( + SearchDevices event, Emitter emit) { // If the search fields are all empty, restore the last filtered devices if ((event.community == null || event.community!.isEmpty) && (event.unitName == null || event.unitName!.isEmpty) && @@ -151,19 +179,32 @@ class DeviceManagementBloc extends Bloc get props => [selectedDevice]; } + +class ResetFilters extends DeviceManagementEvent {} diff --git a/lib/pages/device_managment/all_devices/widgets/device_search_filters.dart b/lib/pages/device_managment/all_devices/widgets/device_search_filters.dart index e2d43b1a..51215c84 100644 --- a/lib/pages/device_managment/all_devices/widgets/device_search_filters.dart +++ b/lib/pages/device_managment/all_devices/widgets/device_search_filters.dart @@ -77,7 +77,9 @@ class _DeviceSearchFiltersState extends State communityController.clear(); unitNameController.clear(); productNameController.clear(); - context.read().add(FetchDevices()); + context.read() + ..add(ResetFilters()) + ..add(FetchDevices()); }, ); } diff --git a/lib/pages/device_managment/curtain/bloc/curtain_bloc.dart b/lib/pages/device_managment/curtain/bloc/curtain_bloc.dart index fe6dd0b8..1b2f5864 100644 --- a/lib/pages/device_managment/curtain/bloc/curtain_bloc.dart +++ b/lib/pages/device_managment/curtain/bloc/curtain_bloc.dart @@ -59,7 +59,7 @@ class CurtainBloc extends Bloc { } _timer = Timer(const Duration(seconds: 1), () async { try { - final controlValue = value ? 'open' : 'stop'; + final controlValue = value ? 'open' : 'close'; final response = await DevicesManagementApi() .deviceControl(deviceId, Status(code: code, value: controlValue));