diff --git a/lib/features/app_layout/bloc/home_cubit.dart b/lib/features/app_layout/bloc/home_cubit.dart index e4dde3e..bc4d7b2 100644 --- a/lib/features/app_layout/bloc/home_cubit.dart +++ b/lib/features/app_layout/bloc/home_cubit.dart @@ -260,6 +260,7 @@ class HomeCubit extends Cubit { try { spaces = await SpacesAPI.getSpacesByUserId(); } catch (failure) { + print(failure); emitSafe(GetSpacesError("No units found")); return; } diff --git a/lib/features/devices/bloc/device_manager_bloc/device_manager_bloc.dart b/lib/features/devices/bloc/device_manager_bloc/device_manager_bloc.dart index a8b1eb1..2824ef0 100644 --- a/lib/features/devices/bloc/device_manager_bloc/device_manager_bloc.dart +++ b/lib/features/devices/bloc/device_manager_bloc/device_manager_bloc.dart @@ -25,7 +25,8 @@ class DeviceManagerBloc extends Bloc { static List? allCategories; - Future _onFetchAllDevices(FetchAllDevices event, Emitter emit) async { + Future _onFetchAllDevices( + FetchAllDevices event, Emitter emit) async { emit(state.copyWith(loading: true)); try { final allDevices = await HomeManagementAPI.fetchDevicesByUnitId(); @@ -39,21 +40,27 @@ class DeviceManagerBloc extends Bloc { FetchDevicesByRoomId event, Emitter emit) async { emit(state.copyWith(loading: true)); try { - final devices = await DevicesAPI.getDevicesByRoomId(event.roomId); + final devices = await DevicesAPI.getDevicesByRoomId( + communityUuid: event.unit.community.uuid, + spaceUuid: event.unit.id, + roomId: event.roomId, + ); emit(state.copyWith(devices: devices, loading: false)); } catch (e) { emit(state.copyWith(error: e.toString(), loading: false)); } } - void _onSelectCategory(SelectCategory event, Emitter emit) { + void _onSelectCategory( + SelectCategory event, Emitter emit) { for (var i = 0; i < allCategories!.length; i++) { allCategories![i].isSelected = i == event.index; } emit(state.copyWith(categoryChanged: true)); } - void _onUnselectAllCategories(UnselectAllCategories event, Emitter emit) { + void _onUnselectAllCategories( + UnselectAllCategories event, Emitter emit) { for (var category in allCategories!) { category.isSelected = false; } @@ -97,7 +104,8 @@ class DeviceManagerBloc extends Bloc { _updateDevicesStatus(category, emit); } - void _onTurnOnOffDevice(TurnOnOffDevice event, Emitter emit) { + void _onTurnOnOffDevice( + TurnOnOffDevice event, Emitter emit) { var device = event.device; device.isOnline = !device.isOnline!; DevicesCategoryModel category = allCategories!.firstWhere((category) { @@ -120,7 +128,8 @@ class DeviceManagerBloc extends Bloc { emit(state.copyWith(categoryChanged: true)); // Set category changed state } - void _updateDevicesStatus(DevicesCategoryModel category, Emitter emit) { + void _updateDevicesStatus( + DevicesCategoryModel category, Emitter emit) { if (category.devices != null && category.devices!.isNotEmpty) { bool? tempStatus = category.devices![0].isOnline; for (var device in category.devices!) { @@ -140,7 +149,8 @@ class DeviceManagerBloc extends Bloc { try { final deviceFunctions = await DevicesAPI.deviceFunctions(event.deviceId); - emit(state.copyWith(functionsLoading: false, deviceFunctions: deviceFunctions)); + emit(state.copyWith( + functionsLoading: false, deviceFunctions: deviceFunctions)); } catch (e) { emit(state.copyWith(functionsLoading: false, error: e.toString())); } diff --git a/lib/features/devices/bloc/device_manager_bloc/device_manager_event.dart b/lib/features/devices/bloc/device_manager_bloc/device_manager_event.dart index 867027d..e0abf8c 100644 --- a/lib/features/devices/bloc/device_manager_bloc/device_manager_event.dart +++ b/lib/features/devices/bloc/device_manager_bloc/device_manager_event.dart @@ -1,5 +1,6 @@ import 'package:equatable/equatable.dart'; import 'package:flutter/material.dart'; +import 'package:syncrow_app/features/app_layout/model/space_model.dart'; import 'package:syncrow_app/features/devices/model/device_category_model.dart'; import 'package:syncrow_app/features/devices/model/device_control_model.dart'; import 'package:syncrow_app/features/devices/model/device_model.dart'; @@ -15,8 +16,9 @@ class FetchAllDevices extends DeviceManagerEvent {} class FetchDevicesByRoomId extends DeviceManagerEvent { final String roomId; + final SpaceModel unit; - const FetchDevicesByRoomId(this.roomId); + const FetchDevicesByRoomId(this.roomId, this.unit); @override List get props => [roomId]; diff --git a/lib/features/devices/bloc/devices_cubit.dart b/lib/features/devices/bloc/devices_cubit.dart index 943f0db..982f904 100644 --- a/lib/features/devices/bloc/devices_cubit.dart +++ b/lib/features/devices/bloc/devices_cubit.dart @@ -23,14 +23,22 @@ import 'package:syncrow_app/utils/resource_manager/constants.dart'; part 'devices_state.dart'; class DevicesCubit extends Cubit { + Future _initializeDevices(SpaceModel selectedSpace) async { + // Fetch devices for each room in the selected space + for (var room in selectedSpace.subspaces ?? []) { + await fetchDevicesByRoomId(selectedSpace, room.id!); + } + + // Fetch groups based on the selected space ID + await fetchGroups(selectedSpace.id ?? ''); + } + DevicesCubit._() : super(DevicesInitial()) { - if (HomeCubit.getInstance().selectedSpace != null && - HomeCubit.getInstance().spaces!.isNotEmpty) { - // fetchGroups(HomeCubit.getInstance().selectedSpace!.id!); - for (var room in HomeCubit.getInstance().selectedSpace!.subspaces!) { - fetchDevicesByRoomId(room.id!); - } - fetchGroups(HomeCubit.getInstance().selectedSpace?.id ?? ''); + final selectedSpace = HomeCubit.getInstance().selectedSpace; + final spaces = HomeCubit.getInstance().spaces; + + if (selectedSpace != null && spaces != null && spaces.isNotEmpty) { + _initializeDevices(selectedSpace); } } @@ -87,10 +95,10 @@ class DevicesCubit extends Cubit { return const DoorsListView(); case DeviceType.Curtain: return const CurtainListView(); - // case DeviceType.ThreeGang: - // return const ThreeGangSwitchesView(); - // case DeviceType.Gateway: - // return const GateWayView(); + // case DeviceType.ThreeGang: + // return const ThreeGangSwitchesView(); + // case DeviceType.Gateway: + // return const GateWayView(); default: return null; } @@ -303,20 +311,26 @@ class DevicesCubit extends Cubit { } } - fetchDevicesByRoomId(String? roomId) async { + fetchDevicesByRoomId(SpaceModel? unit, String? roomId) async { if (roomId == null) return; emitSafe(GetDevicesLoading()); - int roomIndex = - HomeCubit.getInstance().selectedSpace!.subspaces!.indexWhere((element) => element.id == roomId); + int roomIndex = HomeCubit.getInstance() + .selectedSpace! + .subspaces! + .indexWhere((element) => element.id == roomId); try { HomeCubit.getInstance().selectedSpace!.subspaces![roomIndex].devices = - await DevicesAPI.getDevicesByRoomId(roomId); + await DevicesAPI.getDevicesByRoomId( + communityUuid: unit!.community.uuid, + spaceUuid: unit.id, + roomId: roomId); } catch (e) { emitSafe(GetDevicesError(e.toString())); return; } - final devices = HomeCubit.getInstance().selectedSpace!.subspaces![roomIndex].devices; + final devices = + HomeCubit.getInstance().selectedSpace!.subspaces![roomIndex].devices; emitSafe(GetDevicesSuccess(devices)); //get status for each device @@ -346,8 +360,11 @@ class DevicesCubit extends Cubit { emitSafe(GetDeviceStatusError(e.toString())); return; } - HomeCubit.getInstance().selectedSpace!.subspaces![roomIndex].devices![deviceIndex].status = - statuses; + HomeCubit.getInstance() + .selectedSpace! + .subspaces![roomIndex] + .devices![deviceIndex] + .status = statuses; emitSafe(GetDeviceStatusSuccess(code: code)); } @@ -396,8 +413,6 @@ class DevicesCubit extends Cubit { // emitSafe(LightBrightnessChanged(value)); // } // } - - } enum LightMode { diff --git a/lib/features/menu/bloc/manage_unit_bloc/manage_unit_bloc.dart b/lib/features/menu/bloc/manage_unit_bloc/manage_unit_bloc.dart index bffb001..f225077 100644 --- a/lib/features/menu/bloc/manage_unit_bloc/manage_unit_bloc.dart +++ b/lib/features/menu/bloc/manage_unit_bloc/manage_unit_bloc.dart @@ -19,10 +19,12 @@ class ManageUnitBloc extends Bloc { on(_addNewRoom); } - void _fetchRoomsAndDevices(FetchRoomsEvent event, Emitter emit) async { + void _fetchRoomsAndDevices( + FetchRoomsEvent event, Emitter emit) async { try { emit(LoadingState()); - final roomsList = await SpacesAPI.getSubSpaceBySpaceId(event.unit.community.uuid, event.unit.id); + final roomsList = await SpacesAPI.getSubSpaceBySpaceId( + event.unit.community.uuid, event.unit.id); emit(FetchRoomsState(devicesList: allDevices, roomsList: roomsList)); } catch (e) { emit(const ErrorState(message: 'Something went wrong')); @@ -30,11 +32,15 @@ class ManageUnitBloc extends Bloc { } } - void _fetchDevicesByRoomId(FetchDevicesByRoomIdEvent event, Emitter emit) async { + void _fetchDevicesByRoomId( + FetchDevicesByRoomIdEvent event, Emitter emit) async { try { Map roomDevicesId = {}; emit(LoadingState()); - final devicesList = await DevicesAPI.getDevicesByRoomId(event.roomId); + final devicesList = await DevicesAPI.getDevicesByRoomId( + communityUuid: event.unit.community.uuid, + spaceUuid: event.unit.id, + roomId: event.roomId); allDevices = await HomeManagementAPI.fetchDevicesByUserId(); List allDevicesIds = []; @@ -61,13 +67,20 @@ class ManageUnitBloc extends Bloc { } } - void _assignDevice(AssignRoomEvent event, Emitter emit) async { + void _assignDevice( + AssignRoomEvent event, Emitter emit) async { try { Map roomDevicesId = {}; emit(LoadingState()); - Map body = {"deviceUuid": event.deviceId, "roomUuid": event.roomId}; + Map body = { + "deviceUuid": event.deviceId, + "roomUuid": event.roomId + }; await HomeManagementAPI.assignDeviceToRoom(body); - final devicesList = await DevicesAPI.getDevicesByRoomId(event.roomId); + final devicesList = await DevicesAPI.getDevicesByRoomId( + communityUuid: event.unit.community.uuid, + spaceUuid: event.unit.id, + roomId: event.roomId); List allDevicesIds = []; @@ -99,7 +112,8 @@ class ManageUnitBloc extends Bloc { emit(LoadingState()); final response = await HomeCreation.createRoom(body); if (response['data']['uuid'] != '') { - final roomsList = await SpacesAPI.getSubSpaceBySpaceId(event.unit.community.uuid, event.unit.id); + final roomsList = await SpacesAPI.getSubSpaceBySpaceId( + event.unit.community.uuid, event.unit.id); allDevices = await HomeManagementAPI.fetchDevicesByUserId(); emit(FetchRoomsState(devicesList: allDevices, roomsList: roomsList)); await HomeCubit.getInstance().fetchUnitsByUserId(); diff --git a/lib/features/menu/bloc/manage_unit_bloc/manage_unit_event.dart b/lib/features/menu/bloc/manage_unit_bloc/manage_unit_event.dart index bd9de51..4db0ea3 100644 --- a/lib/features/menu/bloc/manage_unit_bloc/manage_unit_event.dart +++ b/lib/features/menu/bloc/manage_unit_bloc/manage_unit_event.dart @@ -23,11 +23,12 @@ class FetchRoomsEvent extends ManageUnitEvent { class FetchDevicesByRoomIdEvent extends ManageUnitEvent { final String roomId; + final SpaceModel unit; - const FetchDevicesByRoomIdEvent({required this.roomId}); + const FetchDevicesByRoomIdEvent({required this.roomId, required this.unit}); @override - List get props => [roomId]; + List get props => [roomId, unit]; } class AddNewRoom extends ManageUnitEvent { @@ -43,9 +44,11 @@ class AddNewRoom extends ManageUnitEvent { class AssignRoomEvent extends ManageUnitEvent { final String roomId; final String deviceId; + final SpaceModel unit; - const AssignRoomEvent({required this.roomId, required this.deviceId}); + const AssignRoomEvent( + {required this.roomId, required this.deviceId, required this.unit}); @override - List get props => [roomId]; + List get props => [roomId, unit]; } diff --git a/lib/features/menu/view/widgets/manage_home/assign_devices.dart b/lib/features/menu/view/widgets/manage_home/assign_devices.dart index bc097fb..e8002ab 100644 --- a/lib/features/menu/view/widgets/manage_home/assign_devices.dart +++ b/lib/features/menu/view/widgets/manage_home/assign_devices.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_svg/svg.dart'; +import 'package:syncrow_app/features/app_layout/model/space_model.dart'; import 'package:syncrow_app/features/menu/bloc/manage_unit_bloc/manage_unit_bloc.dart'; import 'package:syncrow_app/features/menu/bloc/manage_unit_bloc/manage_unit_event.dart'; import 'package:syncrow_app/features/menu/bloc/manage_unit_bloc/manage_unit_state.dart'; @@ -11,16 +12,24 @@ import 'package:syncrow_app/utils/helpers/snack_bar.dart'; class AssignDeviceView extends StatelessWidget { final String unitId; final String roomId; - const AssignDeviceView({super.key, required this.unitId, required this.roomId}); + final SpaceModel unit; + const AssignDeviceView( + {super.key, + required this.unitId, + required this.roomId, + required this.unit}); @override Widget build(BuildContext context) { return BlocProvider( - create: (context) => ManageUnitBloc()..add(FetchDevicesByRoomIdEvent(roomId: roomId)), - child: BlocConsumer(listener: (context, state) { + create: (context) => ManageUnitBloc() + ..add(FetchDevicesByRoomIdEvent(roomId: roomId, unit: unit)), + child: BlocConsumer( + listener: (context, state) { if (state is FetchDeviceByRoomIdState) { if (state.allDevices.isEmpty) { - CustomSnackBar.displaySnackBar('You do not have the permission to assign devices'); + CustomSnackBar.displaySnackBar( + 'You do not have the permission to assign devices'); Navigator.of(context).pop(); } } @@ -34,7 +43,8 @@ class AssignDeviceView extends StatelessWidget { width: MediaQuery.sizeOf(context).width, height: MediaQuery.sizeOf(context).height, child: GridView.builder( - gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( + gridDelegate: + const SliverGridDelegateWithFixedCrossAxisCount( crossAxisCount: 2, crossAxisSpacing: 10.0, mainAxisSpacing: 10.0, @@ -52,11 +62,14 @@ class AssignDeviceView extends StatelessWidget { ), ), child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - mainAxisAlignment: MainAxisAlignment.spaceBetween, + crossAxisAlignment: + CrossAxisAlignment.start, + mainAxisAlignment: + MainAxisAlignment.spaceBetween, children: [ Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, + mainAxisAlignment: + MainAxisAlignment.spaceBetween, children: [ SvgPicture.asset( state.allDevices[index].icon!, @@ -64,19 +77,27 @@ class AssignDeviceView extends StatelessWidget { ), GestureDetector( onTap: () { - if (state.roomDevicesId[ - state.allDevices[index].uuid!] ?? + if (state.roomDevicesId[state + .allDevices[index] + .uuid!] ?? false == false) { - BlocProvider.of(context).add( - AssignRoomEvent( - deviceId: - state.allDevices[index].uuid ?? '', + BlocProvider.of< + ManageUnitBloc>( + context) + .add(AssignRoomEvent( + deviceId: state + .allDevices[ + index] + .uuid ?? + '', + unit: unit, roomId: roomId)); } }, child: SvgPicture.asset( - state.roomDevicesId[ - state.allDevices[index].uuid!] ?? + state.roomDevicesId[state + .allDevices[index] + .uuid!] ?? false ? Assets.blueCheckboxIcon : Assets.emptyCheckboxIcon, diff --git a/lib/features/menu/view/widgets/manage_home/room_screen.dart b/lib/features/menu/view/widgets/manage_home/room_screen.dart index 12aeac5..1ab85ab 100644 --- a/lib/features/menu/view/widgets/manage_home/room_screen.dart +++ b/lib/features/menu/view/widgets/manage_home/room_screen.dart @@ -64,6 +64,7 @@ class RoomsView extends StatelessWidget { builder: (context) => AssignDeviceView( roomId: state.roomsList[index].id ?? '', unitId: unit.id, + unit: unit, )), ); }, diff --git a/lib/features/scene/bloc/tab_change/tab_change_bloc.dart b/lib/features/scene/bloc/tab_change/tab_change_bloc.dart index a7c6481..7fc18db 100644 --- a/lib/features/scene/bloc/tab_change/tab_change_bloc.dart +++ b/lib/features/scene/bloc/tab_change/tab_change_bloc.dart @@ -17,7 +17,7 @@ class TabBarBloc extends Bloc { if (event.roomId == "-1") { deviceManagerBloc.add(FetchAllDevices()); } else { - deviceManagerBloc.add(FetchDevicesByRoomId(event.roomId)); + deviceManagerBloc.add(FetchDevicesByRoomId(event.roomId,event.unit)); } emit(TabSelected( roomId: event.roomId, selectedTabIndex: event.selectedIndex)); diff --git a/lib/features/scene/bloc/tab_change/tab_change_event.dart b/lib/features/scene/bloc/tab_change/tab_change_event.dart index c052280..f39d823 100644 --- a/lib/features/scene/bloc/tab_change/tab_change_event.dart +++ b/lib/features/scene/bloc/tab_change/tab_change_event.dart @@ -1,3 +1,5 @@ +import 'package:syncrow_app/features/app_layout/model/space_model.dart'; + abstract class TabBarEvent { const TabBarEvent(); } @@ -5,5 +7,7 @@ abstract class TabBarEvent { class TabChanged extends TabBarEvent { final int selectedIndex; final String roomId; - const TabChanged({required this.selectedIndex, required this.roomId}); + final SpaceModel unit; + const TabChanged( + {required this.selectedIndex, required this.roomId, required this.unit}); } diff --git a/lib/features/scene/view/scene_rooms_tabbar.dart b/lib/features/scene/view/scene_rooms_tabbar.dart index 5f4932a..9d0a491 100644 --- a/lib/features/scene/view/scene_rooms_tabbar.dart +++ b/lib/features/scene/view/scene_rooms_tabbar.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart'; +import 'package:syncrow_app/features/app_layout/model/space_model.dart'; import 'package:syncrow_app/features/devices/bloc/devices_cubit.dart'; import 'package:syncrow_app/features/devices/model/room_model.dart'; import 'package:syncrow_app/features/scene/bloc/tab_change/tab_change_bloc.dart'; @@ -27,9 +28,11 @@ class _SceneRoomsTabBarDevicesViewState with SingleTickerProviderStateMixin { late final TabController _tabController; List? rooms = []; + late final SpaceModel selectedSpace; @override void initState() { + selectedSpace = HomeCubit.getInstance().selectedSpace!; rooms = List.from(HomeCubit.getInstance().selectedSpace?.subspaces ?? []); if (rooms != null) { if (rooms![0].id != '-1') { @@ -56,8 +59,10 @@ class _SceneRoomsTabBarDevicesViewState final value = _tabController.index; /// select tab - context.read().add( - TabChanged(selectedIndex: value, roomId: rooms?[value].id ?? '')); + context.read().add(TabChanged( + selectedIndex: value, + roomId: rooms?[value].id ?? '', + unit: selectedSpace)); return; } } diff --git a/lib/navigation/router.dart b/lib/navigation/router.dart index 990b7fb..c09a307 100644 --- a/lib/navigation/router.dart +++ b/lib/navigation/router.dart @@ -1,5 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:syncrow_app/features/app_layout/model/community_model.dart'; +import 'package:syncrow_app/features/app_layout/model/space_model.dart'; import 'package:syncrow_app/features/app_layout/view/app_layout.dart'; import 'package:syncrow_app/features/auth/view/otp_view.dart'; import 'package:syncrow_app/features/auth/view/login_view.dart'; @@ -84,9 +86,18 @@ class Router { DeviceManagerBloc()..add(FetchAllDevices()), ), BlocProvider( - create: (BuildContext context) => TabBarBloc( - context.read()) - ..add(const TabChanged(selectedIndex: 0, roomId: '-1')), + create: (BuildContext context) => + TabBarBloc(context.read()) + ..add(TabChanged( + selectedIndex: 0, + roomId: '-1', + unit: SpaceModel( + id: '-1', + name: '', + community: Community( + uuid: '-1', + name: '', + )))), ), ], child: const SceneRoomsTabBarDevicesView(), diff --git a/lib/services/api/api_links_endpoints.dart b/lib/services/api/api_links_endpoints.dart index dcff924..225fd60 100644 --- a/lib/services/api/api_links_endpoints.dart +++ b/lib/services/api/api_links_endpoints.dart @@ -55,7 +55,8 @@ abstract class ApiEndpoints { static const String addUnitToUser = '/unit/user'; //GET static const String unitByUuid = '/unit/'; - static const String listSubspace = '/communities/{communityUuid}/spaces/{spaceUuid}/subspaces'; + static const String listSubspace = + '/communities/{communityUuid}/spaces/{spaceUuid}/subspaces'; static const String unitParent = '/unit/parent/{unitUuid}'; static const String unitUser = '/unit/user/'; static const String invitationCode = '/unit/{unitUuid}/invitation-code'; @@ -75,10 +76,11 @@ abstract class ApiEndpoints { //PUT static const String renameRoom = '/room/{roomUuid}'; - //SPACE Module //GET static const String userSpaces = '/user/{userUuid}/spaces'; + static const String spaceDevices = + '/communities/{communityUuid}/spaces/{spaceUuid}/devices'; ///Group Module //POST @@ -106,7 +108,8 @@ abstract class ApiEndpoints { static const String openDoorLock = '/door-lock/open/{doorLockUuid}'; //GET - static const String deviceByRoom = '/device/room'; + static const String deviceByRoom = + '/communities/:communityUuid/spaces/:spaceUuid/subspaces/:subSpaceUuid/devices'; static const String deviceByUuid = '/device/{deviceUuid}'; static const String deviceFunctions = '/device/{deviceUuid}/functions'; static const String gatewayApi = '/device/gateway/{gatewayUuid}/devices'; diff --git a/lib/services/api/devices_api.dart b/lib/services/api/devices_api.dart index 8d096f7..60fc203 100644 --- a/lib/services/api/devices_api.dart +++ b/lib/services/api/devices_api.dart @@ -1,6 +1,5 @@ import 'dart:async'; import 'dart:convert'; -import 'dart:developer'; import 'package:syncrow_app/features/devices/model/device_category_model.dart'; import 'package:syncrow_app/features/devices/model/device_control_model.dart'; import 'package:syncrow_app/features/devices/model/device_model.dart'; @@ -130,23 +129,43 @@ class DevicesAPI { return response; } - static Future> getDevicesByRoomId(String roomId) async { - final response = await _httpService.get( - path: ApiEndpoints.deviceByRoom, - queryParameters: {"roomUuid": roomId}, - showServerMessage: false, - expectedResponseModel: (json) { - if (json == null || json.isEmpty || json == []) { - return []; - } - List devices = []; - for (var device in json) { - devices.add(DeviceModel.fromJson(device)); - } - return devices; - }, - ); - return response; + static Future> getDevicesByRoomId({ + required String communityUuid, + required String spaceUuid, + required String roomId, + }) async { + try { + final String path = ApiEndpoints.deviceByRoom + .replaceAll(':communityUuid', communityUuid) + .replaceAll(':spaceUuid', spaceUuid) + .replaceAll(':subSpaceUuid', roomId); + + final response = await _httpService.get( + path: path, + showServerMessage: false, + expectedResponseModel: (json) { + final data = json['data']; + + if (data == null || data.isEmpty) { + return []; + } + if (json == null || json.isEmpty || json == []) { + return []; + } + return data + .map((device) => DeviceModel.fromJson(device)) + .toList(); + }, + ); + + return response; + } catch (e) { + // Log the error if needed + print("Error fetching devices for room: $e"); + + // Return an empty list in case of error + return []; + } } static Future> getDevicesByGatewayId( diff --git a/lib/services/api/home_management_api.dart b/lib/services/api/home_management_api.dart index 9eb35b2..c9183b7 100644 --- a/lib/services/api/home_management_api.dart +++ b/lib/services/api/home_management_api.dart @@ -28,15 +28,38 @@ class HomeManagementAPI { static Future> fetchDevicesByUnitId() async { List list = []; - await _httpService.get( - path: ApiEndpoints.getDevicesByUnitId.replaceAll( - "{unitUuid}", HomeCubit.getInstance().selectedSpace?.id ?? ''), + + try { + // Retrieve selected space details + final selectedSpace = HomeCubit.getInstance().selectedSpace; + final communityUuid = selectedSpace?.community?.uuid ?? ''; + final spaceUuid = selectedSpace?.id ?? ''; + + // Ensure both placeholders are replaced + final path = ApiEndpoints.spaceDevices + .replaceAll("{communityUuid}", communityUuid) + .replaceAll("{spaceUuid}", spaceUuid); + + // Debugging: Log the path + print("Fetching devices with path: $path"); + + await _httpService.get( + path: path, showServerMessage: false, expectedResponseModel: (json) { json.forEach((value) { list.add(DeviceModel.fromJson(value)); }); - }); + }, + ); + + // Debugging: Log successful fetch + print("Successfully fetched ${list.length} devices."); + } catch (e) { + // Log the error for debugging + print("Error fetching devices for the unit: $e"); + } + return list; }