import 'package:bloc/bloc.dart'; import 'package:equatable/equatable.dart'; import 'package:syncrow_web/pages/device_managment/models/devices_model.dart'; import 'package:syncrow_web/services/devices_mang_api.dart'; part 'device_managment_event.dart'; part 'device_managment_state.dart'; class DeviceManagementBloc extends Bloc { int _selectedIndex = 0; List _devices = []; int _onlineCount = 0; int _offlineCount = 0; int _lowBatteryCount = 0; DeviceManagementBloc() : super(DeviceManagementInitial()) { on(_onFetchDevices); on(_onFilterDevices); on(_onSelectedFilterChanged); on(_onSearchDevices); } Future _onFetchDevices( FetchDevices event, Emitter emit) async { emit(DeviceManagementLoading()); try { final devices = await DevicesManagementApi().fetchDevices(); _devices = devices; _calculateDeviceCounts(); emit(DeviceManagementLoaded( devices: devices, selectedIndex: _selectedIndex, onlineCount: _onlineCount, offlineCount: _offlineCount, lowBatteryCount: _lowBatteryCount, )); } catch (e) { emit(DeviceManagementInitial()); } } void _onFilterDevices( FilterDevices event, Emitter emit) { if (_devices.isNotEmpty) { final filteredDevices = _devices.where((device) { switch (event.filter) { case 'Online': return device.online == true; case 'Offline': return device.online == false; case 'Low Battery': return device.batteryLevel != null && device.batteryLevel! < 20; default: return true; } }).toList(); emit(DeviceManagementFiltered( filteredDevices: filteredDevices, selectedIndex: _selectedIndex, onlineCount: _onlineCount, offlineCount: _offlineCount, lowBatteryCount: _lowBatteryCount, )); } } void _onSelectedFilterChanged( SelectedFilterChanged event, Emitter emit) { _selectedIndex = event.selectedIndex; add(FilterDevices(_getFilterFromIndex(_selectedIndex))); } void _calculateDeviceCounts() { _onlineCount = _devices.where((device) => device.online == true).length; _offlineCount = _devices.where((device) => device.online == false).length; _lowBatteryCount = _devices .where((device) => device.batteryLevel != null && device.batteryLevel! < 20) .length; } String _getFilterFromIndex(int index) { switch (index) { case 1: return 'Online'; case 2: return 'Offline'; case 3: return 'Low Battery'; default: return 'All'; } } void _onSearchDevices( SearchDevices event, Emitter emit) { if (_devices.isNotEmpty) { final filteredDevices = _devices.where((device) { final matchesCommunity = event.community == null || event.community!.isEmpty || (device.room?.name ?.toLowerCase() .contains(event.community!.toLowerCase()) ?? false); final matchesUnit = event.unitName == null || event.unitName!.isEmpty || (device.unit?.name ?.toLowerCase() .contains(event.unitName!.toLowerCase()) ?? false); final matchesProductName = event.productName == null || event.productName!.isEmpty || (device.name ?.toLowerCase() .contains(event.productName!.toLowerCase()) ?? false); return matchesCommunity && matchesUnit && matchesProductName; }).toList(); emit(DeviceManagementFiltered( filteredDevices: filteredDevices, selectedIndex: _selectedIndex, onlineCount: _onlineCount, offlineCount: _offlineCount, lowBatteryCount: _lowBatteryCount, )); } } }