Refactor device filtering logic and improve code readability

- Extracted the logic for filtering implemented devices into a separate method `_getOnlyImplementedDevices`
- Created a set `allowedDevices` to store the allowed device types
- Updated the filtering logic to use the `allowedDevices` set for checking device types
- Removed unnecessary conditions for filtering devices

Fix nullability issues in `Action` model

- Added null checks for `actionExecutor`, `entityId`, `name`, `type`, and `productType` properties in the `fromJson` method of the `Action` model
- Set default values for `actionExecutor` and `entityId` if they are null
- Updated the type casting for `name`, `type`, and `productType` properties to avoid potential nullability issues
This commit is contained in:
mohammad
2025-04-17 10:16:10 +03:00
parent 1cff69d496
commit 7005d8ba83
2 changed files with 26 additions and 18 deletions
@@ -169,18 +169,26 @@ class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
}
}
_getOnlyImplementedDevices(List<DeviceModel> devices) {
List<DeviceModel> _getOnlyImplementedDevices(List<DeviceModel> devices) {
List<DeviceModel> implementedDevices = [];
for (int i = 0; i < devices.length; i++) {
if (devices[i].productType == DeviceType.AC ||
devices[i].productType == DeviceType.DoorLock ||
devices[i].productType == DeviceType.Gateway ||
devices[i].productType == DeviceType.WallSensor ||
devices[i].productType == DeviceType.CeilingSensor ||
devices[i].productType == DeviceType.ThreeGang ||
devices[i].productType == DeviceType.OneGang) {
implementedDevices.add(devices[i]);
const allowedDevices = <DeviceType>{
DeviceType.OneGang,
DeviceType.TwoGang,
DeviceType.ThreeGang,
DeviceType.AC,
DeviceType.DoorLock,
DeviceType.Gateway,
DeviceType.WallSensor,
DeviceType.CeilingSensor
};
for (final device in devices) {
final isDeviceAllowed = allowedDevices.contains(device.productType);
if (isDeviceAllowed) {
implementedDevices.add(device);
}
return implementedDevices;
}
return implementedDevices;
}