Add functionality to fetch devices by room ID

Added a new method in DevicesCubit to fetch devices by room ID and updated
related classes and API calls to support this functionality.
This commit is contained in:
Mohammad Salameh
2024-04-02 12:12:54 +03:00
parent 1397778123
commit 5dc4f96a71
8 changed files with 75 additions and 26 deletions

View File

@ -16,6 +16,7 @@ import 'package:syncrow_app/features/devices/view/widgets/three_gang/three_gang_
import 'package:syncrow_app/features/devices/view/widgets/smart_door/door_view.dart';
import 'package:syncrow_app/services/api/devices_api.dart';
import 'package:syncrow_app/services/api/network_exception.dart';
import 'package:syncrow_app/services/api/spaces_api.dart';
import 'package:syncrow_app/utils/resource_manager/constants.dart';
part 'devices_state.dart';
@ -24,6 +25,9 @@ class DevicesCubit extends Cubit<DevicesState> {
DevicesCubit() : super(DevicesInitial()) {
if (HomeCubit.getInstance().selectedSpace != null) {
fetchGroups(HomeCubit.getInstance().selectedSpace!.id!);
for (var room in HomeCubit.getInstance().selectedSpace!.rooms!) {
fetchDevicesByRoomId(room.id!);
}
}
}
bool _isClosed = false;
@ -251,6 +255,26 @@ class DevicesCubit extends Cubit<DevicesState> {
}
}
fetchDevicesByRoomId(int roomId) async {
if (_isClosed) return;
try {
emit(GetDevicesLoading());
int roomIndex = HomeCubit.getInstance()
.selectedSpace!
.rooms!
.indexWhere((element) => element.id == roomId);
HomeCubit.getInstance().selectedSpace!.rooms![roomIndex].devices =
await SpacesAPI.getDevicesByRoomId(roomId);
emit(GetDevicesSuccess());
} on DioException catch (error) {
emit(
GetDevicesError(ServerFailure.fromDioError(error).errMessage),
);
}
}
///Lights
onHorizontalDragUpdate(DeviceModel light, double dx, double screenWidth) {
double newBrightness = (dx / (screenWidth - 15) * 100);