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

View File

@ -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;
}

View File

@ -90,11 +90,11 @@ class Action {
static Action? fromJson(Map<String, dynamic> json) {
if (json['name'] != null && json['type'] != null) {
return Action(
actionExecutor: json["actionExecutor"],
entityId: json["entityId"],
name: json['name'],
type: json['type'],
productType: json['productType'],
actionExecutor: json["actionExecutor"] ?? '',
entityId: json["entityId"] ?? '',
name: json['name'] as String?,
type: json['type'] as String?,
productType: json['productType'] as String?,
);
}
if (json["executorProperty"] == null) {
@ -102,10 +102,10 @@ class Action {
}
return Action(
actionExecutor: json["actionExecutor"],
entityId: json["entityId"],
actionExecutor: json["actionExecutor"] ?? '',
entityId: json["entityId"] ?? '',
executorProperty: ExecutorProperty.fromJson(json["executorProperty"]),
productType: json['productType'],
productType: json['productType'] as String?,
);
}