This commit is contained in:
Faris Armoush
2025-06-29 16:00:15 +03:00
parent 8916efcebb
commit d8bb234537
4 changed files with 108 additions and 69 deletions

View File

@ -16,7 +16,7 @@ class DeviceManagementBloc
int _onlineCount = 0; int _onlineCount = 0;
int _offlineCount = 0; int _offlineCount = 0;
int _lowBatteryCount = 0; int _lowBatteryCount = 0;
List<AllDevicesModel> _selectedDevices = []; final List<AllDevicesModel> _selectedDevices = [];
List<AllDevicesModel> _filteredDevices = []; List<AllDevicesModel> _filteredDevices = [];
String currentProductName = ''; String currentProductName = '';
String? currentCommunity; String? currentCommunity;
@ -40,15 +40,15 @@ class DeviceManagementBloc
FetchDevices event, Emitter<DeviceManagementState> emit) async { FetchDevices event, Emitter<DeviceManagementState> emit) async {
emit(DeviceManagementLoading()); emit(DeviceManagementLoading());
try { try {
List<AllDevicesModel> devices = []; var devices = <AllDevicesModel>[];
_devices.clear(); _devices.clear();
var spaceBloc = event.context.read<SpaceTreeBloc>(); final spaceBloc = event.context.read<SpaceTreeBloc>();
final projectUuid = await ProjectManager.getProjectUUID() ?? ''; final projectUuid = await ProjectManager.getProjectUUID() ?? '';
if (spaceBloc.state.selectedCommunities.isEmpty) { if (spaceBloc.state.selectedCommunities.isEmpty) {
devices = await DevicesManagementApi().fetchDevices(projectUuid); devices = await DevicesManagementApi().fetchDevices(projectUuid);
} else { } else {
for (var community in spaceBloc.state.selectedCommunities) { for (final community in spaceBloc.state.selectedCommunities) {
List<String> spacesList = final spacesList =
spaceBloc.state.selectedCommunityAndSpaces[community] ?? []; spaceBloc.state.selectedCommunityAndSpaces[community] ?? [];
devices.addAll(await DevicesManagementApi() devices.addAll(await DevicesManagementApi()
.fetchDevices(projectUuid, spacesId: spacesList)); .fetchDevices(projectUuid, spacesId: spacesList));
@ -73,7 +73,7 @@ class DeviceManagementBloc
} }
} }
void _onFilterDevices( Future<void> _onFilterDevices(
FilterDevices event, Emitter<DeviceManagementState> emit) async { FilterDevices event, Emitter<DeviceManagementState> emit) async {
if (_devices.isNotEmpty) { if (_devices.isNotEmpty) {
_filteredDevices = List.from(_devices.where((device) { _filteredDevices = List.from(_devices.where((device) {
@ -155,8 +155,7 @@ class DeviceManagementBloc
add(FilterDevices(_getFilterFromIndex(_selectedIndex))); add(FilterDevices(_getFilterFromIndex(_selectedIndex)));
} }
void _onSelectDevice( void _onSelectDevice(SelectDevice event, Emitter<DeviceManagementState> emit) {
SelectDevice event, Emitter<DeviceManagementState> emit) {
final selectedUuid = event.selectedDevice.uuid; final selectedUuid = event.selectedDevice.uuid;
if (_selectedDevices.any((device) => device.uuid == selectedUuid)) { if (_selectedDevices.any((device) => device.uuid == selectedUuid)) {
@ -165,9 +164,9 @@ class DeviceManagementBloc
_selectedDevices.add(event.selectedDevice); _selectedDevices.add(event.selectedDevice);
} }
List<AllDevicesModel> clonedSelectedDevices = List.from(_selectedDevices); final clonedSelectedDevices = List<AllDevicesModel>.from(_selectedDevices);
bool isControlButtonEnabled = final isControlButtonEnabled =
_checkIfControlButtonEnabled(clonedSelectedDevices); _checkIfControlButtonEnabled(clonedSelectedDevices);
if (state is DeviceManagementLoaded) { if (state is DeviceManagementLoaded) {
@ -197,8 +196,8 @@ class DeviceManagementBloc
void _onUpdateSelection( void _onUpdateSelection(
UpdateSelection event, Emitter<DeviceManagementState> emit) { UpdateSelection event, Emitter<DeviceManagementState> emit) {
List<AllDevicesModel> selectedDevices = []; final selectedDevices = <AllDevicesModel>[];
List<AllDevicesModel> devicesToSelectFrom = []; var devicesToSelectFrom = <AllDevicesModel>[];
if (state is DeviceManagementLoaded) { if (state is DeviceManagementLoaded) {
devicesToSelectFrom = (state as DeviceManagementLoaded).devices; devicesToSelectFrom = (state as DeviceManagementLoaded).devices;
@ -206,7 +205,7 @@ class DeviceManagementBloc
devicesToSelectFrom = (state as DeviceManagementFiltered).filteredDevices; devicesToSelectFrom = (state as DeviceManagementFiltered).filteredDevices;
} }
for (int i = 0; i < event.selectedRows.length; i++) { for (var i = 0; i < event.selectedRows.length; i++) {
if (event.selectedRows[i]) { if (event.selectedRows[i]) {
selectedDevices.add(devicesToSelectFrom[i]); selectedDevices.add(devicesToSelectFrom[i]);
} }
@ -252,8 +251,7 @@ class DeviceManagementBloc
_onlineCount = _devices.where((device) => device.online == true).length; _onlineCount = _devices.where((device) => device.online == true).length;
_offlineCount = _devices.where((device) => device.online == false).length; _offlineCount = _devices.where((device) => device.online == false).length;
_lowBatteryCount = _devices _lowBatteryCount = _devices
.where((device) => .where((device) => device.batteryLevel != null && device.batteryLevel! < 20)
device.batteryLevel != null && device.batteryLevel! < 20)
.length; .length;
} }
@ -270,8 +268,7 @@ class DeviceManagementBloc
} }
} }
void _onSearchDevices( void _onSearchDevices(SearchDevices event, Emitter<DeviceManagementState> emit) {
SearchDevices event, Emitter<DeviceManagementState> emit) {
if ((event.community == null || event.community!.isEmpty) && if ((event.community == null || event.community!.isEmpty) &&
(event.unitName == null || event.unitName!.isEmpty) && (event.unitName == null || event.unitName!.isEmpty) &&
(event.deviceNameOrProductName == null || (event.deviceNameOrProductName == null ||
@ -300,7 +297,7 @@ class DeviceManagementBloc
currentCommunity = event.community; currentCommunity = event.community;
currentUnitName = event.unitName; currentUnitName = event.unitName;
List<AllDevicesModel> devicesToSearch = _devices; final devicesToSearch = _devices;
if (devicesToSearch.isNotEmpty) { if (devicesToSearch.isNotEmpty) {
final searchText = event.deviceNameOrProductName?.toLowerCase() ?? ''; final searchText = event.deviceNameOrProductName?.toLowerCase() ?? '';
@ -347,14 +344,84 @@ class DeviceManagementBloc
UpdateDeviceName event, Emitter<DeviceManagementState> emit) { UpdateDeviceName event, Emitter<DeviceManagementState> emit) {
final devices = _devices.map((device) { final devices = _devices.map((device) {
if (device.uuid == event.deviceId) { if (device.uuid == event.deviceId) {
return device.copyWith(name: event.newName); final modifiedDevice = device.copyWith(name: event.newName);
_selectedDevices.removeWhere((device) => device.uuid == event.deviceId);
_selectedDevices.add(modifiedDevice);
return modifiedDevice;
} }
return device; return device;
}).toList(); }).toList();
final filteredDevices = _filteredDevices.map((device) { final filteredDevices = _filteredDevices.map((device) {
if (device.uuid == event.deviceId) { if (device.uuid == event.deviceId) {
return device.copyWith(name: event.newName); final modifiedDevice = device.copyWith(name: event.newName);
_selectedDevices.removeWhere((device) => device.uuid == event.deviceId);
_selectedDevices.add(modifiedDevice);
return modifiedDevice;
}
return device;
}).toList();
_devices = devices;
_filteredDevices = filteredDevices;
if (state is DeviceManagementLoaded) {
final loaded = state as DeviceManagementLoaded;
final selectedDevices01 = _selectedDevices.map((device) {
if (device.uuid == event.deviceId) {
final modifiedDevice = device.copyWith(name: event.newName);
return modifiedDevice;
}
return device;
}).toList();
emit(DeviceManagementLoaded(
devices: devices,
selectedIndex: loaded.selectedIndex,
onlineCount: loaded.onlineCount,
offlineCount: loaded.offlineCount,
lowBatteryCount: loaded.lowBatteryCount,
selectedDevice: selectedDevices01,
isControlButtonEnabled: loaded.isControlButtonEnabled,
));
} else if (state is DeviceManagementFiltered) {
final filtered = state as DeviceManagementFiltered;
final selectedDevices01 = filtered.selectedDevice?.map((device) {
if (device.uuid == event.deviceId) {
final modifiedDevice = device.copyWith(name: event.newName);
return modifiedDevice;
}
return device;
}).toList();
emit(DeviceManagementFiltered(
filteredDevices: filteredDevices,
selectedIndex: filtered.selectedIndex,
onlineCount: filtered.onlineCount,
offlineCount: filtered.offlineCount,
lowBatteryCount: filtered.lowBatteryCount,
selectedDevice: selectedDevices01,
isControlButtonEnabled: filtered.isControlButtonEnabled,
));
}
}
void _onUpdateSubSpaceName(
UpdateSubSpaceName event, Emitter<DeviceManagementState> emit) {
final devices = _devices.map((device) {
if (device.uuid == event.deviceId) {
return device.copyWith(
subspace:
device.subspace?.copyWith(subspaceName: event.newSubSpaceName));
}
return device;
}).toList();
final filteredDevices = _filteredDevices.map((device) {
if (device.uuid == event.deviceId) {
return device.copyWith(
subspace:
device.subspace?.copyWith(subspaceName: event.newSubSpaceName));
} }
return device; return device;
}).toList(); }).toList();
@ -364,53 +431,21 @@ class DeviceManagementBloc
if (state is DeviceManagementLoaded) { if (state is DeviceManagementLoaded) {
final loaded = state as DeviceManagementLoaded; final loaded = state as DeviceManagementLoaded;
emit(DeviceManagementLoaded( final selectedDevices = loaded.selectedDevice?.map((device) {
devices: devices,
selectedIndex: loaded.selectedIndex,
onlineCount: loaded.onlineCount,
offlineCount: loaded.offlineCount,
lowBatteryCount: loaded.lowBatteryCount,
selectedDevice: loaded.selectedDevice,
isControlButtonEnabled: loaded.isControlButtonEnabled,
));
} else if (state is DeviceManagementFiltered) {
final filtered = state as DeviceManagementFiltered;
emit(DeviceManagementFiltered(
filteredDevices: filteredDevices,
selectedIndex: filtered.selectedIndex,
onlineCount: filtered.onlineCount,
offlineCount: filtered.offlineCount,
lowBatteryCount: filtered.lowBatteryCount,
selectedDevice: filtered.selectedDevice,
isControlButtonEnabled: filtered.isControlButtonEnabled,
));
}
}
void _onUpdateSubSpaceName(
UpdateSubSpaceName event, Emitter<DeviceManagementState> emit) {
_devices = _devices.map((device) {
if (device.uuid == event.deviceId) { if (device.uuid == event.deviceId) {
final updatedSubspace = device.subspace?.copyWith( return device.copyWith(
subspaceName: event.newSubSpaceName, subspace:
); device.subspace?.copyWith(subspaceName: event.newSubSpaceName));
final s = device.copyWith(subspace: updatedSubspace);
return s;
} }
subSpaceName = device.subspace!.subspaceName;
return device; return device;
}).toList(); }).toList();
if (state is DeviceManagementLoaded) {
final loaded = state as DeviceManagementLoaded;
emit(DeviceManagementLoaded( emit(DeviceManagementLoaded(
devices: _devices, devices: _devices,
selectedIndex: loaded.selectedIndex, selectedIndex: loaded.selectedIndex,
onlineCount: loaded.onlineCount, onlineCount: loaded.onlineCount,
offlineCount: loaded.offlineCount, offlineCount: loaded.offlineCount,
lowBatteryCount: loaded.lowBatteryCount, lowBatteryCount: loaded.lowBatteryCount,
selectedDevice: loaded.selectedDevice, selectedDevice: selectedDevices,
isControlButtonEnabled: loaded.isControlButtonEnabled, isControlButtonEnabled: loaded.isControlButtonEnabled,
)); ));
} else if (state is DeviceManagementFiltered) { } else if (state is DeviceManagementFiltered) {

View File

@ -23,6 +23,7 @@ class DeviceManagementBody extends StatelessWidget with HelperResponsiveLayout {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocBuilder<DeviceManagementBloc, DeviceManagementState>( return BlocBuilder<DeviceManagementBloc, DeviceManagementState>(
buildWhen: (previous, current) => previous != current,
builder: (context, state) { builder: (context, state) {
List<AllDevicesModel> devicesToShow = []; List<AllDevicesModel> devicesToShow = [];
int selectedIndex = 0; int selectedIndex = 0;
@ -191,7 +192,7 @@ class DeviceManagementBody extends StatelessWidget with HelperResponsiveLayout {
'Product Name', 'Product Name',
'Device ID', 'Device ID',
'Space Name', 'Space Name',
'location', 'Location',
'Battery Level', 'Battery Level',
'Installation Date and Time', 'Installation Date and Time',
'Status', 'Status',
@ -265,7 +266,7 @@ class DeviceManagementBody extends StatelessWidget with HelperResponsiveLayout {
barrierDismissible: true, barrierDismissible: true,
barrierLabel: "Device Settings", barrierLabel: "Device Settings",
transitionDuration: const Duration(milliseconds: 300), transitionDuration: const Duration(milliseconds: 300),
pageBuilder: (context, anim1, anim2) { pageBuilder: (_, anim1, anim2) {
return Align( return Align(
alignment: Alignment.centerRight, alignment: Alignment.centerRight,
child: Material( child: Material(

View File

@ -19,11 +19,14 @@ class DeviceManagementContent extends StatelessWidget {
required this.device, required this.device,
required this.subSpaces, required this.subSpaces,
required this.deviceInfo, required this.deviceInfo,
required this.deviceManagementBloc,
}); });
final AllDevicesModel device; final AllDevicesModel device;
final List<SubSpaceModel> subSpaces; final List<SubSpaceModel> subSpaces;
final DeviceInfoModel deviceInfo; final DeviceInfoModel deviceInfo;
final DeviceManagementBloc deviceManagementBloc;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -88,7 +91,7 @@ class DeviceManagementContent extends StatelessWidget {
); );
}); });
context.read<DeviceManagementBloc>().add(UpdateSubSpaceName( deviceManagementBloc.add(UpdateSubSpaceName(
subspaceId: selectedSubSpace.id!, subspaceId: selectedSubSpace.id!,
deviceId: device.uuid!, deviceId: device.uuid!,
newSubSpaceName: selectedSubSpace.name ?? '')); newSubSpaceName: selectedSubSpace.name ?? ''));

View File

@ -1,4 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/flutter_svg.dart'; import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/bloc/device_mgmt_bloc/device_managment_bloc.dart'; import 'package:syncrow_web/pages/device_managment/all_devices/bloc/device_mgmt_bloc/device_managment_bloc.dart';
@ -141,14 +142,12 @@ class DeviceSettingsPanel extends StatelessWidget {
onFieldSubmitted: (value) { onFieldSubmitted: (value) {
_bloc.add(const ChangeNameEvent( _bloc.add(const ChangeNameEvent(
value: false)); value: false));
context deviceManagementBloc
.read< ..add(UpdateDeviceName(
DeviceManagementBloc>()
.add(UpdateDeviceName(
deviceId: device.uuid!, deviceId: device.uuid!,
newName: _bloc newName: _bloc
.nameController .nameController
.text)); .text))..add(ResetSelectedDevices());
}, },
decoration:const InputDecoration( decoration:const InputDecoration(
isDense: true, isDense: true,
@ -205,6 +204,7 @@ class DeviceSettingsPanel extends StatelessWidget {
device: device, device: device,
subSpaces: subSpaces.cast<SubSpaceModel>(), subSpaces: subSpaces.cast<SubSpaceModel>(),
deviceInfo: deviceInfo, deviceInfo: deviceInfo,
deviceManagementBloc: deviceManagementBloc,
), ),
const SizedBox(height: 32), const SizedBox(height: 32),
RemoveDeviceWidget(bloc: _bloc), RemoveDeviceWidget(bloc: _bloc),