mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2026-10-23 11:57:54 +00:00
updated add to room, and verify to space
This commit is contained in:
@@ -17,6 +17,7 @@ class ManageUnitBloc extends Bloc<ManageUnitEvent, ManageUnitState> {
|
||||
on<FetchDevicesByRoomIdEvent>(_fetchDevicesByRoomId);
|
||||
on<AssignRoomEvent>(_assignDevice);
|
||||
on<AddNewRoom>(_addNewRoom);
|
||||
on<UnassignRoomEvent>(_unassignDevice);
|
||||
}
|
||||
|
||||
void _fetchRoomsAndDevices(
|
||||
@@ -72,11 +73,9 @@ class ManageUnitBloc extends Bloc<ManageUnitEvent, ManageUnitState> {
|
||||
try {
|
||||
Map<String, bool> roomDevicesId = {};
|
||||
emit(LoadingState());
|
||||
Map<String, String> body = {
|
||||
"deviceUuid": event.deviceId,
|
||||
"roomUuid": event.roomId
|
||||
};
|
||||
await HomeManagementAPI.assignDeviceToRoom(body);
|
||||
|
||||
await HomeManagementAPI.assignDeviceToRoom(
|
||||
event.unit.community.uuid, event.unit.id, event.roomId, event.deviceId);
|
||||
final devicesList = await DevicesAPI.getDevicesByRoomId(
|
||||
communityUuid: event.unit.community.uuid,
|
||||
spaceUuid: event.unit.id,
|
||||
@@ -106,6 +105,45 @@ class ManageUnitBloc extends Bloc<ManageUnitEvent, ManageUnitState> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void _unassignDevice(
|
||||
UnassignRoomEvent event, Emitter<ManageUnitState> emit) async {
|
||||
try {
|
||||
Map<String, bool> roomDevicesId = {};
|
||||
emit(LoadingState());
|
||||
|
||||
await HomeManagementAPI.unAssignDeviceToRoom(
|
||||
event.unit.community.uuid, event.unit.id, event.roomId, event.deviceId);
|
||||
final devicesList = await DevicesAPI.getDevicesByRoomId(
|
||||
communityUuid: event.unit.community.uuid,
|
||||
spaceUuid: event.unit.id,
|
||||
roomId: event.roomId);
|
||||
|
||||
List<String> allDevicesIds = [];
|
||||
|
||||
allDevices.forEach((element) {
|
||||
allDevicesIds.add(element.uuid!);
|
||||
});
|
||||
|
||||
devicesList.forEach((e) {
|
||||
if (allDevicesIds.contains(e.uuid!)) {
|
||||
roomDevicesId[e.uuid!] = true;
|
||||
} else {
|
||||
roomDevicesId[e.uuid!] = false;
|
||||
}
|
||||
});
|
||||
emit(FetchDeviceByRoomIdState(
|
||||
roomDevices: devicesList,
|
||||
allDevices: allDevices,
|
||||
roomDevicesId: roomDevicesId,
|
||||
roomId: event.roomId));
|
||||
} catch (e) {
|
||||
emit(const ErrorState(message: 'Something went wrong'));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
_addNewRoom(AddNewRoom event, Emitter<ManageUnitState> emit) async {
|
||||
Map<String, String> body = {'subspaceName': event.roomName};
|
||||
try {
|
||||
|
||||
@@ -52,3 +52,15 @@ class AssignRoomEvent extends ManageUnitEvent {
|
||||
@override
|
||||
List<Object> get props => [roomId, unit];
|
||||
}
|
||||
|
||||
class UnassignRoomEvent extends ManageUnitEvent {
|
||||
final String roomId;
|
||||
final String deviceId;
|
||||
final SpaceModel unit;
|
||||
|
||||
const UnassignRoomEvent(
|
||||
{required this.roomId, required this.deviceId, required this.unit});
|
||||
|
||||
@override
|
||||
List<Object> get props => [roomId, unit];
|
||||
}
|
||||
|
||||
@@ -44,7 +44,8 @@ class JoinHomeView extends StatelessWidget {
|
||||
controller: textEditingController,
|
||||
decoration: InputDecoration(
|
||||
hintText: 'Invitatoin code',
|
||||
hintStyle: context.bodyMedium.copyWith(color: Colors.grey),
|
||||
hintStyle:
|
||||
context.bodyMedium.copyWith(color: Colors.grey),
|
||||
border: InputBorder.none,
|
||||
),
|
||||
),
|
||||
@@ -52,10 +53,12 @@ class JoinHomeView extends StatelessWidget {
|
||||
IconButton(
|
||||
onPressed: () async {
|
||||
if (textEditingController.text.isEmpty) {
|
||||
CustomSnackBar.displaySnackBar('Please enter the invitation code');
|
||||
CustomSnackBar.displaySnackBar(
|
||||
'Please enter the invitation code');
|
||||
return;
|
||||
}
|
||||
if (await HomeCubit.getInstance().joinAUnit(textEditingController.text)) {
|
||||
if (await HomeCubit.getInstance()
|
||||
.joinAUnit(textEditingController.text)) {
|
||||
CustomSnackBar.displaySnackBar('Done successfully');
|
||||
Navigator.of(context).pop();
|
||||
} else {
|
||||
|
||||
@@ -77,10 +77,25 @@ class AssignDeviceView extends StatelessWidget {
|
||||
),
|
||||
GestureDetector(
|
||||
onTap: () {
|
||||
if (state.roomDevicesId[state
|
||||
.allDevices[index]
|
||||
.uuid!] ??
|
||||
false == false) {
|
||||
bool isAssigned =
|
||||
state.roomDevicesId[state
|
||||
.allDevices[index]
|
||||
.uuid!] ??
|
||||
false;
|
||||
if (isAssigned) {
|
||||
BlocProvider.of<
|
||||
ManageUnitBloc>(
|
||||
context)
|
||||
.add(UnassignRoomEvent(
|
||||
deviceId: state
|
||||
.allDevices[
|
||||
index]
|
||||
.uuid ??
|
||||
'',
|
||||
unit: unit,
|
||||
roomId: roomId));
|
||||
} else {
|
||||
// Tick (assign) the device
|
||||
BlocProvider.of<
|
||||
ManageUnitBloc>(
|
||||
context)
|
||||
|
||||
Reference in New Issue
Block a user