added unit devices

This commit is contained in:
hannathkadher
2024-10-30 19:16:10 +04:00
parent 5c65bac076
commit 13a4bf25b3
15 changed files with 220 additions and 88 deletions

View File

@ -260,6 +260,7 @@ class HomeCubit extends Cubit<HomeState> {
try {
spaces = await SpacesAPI.getSpacesByUserId();
} catch (failure) {
print(failure);
emitSafe(GetSpacesError("No units found"));
return;
}

View File

@ -25,7 +25,8 @@ class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
static List<DevicesCategoryModel>? allCategories;
Future<void> _onFetchAllDevices(FetchAllDevices event, Emitter<DeviceManagerState> emit) async {
Future<void> _onFetchAllDevices(
FetchAllDevices event, Emitter<DeviceManagerState> emit) async {
emit(state.copyWith(loading: true));
try {
final allDevices = await HomeManagementAPI.fetchDevicesByUnitId();
@ -39,21 +40,27 @@ class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
FetchDevicesByRoomId event, Emitter<DeviceManagerState> 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<DeviceManagerState> emit) {
void _onSelectCategory(
SelectCategory event, Emitter<DeviceManagerState> 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<DeviceManagerState> emit) {
void _onUnselectAllCategories(
UnselectAllCategories event, Emitter<DeviceManagerState> emit) {
for (var category in allCategories!) {
category.isSelected = false;
}
@ -97,7 +104,8 @@ class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
_updateDevicesStatus(category, emit);
}
void _onTurnOnOffDevice(TurnOnOffDevice event, Emitter<DeviceManagerState> emit) {
void _onTurnOnOffDevice(
TurnOnOffDevice event, Emitter<DeviceManagerState> emit) {
var device = event.device;
device.isOnline = !device.isOnline!;
DevicesCategoryModel category = allCategories!.firstWhere((category) {
@ -120,7 +128,8 @@ class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
emit(state.copyWith(categoryChanged: true)); // Set category changed state
}
void _updateDevicesStatus(DevicesCategoryModel category, Emitter<DeviceManagerState> emit) {
void _updateDevicesStatus(
DevicesCategoryModel category, Emitter<DeviceManagerState> 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<DeviceManagerEvent, DeviceManagerState> {
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()));
}

View File

@ -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<Object> get props => [roomId];

View File

@ -23,14 +23,22 @@ import 'package:syncrow_app/utils/resource_manager/constants.dart';
part 'devices_state.dart';
class DevicesCubit extends Cubit<DevicesState> {
Future<void> _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<DevicesState> {
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<DevicesState> {
}
}
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<DevicesState> {
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<DevicesState> {
// emitSafe(LightBrightnessChanged(value));
// }
// }
}
enum LightMode {

View File

@ -19,10 +19,12 @@ class ManageUnitBloc extends Bloc<ManageUnitEvent, ManageUnitState> {
on<AddNewRoom>(_addNewRoom);
}
void _fetchRoomsAndDevices(FetchRoomsEvent event, Emitter<ManageUnitState> emit) async {
void _fetchRoomsAndDevices(
FetchRoomsEvent event, Emitter<ManageUnitState> 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<ManageUnitEvent, ManageUnitState> {
}
}
void _fetchDevicesByRoomId(FetchDevicesByRoomIdEvent event, Emitter<ManageUnitState> emit) async {
void _fetchDevicesByRoomId(
FetchDevicesByRoomIdEvent event, Emitter<ManageUnitState> emit) async {
try {
Map<String, bool> 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<String> allDevicesIds = [];
@ -61,13 +67,20 @@ class ManageUnitBloc extends Bloc<ManageUnitEvent, ManageUnitState> {
}
}
void _assignDevice(AssignRoomEvent event, Emitter<ManageUnitState> emit) async {
void _assignDevice(
AssignRoomEvent event, Emitter<ManageUnitState> emit) async {
try {
Map<String, bool> roomDevicesId = {};
emit(LoadingState());
Map<String, String> body = {"deviceUuid": event.deviceId, "roomUuid": event.roomId};
Map<String, String> 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<String> allDevicesIds = [];
@ -99,7 +112,8 @@ class ManageUnitBloc extends Bloc<ManageUnitEvent, ManageUnitState> {
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();

View File

@ -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<Object> get props => [roomId];
List<Object> 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<Object> get props => [roomId];
List<Object> get props => [roomId, unit];
}

View File

@ -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<ManageUnitBloc, ManageUnitState>(listener: (context, state) {
create: (context) => ManageUnitBloc()
..add(FetchDevicesByRoomIdEvent(roomId: roomId, unit: unit)),
child: BlocConsumer<ManageUnitBloc, ManageUnitState>(
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<ManageUnitBloc>(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,

View File

@ -64,6 +64,7 @@ class RoomsView extends StatelessWidget {
builder: (context) => AssignDeviceView(
roomId: state.roomsList[index].id ?? '',
unitId: unit.id,
unit: unit,
)),
);
},

View File

@ -17,7 +17,7 @@ class TabBarBloc extends Bloc<TabBarEvent, TabBarState> {
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));

View File

@ -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});
}

View File

@ -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<RoomModel>? 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<TabBarBloc>().add(
TabChanged(selectedIndex: value, roomId: rooms?[value].id ?? ''));
context.read<TabBarBloc>().add(TabChanged(
selectedIndex: value,
roomId: rooms?[value].id ?? '',
unit: selectedSpace));
return;
}
}

View File

@ -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<DeviceManagerBloc>())
..add(const TabChanged(selectedIndex: 0, roomId: '-1')),
create: (BuildContext context) =>
TabBarBloc(context.read<DeviceManagerBloc>())
..add(TabChanged(
selectedIndex: 0,
roomId: '-1',
unit: SpaceModel(
id: '-1',
name: '',
community: Community(
uuid: '-1',
name: '',
)))),
),
],
child: const SceneRoomsTabBarDevicesView(),

View File

@ -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';

View File

@ -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<List<DeviceModel>> 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 <DeviceModel>[];
}
List<DeviceModel> devices = [];
for (var device in json) {
devices.add(DeviceModel.fromJson(device));
}
return devices;
},
);
return response;
static Future<List<DeviceModel>> 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 <DeviceModel>[];
}
if (json == null || json.isEmpty || json == []) {
return <DeviceModel>[];
}
return data
.map<DeviceModel>((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 <DeviceModel>[];
}
}
static Future<List<DeviceModel>> getDevicesByGatewayId(

View File

@ -28,15 +28,38 @@ class HomeManagementAPI {
static Future<List<DeviceModel>> fetchDevicesByUnitId() async {
List<DeviceModel> 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;
}