mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
added community filtering
This commit is contained in:
@ -6,7 +6,8 @@ import 'package:syncrow_web/services/devices_mang_api.dart';
|
|||||||
part 'device_managment_event.dart';
|
part 'device_managment_event.dart';
|
||||||
part 'device_managment_state.dart';
|
part 'device_managment_state.dart';
|
||||||
|
|
||||||
class DeviceManagementBloc extends Bloc<DeviceManagementEvent, DeviceManagementState> {
|
class DeviceManagementBloc
|
||||||
|
extends Bloc<DeviceManagementEvent, DeviceManagementState> {
|
||||||
int _selectedIndex = 0;
|
int _selectedIndex = 0;
|
||||||
List<AllDevicesModel> _devices = [];
|
List<AllDevicesModel> _devices = [];
|
||||||
int _onlineCount = 0;
|
int _onlineCount = 0;
|
||||||
@ -29,7 +30,8 @@ class DeviceManagementBloc extends Bloc<DeviceManagementEvent, DeviceManagementS
|
|||||||
on<UpdateSelection>(_onUpdateSelection);
|
on<UpdateSelection>(_onUpdateSelection);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _onFetchDevices(FetchDevices event, Emitter<DeviceManagementState> emit) async {
|
Future<void> _onFetchDevices(
|
||||||
|
FetchDevices event, Emitter<DeviceManagementState> emit) async {
|
||||||
emit(DeviceManagementLoading());
|
emit(DeviceManagementLoading());
|
||||||
try {
|
try {
|
||||||
final devices = await DevicesManagementApi().fetchDevices();
|
final devices = await DevicesManagementApi().fetchDevices();
|
||||||
@ -51,7 +53,8 @@ class DeviceManagementBloc extends Bloc<DeviceManagementEvent, DeviceManagementS
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onFilterDevices(FilterDevices event, Emitter<DeviceManagementState> emit) async {
|
void _onFilterDevices(
|
||||||
|
FilterDevices event, Emitter<DeviceManagementState> emit) async {
|
||||||
if (_devices.isNotEmpty) {
|
if (_devices.isNotEmpty) {
|
||||||
_filteredDevices = List.from(_devices.where((device) {
|
_filteredDevices = List.from(_devices.where((device) {
|
||||||
switch (event.filter) {
|
switch (event.filter) {
|
||||||
@ -82,7 +85,8 @@ class DeviceManagementBloc extends Bloc<DeviceManagementEvent, DeviceManagementS
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _onResetFilters(ResetFilters event, Emitter<DeviceManagementState> emit) async {
|
Future<void> _onResetFilters(
|
||||||
|
ResetFilters event, Emitter<DeviceManagementState> emit) async {
|
||||||
currentProductName = '';
|
currentProductName = '';
|
||||||
_selectedDevices.clear();
|
_selectedDevices.clear();
|
||||||
_filteredDevices = List.from(_devices);
|
_filteredDevices = List.from(_devices);
|
||||||
@ -98,7 +102,8 @@ class DeviceManagementBloc extends Bloc<DeviceManagementEvent, DeviceManagementS
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onResetSelectedDevices(ResetSelectedDevices event, Emitter<DeviceManagementState> emit) {
|
void _onResetSelectedDevices(
|
||||||
|
ResetSelectedDevices event, Emitter<DeviceManagementState> emit) {
|
||||||
_selectedDevices.clear();
|
_selectedDevices.clear();
|
||||||
|
|
||||||
if (state is DeviceManagementLoaded) {
|
if (state is DeviceManagementLoaded) {
|
||||||
@ -124,12 +129,14 @@ class DeviceManagementBloc extends Bloc<DeviceManagementEvent, DeviceManagementS
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onSelectedFilterChanged(SelectedFilterChanged event, Emitter<DeviceManagementState> emit) {
|
void _onSelectedFilterChanged(
|
||||||
|
SelectedFilterChanged event, Emitter<DeviceManagementState> emit) {
|
||||||
_selectedIndex = event.selectedIndex;
|
_selectedIndex = event.selectedIndex;
|
||||||
add(FilterDevices(_getFilterFromIndex(_selectedIndex)));
|
add(FilterDevices(_getFilterFromIndex(_selectedIndex)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onSelectDevice(SelectDevice event, Emitter<DeviceManagementState> emit) {
|
void _onSelectDevice(
|
||||||
|
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)) {
|
||||||
@ -140,7 +147,8 @@ class DeviceManagementBloc extends Bloc<DeviceManagementEvent, DeviceManagementS
|
|||||||
|
|
||||||
List<AllDevicesModel> clonedSelectedDevices = List.from(_selectedDevices);
|
List<AllDevicesModel> clonedSelectedDevices = List.from(_selectedDevices);
|
||||||
|
|
||||||
bool isControlButtonEnabled = _checkIfControlButtonEnabled(clonedSelectedDevices);
|
bool isControlButtonEnabled =
|
||||||
|
_checkIfControlButtonEnabled(clonedSelectedDevices);
|
||||||
|
|
||||||
if (state is DeviceManagementLoaded) {
|
if (state is DeviceManagementLoaded) {
|
||||||
emit(DeviceManagementLoaded(
|
emit(DeviceManagementLoaded(
|
||||||
@ -149,7 +157,8 @@ class DeviceManagementBloc extends Bloc<DeviceManagementEvent, DeviceManagementS
|
|||||||
onlineCount: _onlineCount,
|
onlineCount: _onlineCount,
|
||||||
offlineCount: _offlineCount,
|
offlineCount: _offlineCount,
|
||||||
lowBatteryCount: _lowBatteryCount,
|
lowBatteryCount: _lowBatteryCount,
|
||||||
selectedDevice: clonedSelectedDevices.isNotEmpty ? clonedSelectedDevices : null,
|
selectedDevice:
|
||||||
|
clonedSelectedDevices.isNotEmpty ? clonedSelectedDevices : null,
|
||||||
isControlButtonEnabled: isControlButtonEnabled,
|
isControlButtonEnabled: isControlButtonEnabled,
|
||||||
));
|
));
|
||||||
} else if (state is DeviceManagementFiltered) {
|
} else if (state is DeviceManagementFiltered) {
|
||||||
@ -159,13 +168,15 @@ class DeviceManagementBloc extends Bloc<DeviceManagementEvent, DeviceManagementS
|
|||||||
onlineCount: _onlineCount,
|
onlineCount: _onlineCount,
|
||||||
offlineCount: _offlineCount,
|
offlineCount: _offlineCount,
|
||||||
lowBatteryCount: _lowBatteryCount,
|
lowBatteryCount: _lowBatteryCount,
|
||||||
selectedDevice: clonedSelectedDevices.isNotEmpty ? clonedSelectedDevices : null,
|
selectedDevice:
|
||||||
|
clonedSelectedDevices.isNotEmpty ? clonedSelectedDevices : null,
|
||||||
isControlButtonEnabled: isControlButtonEnabled,
|
isControlButtonEnabled: isControlButtonEnabled,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onUpdateSelection(UpdateSelection event, Emitter<DeviceManagementState> emit) {
|
void _onUpdateSelection(
|
||||||
|
UpdateSelection event, Emitter<DeviceManagementState> emit) {
|
||||||
List<AllDevicesModel> selectedDevices = [];
|
List<AllDevicesModel> selectedDevices = [];
|
||||||
List<AllDevicesModel> devicesToSelectFrom = [];
|
List<AllDevicesModel> devicesToSelectFrom = [];
|
||||||
|
|
||||||
@ -208,7 +219,8 @@ class DeviceManagementBloc extends Bloc<DeviceManagementEvent, DeviceManagementS
|
|||||||
|
|
||||||
bool _checkIfControlButtonEnabled(List<AllDevicesModel> selectedDevices) {
|
bool _checkIfControlButtonEnabled(List<AllDevicesModel> selectedDevices) {
|
||||||
if (selectedDevices.length > 1) {
|
if (selectedDevices.length > 1) {
|
||||||
final productTypes = selectedDevices.map((device) => device.productType).toSet();
|
final productTypes =
|
||||||
|
selectedDevices.map((device) => device.productType).toSet();
|
||||||
return productTypes.length == 1;
|
return productTypes.length == 1;
|
||||||
} else if (selectedDevices.length == 1) {
|
} else if (selectedDevices.length == 1) {
|
||||||
return true;
|
return true;
|
||||||
@ -219,8 +231,10 @@ class DeviceManagementBloc extends Bloc<DeviceManagementEvent, DeviceManagementS
|
|||||||
void _calculateDeviceCounts() {
|
void _calculateDeviceCounts() {
|
||||||
_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 =
|
_lowBatteryCount = _devices
|
||||||
_devices.where((device) => device.batteryLevel != null && device.batteryLevel! < 20).length;
|
.where((device) =>
|
||||||
|
device.batteryLevel != null && device.batteryLevel! < 20)
|
||||||
|
.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
String _getFilterFromIndex(int index) {
|
String _getFilterFromIndex(int index) {
|
||||||
@ -236,7 +250,8 @@ class DeviceManagementBloc extends Bloc<DeviceManagementEvent, DeviceManagementS
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onSearchDevices(SearchDevices event, Emitter<DeviceManagementState> emit) {
|
void _onSearchDevices(
|
||||||
|
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.productName == null || event.productName!.isEmpty)) {
|
(event.productName == null || event.productName!.isEmpty)) {
|
||||||
@ -265,19 +280,32 @@ class DeviceManagementBloc extends Bloc<DeviceManagementEvent, DeviceManagementS
|
|||||||
final filteredDevices = devicesToSearch.where((device) {
|
final filteredDevices = devicesToSearch.where((device) {
|
||||||
final matchesCommunity = event.community == null ||
|
final matchesCommunity = event.community == null ||
|
||||||
event.community!.isEmpty ||
|
event.community!.isEmpty ||
|
||||||
(device.room?.name?.toLowerCase().contains(event.community!.toLowerCase()) ?? false);
|
(device.community?.name
|
||||||
|
?.toLowerCase()
|
||||||
|
.contains(event.community!.toLowerCase()) ??
|
||||||
|
false);
|
||||||
final matchesUnit = event.unitName == null ||
|
final matchesUnit = event.unitName == null ||
|
||||||
event.unitName!.isEmpty ||
|
event.unitName!.isEmpty ||
|
||||||
(device.unit?.name?.toLowerCase().contains(event.unitName!.toLowerCase()) ?? false);
|
(device.unit?.name
|
||||||
|
?.toLowerCase()
|
||||||
|
.contains(event.unitName!.toLowerCase()) ??
|
||||||
|
false);
|
||||||
final matchesProductName = event.productName == null ||
|
final matchesProductName = event.productName == null ||
|
||||||
event.productName!.isEmpty ||
|
event.productName!.isEmpty ||
|
||||||
(device.name?.toLowerCase().contains(event.productName!.toLowerCase()) ?? false);
|
(device.name
|
||||||
|
?.toLowerCase()
|
||||||
|
.contains(event.productName!.toLowerCase()) ??
|
||||||
|
false);
|
||||||
final matchesDeviceName = event.productName == null ||
|
final matchesDeviceName = event.productName == null ||
|
||||||
event.productName!.isEmpty ||
|
event.productName!.isEmpty ||
|
||||||
(device.categoryName?.toLowerCase().contains(event.productName!.toLowerCase()) ??
|
(device.categoryName
|
||||||
|
?.toLowerCase()
|
||||||
|
.contains(event.productName!.toLowerCase()) ??
|
||||||
false);
|
false);
|
||||||
|
|
||||||
return matchesCommunity && matchesUnit && (matchesProductName || matchesDeviceName);
|
return matchesCommunity &&
|
||||||
|
matchesUnit &&
|
||||||
|
(matchesProductName || matchesDeviceName);
|
||||||
}).toList();
|
}).toList();
|
||||||
|
|
||||||
emit(DeviceManagementFiltered(
|
emit(DeviceManagementFiltered(
|
||||||
|
@ -32,7 +32,7 @@ class _DeviceSearchFiltersState extends State<DeviceSearchFilters> with HelperRe
|
|||||||
children: [
|
children: [
|
||||||
_buildSearchField("Community", communityController, 200),
|
_buildSearchField("Community", communityController, 200),
|
||||||
const SizedBox(width: 20),
|
const SizedBox(width: 20),
|
||||||
_buildSearchField("Unit Name", unitNameController, 200),
|
_buildSearchField("Space Name", unitNameController, 200),
|
||||||
const SizedBox(width: 20),
|
const SizedBox(width: 20),
|
||||||
_buildSearchField("Device Name / Product Name", productNameController, 300),
|
_buildSearchField("Device Name / Product Name", productNameController, 300),
|
||||||
const SizedBox(width: 20),
|
const SizedBox(width: 20),
|
||||||
@ -48,7 +48,7 @@ class _DeviceSearchFiltersState extends State<DeviceSearchFilters> with HelperRe
|
|||||||
communityController,
|
communityController,
|
||||||
200,
|
200,
|
||||||
),
|
),
|
||||||
_buildSearchField("Unit Name", unitNameController, 200),
|
_buildSearchField("Space Name", unitNameController, 200),
|
||||||
_buildSearchField(
|
_buildSearchField(
|
||||||
"Device Name / Product Name",
|
"Device Name / Product Name",
|
||||||
productNameController,
|
productNameController,
|
||||||
|
@ -94,7 +94,7 @@ class DeviceControlDialog extends StatelessWidget with RouteControlsBasedCode {
|
|||||||
]),
|
]),
|
||||||
TableRow(
|
TableRow(
|
||||||
children: [
|
children: [
|
||||||
_buildInfoRow('Unit Name:', device.unit?.name ?? 'N/A'),
|
_buildInfoRow('Space Name:', device.unit?.name ?? 'N/A'),
|
||||||
_buildInfoRow('Room:', device.room?.name ?? 'N/A'),
|
_buildInfoRow('Room:', device.room?.name ?? 'N/A'),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
@ -111,7 +111,7 @@ class AddDeviceDialog extends StatelessWidget {
|
|||||||
child: CustomWebTextField(
|
child: CustomWebTextField(
|
||||||
controller: visitorBloc.unitNameController,
|
controller: visitorBloc.unitNameController,
|
||||||
isRequired: false,
|
isRequired: false,
|
||||||
textFieldName: 'Unit Name',
|
textFieldName: 'Space Name',
|
||||||
description: '',
|
description: '',
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@ -185,7 +185,7 @@ class AddDeviceDialog extends StatelessWidget {
|
|||||||
'Device Name',
|
'Device Name',
|
||||||
'Device ID',
|
'Device ID',
|
||||||
'Access Type',
|
'Access Type',
|
||||||
'Unit Name',
|
'Space Name',
|
||||||
'Status'
|
'Status'
|
||||||
],
|
],
|
||||||
data: state.data.map((item) {
|
data: state.data.map((item) {
|
||||||
|
Reference in New Issue
Block a user