updated add to room, and verify to space

This commit is contained in:
hannathkadher
2024-11-05 12:27:27 +04:00
parent 34536e6584
commit 52f6be3db0
8 changed files with 121 additions and 26 deletions

View File

@ -239,12 +239,12 @@ class HomeCubit extends Cubit<HomeState> {
Future<bool> joinAUnit(String code) async {
try {
var uuid =
var userUuid =
await const FlutterSecureStorage().read(key: UserModel.userUuidKey) ??
'';
Map<String, String> body = {'userUuid': uuid, 'inviteCode': code};
Map<String, String> body = {'inviteCode': code};
final success = await SpacesAPI.joinUnit(body);
final success = await SpacesAPI.joinUnit(userUuid, body);
if (success) {
await fetchUnitsByUserId();
CustomSnackBar.displaySnackBar('Done successfully');

View File

@ -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 {

View File

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

View File

@ -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 {

View File

@ -77,10 +77,25 @@ class AssignDeviceView extends StatelessWidget {
),
GestureDetector(
onTap: () {
if (state.roomDevicesId[state
bool isAssigned =
state.roomDevicesId[state
.allDevices[index]
.uuid!] ??
false == false) {
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)

View File

@ -53,14 +53,17 @@ abstract class ApiEndpoints {
//POST
static const String addUnit = '/unit';
static const String addUnitToUser = '/unit/user';
static const String verifyInvitationCode =
'/user/{userUuid}/spaces/verify-code';
//GET
static const String unitByUuid = '/unit/';
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';
static const String verifyInvitationCode = '/unit/user/verify-code';
static const String invitationCode =
'/communities/{communityUuid}/spaces/{unitUuid}/invitation-code';
//PUT
static const String renameUnit = '/unit/{unitUuid}';
@ -131,7 +134,8 @@ abstract class ApiEndpoints {
//PUT
static const String editDevicePermission = '/device-permission/edit/{userId}';
static const String assignDeviceToRoom = '/device/room';
static const String assignDeviceToRoom =
'/communities/{communityUuid}/spaces/{spaceUuid}/subspaces/{subSpaceUuid}/devices/{deviceUuid}';
/// Scene & Automation API ////////////////////
/// POST
@ -162,8 +166,7 @@ abstract class ApiEndpoints {
/// DELETE
static const String deleteScene = '/scene/tap-to-run/{sceneId}';
static const String deleteAutomation =
'/automation/{automationId}';
static const String deleteAutomation = '/automation/{automationId}';
//////////////////////Door Lock //////////////////////
//online

View File

@ -59,12 +59,15 @@ class HomeManagementAPI {
return list;
}
static Future<Map<String, dynamic>> assignDeviceToRoom(
Map<String, String> body) async {
static Future<Map<String, dynamic>> assignDeviceToRoom(String communityId,
String spaceId, String subSpaceId, String deviceId) async {
try {
final response = await _httpService.put(
path: ApiEndpoints.assignDeviceToRoom,
body: body,
final response = await _httpService.post(
path: ApiEndpoints.assignDeviceToRoom
.replaceAll('{communityUuid}', communityId)
.replaceAll('{spaceUuid}', spaceId)
.replaceAll('{subSpaceUuid}', subSpaceId)
.replaceAll('{deviceUuid}', deviceId),
expectedResponseModel: (json) {
return json;
},
@ -74,4 +77,24 @@ class HomeManagementAPI {
rethrow;
}
}
static Future<Map<String, dynamic>> unAssignDeviceToRoom(String communityId,
String spaceId, String subSpaceId, String deviceId) async {
try {
final response = await _httpService.delete(
path: ApiEndpoints.assignDeviceToRoom
.replaceAll('{communityUuid}', communityId)
.replaceAll('{spaceUuid}', spaceId)
.replaceAll('{subSpaceUuid}', subSpaceId)
.replaceAll('{deviceUuid}', deviceId),
expectedResponseModel: (json) {
return json;
},
);
return response;
} catch (e) {
rethrow;
}
}
}

View File

@ -79,10 +79,11 @@ class SpacesAPI {
}
static Future<bool> joinUnit(
String userId,
Map<String, String> body,
) async {
final response = await _httpService.post(
path: ApiEndpoints.verifyInvitationCode,
path: ApiEndpoints.verifyInvitationCode.replaceAll('{userUuid}', userId),
showServerMessage: false,
body: body,
expectedResponseModel: (json) => json['success'],