diff --git a/lib/features/app_layout/bloc/home_cubit.dart b/lib/features/app_layout/bloc/home_cubit.dart index 8f7f572..35dc9ba 100644 --- a/lib/features/app_layout/bloc/home_cubit.dart +++ b/lib/features/app_layout/bloc/home_cubit.dart @@ -20,17 +20,13 @@ class HomeCubit extends Cubit { // Create a private static instance variable static HomeCubit? _instance; HomeCubit._() : super(HomeInitial()) { - if (spaces != null) { - if (selectedSpace == null) { - fetchSpaces().then((value) { - if (selectedSpace != null) { - print('selectedSpace: ${selectedSpace!.name}'); - fetchRooms(selectedSpace!); - } - }); - } - } else { - fetchSpaces(); // this is for the first time + if (selectedSpace == null) { + fetchSpaces().then((value) { + if (selectedSpace != null) { + print('selectedSpace: ${selectedSpace!.name}'); + fetchRooms(selectedSpace!); + } + }); } } static HomeCubit getInstance() { @@ -128,7 +124,7 @@ class HomeCubit extends Cubit { fetchRooms(SpaceModel space) async { emit(GetSpaceRoomsLoading()); try { - space.rooms = await SpacesAPI.getRooms(space.id!); + space.rooms = await SpacesAPI.getRoomsBySpaceId(space.id!); if (space.rooms != null) { emit(GetSpaceRoomsLoaded(space.rooms!)); } else { diff --git a/lib/features/devices/bloc/devices_cubit.dart b/lib/features/devices/bloc/devices_cubit.dart index 2dd5ecf..1d3add2 100644 --- a/lib/features/devices/bloc/devices_cubit.dart +++ b/lib/features/devices/bloc/devices_cubit.dart @@ -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 { 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 { } } + 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); diff --git a/lib/features/devices/bloc/devices_state.dart b/lib/features/devices/bloc/devices_state.dart index b7b3fa7..4fd8b2d 100644 --- a/lib/features/devices/bloc/devices_state.dart +++ b/lib/features/devices/bloc/devices_state.dart @@ -14,6 +14,17 @@ class DevicesFailure extends DevicesState {} class ChangeIndex extends DevicesState {} // Devices + +class GetDevicesLoading extends DevicesState {} + +class GetDevicesSuccess extends DevicesState {} + +class GetDevicesError extends DevicesState { + final String errorMsg; + + GetDevicesError(this.errorMsg); +} + class DevicesCategoryChanged extends DevicesState {} class CategorySwitchChanged extends DevicesState {} diff --git a/lib/features/devices/model/room_model.dart b/lib/features/devices/model/room_model.dart index 3937f17..8dfbff8 100644 --- a/lib/features/devices/model/room_model.dart +++ b/lib/features/devices/model/room_model.dart @@ -1,30 +1,37 @@ import 'package:syncrow_app/features/devices/model/device_category_model.dart'; +import 'package:syncrow_app/features/devices/model/device_model.dart'; class RoomModel { final int? id; final String? name; - final List? categories; + List? devices; RoomModel({ required this.id, required this.name, - required this.categories, + required this.devices, }); Map toJson() { return { 'id': id, 'name': name, - 'devices': categories, + 'devices': devices, }; } factory RoomModel.fromJson(Map json) { + List devices = []; + if (json['devices'] != null) { + for (var device in json['devices']) { + devices.add(DeviceModel.fromJson(device)); + } + } return RoomModel( id: json['roomId'], name: json['roomName'], - categories: json['devices'], + devices: devices, ); } } diff --git a/lib/features/devices/view/widgets/room_page.dart b/lib/features/devices/view/widgets/room_page.dart index 7f97b81..3e03f39 100644 --- a/lib/features/devices/view/widgets/room_page.dart +++ b/lib/features/devices/view/widgets/room_page.dart @@ -13,10 +13,6 @@ class RoomPage extends StatelessWidget { @override Widget build(BuildContext context) { - List devices = []; - for (var category in room.categories ?? []) { - devices.addAll(category.devices); - } return Padding( padding: const EdgeInsets.symmetric(horizontal: Constants.defaultPadding), child: SingleChildScrollView( @@ -32,9 +28,9 @@ class RoomPage extends StatelessWidget { padding: const EdgeInsets.only(top: 10), physics: const NeverScrollableScrollPhysics(), shrinkWrap: true, - itemCount: devices.length, + itemCount: room.devices!.length, itemBuilder: (_, index) { - return RoomPageSwitch(device: devices[index]); + return RoomPageSwitch(device: room.devices![index]); }, ); }, diff --git a/lib/services/api/api_links_endpoints.dart b/lib/services/api/api_links_endpoints.dart index bfe22be..f249a1d 100644 --- a/lib/services/api/api_links_endpoints.dart +++ b/lib/services/api/api_links_endpoints.dart @@ -16,6 +16,7 @@ abstract class ApiEndpoints { // Devices static const String control = '$baseUrl/device/control'; + static const String devicesByRoom = '$baseUrl/device/room'; //groups static const String groups = '$baseUrl/group'; diff --git a/lib/services/api/http_service.dart b/lib/services/api/http_service.dart index a5f24bd..9551d4a 100644 --- a/lib/services/api/http_service.dart +++ b/lib/services/api/http_service.dart @@ -37,9 +37,6 @@ class HTTPService { path, queryParameters: queryParameters, ); - - // debugPrint("status code is ${response.statusCode}"); - // debugPrint("response data is ${response.data}"); return expectedResponseModel(response.data); } catch (error) { debugPrint("******* Error"); diff --git a/lib/services/api/spaces_api.dart b/lib/services/api/spaces_api.dart index 3f93385..ebf2597 100644 --- a/lib/services/api/spaces_api.dart +++ b/lib/services/api/spaces_api.dart @@ -1,6 +1,7 @@ import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:syncrow_app/features/app_layout/model/space_model.dart'; import 'package:syncrow_app/features/auth/model/user_model.dart'; +import 'package:syncrow_app/features/devices/model/device_model.dart'; import 'package:syncrow_app/features/devices/model/room_model.dart'; import 'package:syncrow_app/services/api/api_links_endpoints.dart'; import 'package:syncrow_app/services/api/http_service.dart'; @@ -23,7 +24,7 @@ class SpacesAPI { } //get rooms by space id - static Future> getRooms(int spaceId) async { + static Future> getRoomsBySpaceId(int spaceId) async { final response = await _httpService.get( path: ApiEndpoints.rooms, queryParameters: {"homeId": spaceId}, @@ -38,4 +39,20 @@ class SpacesAPI { ); return response; } + + static Future> getDevicesByRoomId(int roomId) async { + final response = await _httpService.get( + path: ApiEndpoints.devicesByRoom, + queryParameters: {"roomId": roomId, "pageSize": 10}, + showServerMessage: false, + expectedResponseModel: (json) { + List devices = []; + for (var device in json['devices']) { + devices.add(DeviceModel.fromJson(device)); + } + return devices; + }, + ); + return response; + } }