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 { Future<bool> joinAUnit(String code) async {
try { try {
var uuid = var userUuid =
await const FlutterSecureStorage().read(key: UserModel.userUuidKey) ?? 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) { if (success) {
await fetchUnitsByUserId(); await fetchUnitsByUserId();
CustomSnackBar.displaySnackBar('Done successfully'); CustomSnackBar.displaySnackBar('Done successfully');

View File

@ -17,6 +17,7 @@ class ManageUnitBloc extends Bloc<ManageUnitEvent, ManageUnitState> {
on<FetchDevicesByRoomIdEvent>(_fetchDevicesByRoomId); on<FetchDevicesByRoomIdEvent>(_fetchDevicesByRoomId);
on<AssignRoomEvent>(_assignDevice); on<AssignRoomEvent>(_assignDevice);
on<AddNewRoom>(_addNewRoom); on<AddNewRoom>(_addNewRoom);
on<UnassignRoomEvent>(_unassignDevice);
} }
void _fetchRoomsAndDevices( void _fetchRoomsAndDevices(
@ -72,11 +73,9 @@ class ManageUnitBloc extends Bloc<ManageUnitEvent, ManageUnitState> {
try { try {
Map<String, bool> roomDevicesId = {}; Map<String, bool> roomDevicesId = {};
emit(LoadingState()); emit(LoadingState());
Map<String, String> body = {
"deviceUuid": event.deviceId, await HomeManagementAPI.assignDeviceToRoom(
"roomUuid": event.roomId event.unit.community.uuid, event.unit.id, event.roomId, event.deviceId);
};
await HomeManagementAPI.assignDeviceToRoom(body);
final devicesList = await DevicesAPI.getDevicesByRoomId( final devicesList = await DevicesAPI.getDevicesByRoomId(
communityUuid: event.unit.community.uuid, communityUuid: event.unit.community.uuid,
spaceUuid: event.unit.id, 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 { _addNewRoom(AddNewRoom event, Emitter<ManageUnitState> emit) async {
Map<String, String> body = {'subspaceName': event.roomName}; Map<String, String> body = {'subspaceName': event.roomName};
try { try {

View File

@ -52,3 +52,15 @@ class AssignRoomEvent extends ManageUnitEvent {
@override @override
List<Object> get props => [roomId, unit]; 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, controller: textEditingController,
decoration: InputDecoration( decoration: InputDecoration(
hintText: 'Invitatoin code', hintText: 'Invitatoin code',
hintStyle: context.bodyMedium.copyWith(color: Colors.grey), hintStyle:
context.bodyMedium.copyWith(color: Colors.grey),
border: InputBorder.none, border: InputBorder.none,
), ),
), ),
@ -52,10 +53,12 @@ class JoinHomeView extends StatelessWidget {
IconButton( IconButton(
onPressed: () async { onPressed: () async {
if (textEditingController.text.isEmpty) { if (textEditingController.text.isEmpty) {
CustomSnackBar.displaySnackBar('Please enter the invitation code'); CustomSnackBar.displaySnackBar(
'Please enter the invitation code');
return; return;
} }
if (await HomeCubit.getInstance().joinAUnit(textEditingController.text)) { if (await HomeCubit.getInstance()
.joinAUnit(textEditingController.text)) {
CustomSnackBar.displaySnackBar('Done successfully'); CustomSnackBar.displaySnackBar('Done successfully');
Navigator.of(context).pop(); Navigator.of(context).pop();
} else { } else {

View File

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

View File

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

View File

@ -59,12 +59,15 @@ class HomeManagementAPI {
return list; return list;
} }
static Future<Map<String, dynamic>> assignDeviceToRoom( static Future<Map<String, dynamic>> assignDeviceToRoom(String communityId,
Map<String, String> body) async { String spaceId, String subSpaceId, String deviceId) async {
try { try {
final response = await _httpService.put( final response = await _httpService.post(
path: ApiEndpoints.assignDeviceToRoom, path: ApiEndpoints.assignDeviceToRoom
body: body, .replaceAll('{communityUuid}', communityId)
.replaceAll('{spaceUuid}', spaceId)
.replaceAll('{subSpaceUuid}', subSpaceId)
.replaceAll('{deviceUuid}', deviceId),
expectedResponseModel: (json) { expectedResponseModel: (json) {
return json; return json;
}, },
@ -74,4 +77,24 @@ class HomeManagementAPI {
rethrow; 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( static Future<bool> joinUnit(
String userId,
Map<String, String> body, Map<String, String> body,
) async { ) async {
final response = await _httpService.post( final response = await _httpService.post(
path: ApiEndpoints.verifyInvitationCode, path: ApiEndpoints.verifyInvitationCode.replaceAll('{userUuid}', userId),
showServerMessage: false, showServerMessage: false,
body: body, body: body,
expectedResponseModel: (json) => json['success'], expectedResponseModel: (json) => json['success'],