mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 07:07:19 +00:00
fix selection for the devices
This commit is contained in:
@ -47,13 +47,10 @@ class _DynamicTableState extends State<DynamicTable> {
|
||||
|
||||
void _toggleRowSelection(int index) {
|
||||
setState(() {
|
||||
for (int i = 0; i < _selected.length; i++) {
|
||||
_selected[i] = false;
|
||||
}
|
||||
_selected[index] = true;
|
||||
_selected[index] = !_selected[index];
|
||||
|
||||
if (widget.onRowSelected != null) {
|
||||
widget.onRowSelected!(index, true, widget.data[index]);
|
||||
widget.onRowSelected!(index, _selected[index], widget.data[index]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ class AcBloc extends Bloc<AcsEvent, AcsState> {
|
||||
FutureOr<void> _onAcControl(AcControl event, Emitter<AcsState> emit) async {
|
||||
final oldValue = _getValueByCode(event.code);
|
||||
|
||||
_updateLocalValue(event.code, event.value);
|
||||
_updateLocalValue(event.code, event.value, emit);
|
||||
|
||||
emit(ACStatusLoaded(deviceStatus));
|
||||
|
||||
@ -75,12 +75,12 @@ class AcBloc extends Bloc<AcsEvent, AcsState> {
|
||||
|
||||
void _revertValueAndEmit(
|
||||
String deviceId, String code, dynamic oldValue, Emitter<AcsState> emit) {
|
||||
_updateLocalValue(code, oldValue);
|
||||
_updateLocalValue(code, oldValue, emit);
|
||||
emit(ACStatusLoaded(deviceStatus));
|
||||
emit(const AcsFailedState(error: 'Failed to control the device.'));
|
||||
}
|
||||
|
||||
void _updateLocalValue(String code, dynamic value) {
|
||||
void _updateLocalValue(String code, dynamic value, Emitter<AcsState> emit) {
|
||||
switch (code) {
|
||||
case 'switch':
|
||||
if (value is bool) {
|
||||
|
@ -1,35 +0,0 @@
|
||||
// import 'package:flutter/cupertino.dart';
|
||||
// import 'package:syncrow_web/pages/device_managment/ac/control_list/ac_mode.dart';
|
||||
// import 'package:syncrow_web/pages/device_managment/ac/control_list/ac_toggle.dart';
|
||||
// import 'package:syncrow_web/pages/device_managment/ac/control_list/current_temp.dart';
|
||||
// import 'package:syncrow_web/utils/constants/assets.dart';
|
||||
|
||||
// mixin ACHelper {
|
||||
// Widget acHelperControlWidgets({
|
||||
// required dynamic value,
|
||||
// required String code,
|
||||
// required String deviceId,
|
||||
// }) {
|
||||
// switch (code) {
|
||||
// case 'switch':
|
||||
// return AcToggle(value: value, code: code, deviceId: deviceId);
|
||||
// case 'temp_current':
|
||||
// return CurrentTemp(value: value, code: 'temp_set', deviceId: deviceId);
|
||||
// case 'temp_set':
|
||||
// return SizedBox();
|
||||
// case 'mode':
|
||||
// return AcMode(value: value, code: code, deviceId: deviceId);
|
||||
// case 'level':
|
||||
// return SizedBox();
|
||||
// case 'child_lock':
|
||||
// return AcToggle(
|
||||
// value: value,
|
||||
// code: code,
|
||||
// deviceId: deviceId,
|
||||
// icon: Assets.childLock,
|
||||
// description: 'Child Lock');
|
||||
// default:
|
||||
// return const SizedBox();
|
||||
// }
|
||||
// }
|
||||
// }
|
@ -13,7 +13,7 @@ class DeviceManagementBloc
|
||||
int _onlineCount = 0;
|
||||
int _offlineCount = 0;
|
||||
int _lowBatteryCount = 0;
|
||||
AllDevicesModel? _selectedDevice;
|
||||
List<AllDevicesModel> _selectedDevices = [];
|
||||
|
||||
DeviceManagementBloc() : super(DeviceManagementInitial()) {
|
||||
on<FetchDevices>(_onFetchDevices);
|
||||
@ -75,7 +75,33 @@ class DeviceManagementBloc
|
||||
|
||||
void _onSelectDevice(
|
||||
SelectDevice event, Emitter<DeviceManagementState> emit) {
|
||||
_selectedDevice = event.selectedDevice;
|
||||
if (_selectedDevices.contains(event.selectedDevice)) {
|
||||
_selectedDevices.remove(event.selectedDevice);
|
||||
} else {
|
||||
_selectedDevices.add(event.selectedDevice);
|
||||
}
|
||||
|
||||
bool isControlButtonEnabled = _selectedDevices.length == 1;
|
||||
|
||||
if (state is DeviceManagementLoaded) {
|
||||
emit(DeviceManagementLoaded(
|
||||
devices: _devices,
|
||||
selectedIndex: _selectedIndex,
|
||||
onlineCount: _onlineCount,
|
||||
offlineCount: _offlineCount,
|
||||
lowBatteryCount: _lowBatteryCount,
|
||||
selectedDevice: isControlButtonEnabled ? _selectedDevices.first : null,
|
||||
));
|
||||
} else if (state is DeviceManagementFiltered) {
|
||||
emit(DeviceManagementFiltered(
|
||||
filteredDevices: (state as DeviceManagementFiltered).filteredDevices,
|
||||
selectedIndex: _selectedIndex,
|
||||
onlineCount: _onlineCount,
|
||||
offlineCount: _offlineCount,
|
||||
lowBatteryCount: _lowBatteryCount,
|
||||
selectedDevice: isControlButtonEnabled ? _selectedDevices.first : null,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
void _calculateDeviceCounts() {
|
||||
@ -135,5 +161,5 @@ class DeviceManagementBloc
|
||||
}
|
||||
}
|
||||
|
||||
AllDevicesModel? get selectedDevice => _selectedDevice;
|
||||
List<AllDevicesModel> get selectedDevices => _selectedDevices;
|
||||
}
|
||||
|
@ -66,3 +66,12 @@ class DeviceManagementFiltered extends DeviceManagementState {
|
||||
selectedDevice
|
||||
];
|
||||
}
|
||||
|
||||
class DeviceSelectionUpdated extends DeviceManagementState {
|
||||
final List<AllDevicesModel> selectedDevices;
|
||||
|
||||
const DeviceSelectionUpdated(this.selectedDevices);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [selectedDevices];
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ class DeviceManagementBody extends StatelessWidget with HelperResponsiveLayout {
|
||||
int onlineCount = 0;
|
||||
int offlineCount = 0;
|
||||
int lowBatteryCount = 0;
|
||||
bool isControlButtonEnabled = false;
|
||||
|
||||
if (state is DeviceManagementLoaded) {
|
||||
devicesToShow = state.devices;
|
||||
@ -33,12 +34,14 @@ class DeviceManagementBody extends StatelessWidget with HelperResponsiveLayout {
|
||||
onlineCount = state.onlineCount;
|
||||
offlineCount = state.offlineCount;
|
||||
lowBatteryCount = state.lowBatteryCount;
|
||||
isControlButtonEnabled = state.selectedDevice != null;
|
||||
} else if (state is DeviceManagementFiltered) {
|
||||
devicesToShow = state.filteredDevices;
|
||||
selectedIndex = state.selectedIndex;
|
||||
onlineCount = state.onlineCount;
|
||||
offlineCount = state.offlineCount;
|
||||
lowBatteryCount = state.lowBatteryCount;
|
||||
isControlButtonEnabled = state.selectedDevice != null;
|
||||
}
|
||||
|
||||
final tabs = [
|
||||
@ -77,20 +80,28 @@ class DeviceManagementBody extends StatelessWidget with HelperResponsiveLayout {
|
||||
decoration: containerDecoration,
|
||||
child: Center(
|
||||
child: DefaultButton(
|
||||
onPressed: () {
|
||||
final selectedDevice = context
|
||||
.read<DeviceManagementBloc>()
|
||||
.selectedDevice;
|
||||
if (selectedDevice != null) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) =>
|
||||
DeviceControlDialog(device: selectedDevice),
|
||||
);
|
||||
}
|
||||
},
|
||||
onPressed: isControlButtonEnabled
|
||||
? () {
|
||||
final selectedDevice = context
|
||||
.read<DeviceManagementBloc>()
|
||||
.selectedDevices
|
||||
.first;
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => DeviceControlDialog(
|
||||
device: selectedDevice),
|
||||
);
|
||||
}
|
||||
: null,
|
||||
borderRadius: 9,
|
||||
child: const Text('Control'),
|
||||
child: Text(
|
||||
'Control',
|
||||
style: TextStyle(
|
||||
color: isControlButtonEnabled
|
||||
? Colors.white
|
||||
: Colors.grey,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
@ -106,11 +117,10 @@ class DeviceManagementBody extends StatelessWidget with HelperResponsiveLayout {
|
||||
child: DynamicTable(
|
||||
cellDecoration: containerDecoration,
|
||||
onRowSelected: (index, isSelected, row) {
|
||||
if (isSelected) {
|
||||
context
|
||||
.read<DeviceManagementBloc>()
|
||||
.add(SelectDevice(devicesToShow[index]));
|
||||
}
|
||||
final selectedDevice = devicesToShow[index];
|
||||
context
|
||||
.read<DeviceManagementBloc>()
|
||||
.add(SelectDevice(selectedDevice));
|
||||
},
|
||||
withCheckBox: true,
|
||||
size: context.screenSize,
|
||||
|
Reference in New Issue
Block a user