mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2026-10-26 03:17:55 +00:00
Implemented home management and user invitation flows
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
|
||||
import 'package:syncrow_app/features/auth/model/user_model.dart';
|
||||
import 'package:syncrow_app/features/menu/bloc/create_unit_bloc/create_unit_event.dart';
|
||||
import 'package:syncrow_app/features/menu/bloc/create_unit_bloc/create_unit_state.dart';
|
||||
import 'package:syncrow_app/services/api/home_creation_api.dart';
|
||||
import 'package:syncrow_app/utils/helpers/snack_bar.dart';
|
||||
|
||||
class CreateUnitBloc extends Bloc<CreateUnitEvent, CreateUnitState> {
|
||||
List<String> rooms = [];
|
||||
@@ -45,11 +47,16 @@ class CreateUnitBloc extends Bloc<CreateUnitEvent, CreateUnitState> {
|
||||
|
||||
void _save(SaveRooms event, Emitter<CreateUnitState> emit) async {
|
||||
try {
|
||||
emit(LoadingState());
|
||||
if (rooms.isEmpty) {
|
||||
const ErrorState(message: 'Please add at least one room!');
|
||||
if (event.unitName.isEmpty) {
|
||||
CustomSnackBar.displaySnackBar('Please enter the unit name');
|
||||
return;
|
||||
}
|
||||
if (rooms.isEmpty) {
|
||||
CustomSnackBar.displaySnackBar('Please add at least one room!');
|
||||
return;
|
||||
}
|
||||
|
||||
emit(LoadingState());
|
||||
|
||||
var storage = const FlutterSecureStorage();
|
||||
var userId = await storage.read(key: UserModel.userUuidKey) ?? '';
|
||||
@@ -83,6 +90,7 @@ class CreateUnitBloc extends Bloc<CreateUnitEvent, CreateUnitState> {
|
||||
}
|
||||
// }
|
||||
}
|
||||
await HomeCubit.getInstance().fetchUnitsByUserId();
|
||||
emit(RoomsSavedSuccessfully());
|
||||
} catch (_) {
|
||||
emit(const ErrorState(message: 'Something went wrong'));
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:syncrow_app/features/devices/model/device_model.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';
|
||||
import 'package:syncrow_app/services/api/devices_api.dart';
|
||||
import 'package:syncrow_app/services/api/home_creation_api.dart';
|
||||
import 'package:syncrow_app/services/api/home_management_api.dart';
|
||||
import 'package:syncrow_app/services/api/spaces_api.dart';
|
||||
|
||||
class ManageUnitBloc extends Bloc<ManageUnitEvent, ManageUnitState> {
|
||||
List<DeviceModel> allDevices = [];
|
||||
String unitName = '';
|
||||
|
||||
ManageUnitBloc() : super(InitialState()) {
|
||||
on<FetchRoomsEvent>(_fetchRoomsAndDevices);
|
||||
on<FetchDevicesByRoomIdEvent>(_fetchDevicesByRoomId);
|
||||
on<AssignRoomEvent>(_assignDevice);
|
||||
on<AddNewRoom>(_addNewRoom);
|
||||
}
|
||||
|
||||
void _fetchRoomsAndDevices(FetchRoomsEvent event, Emitter<ManageUnitState> emit) async {
|
||||
try {
|
||||
emit(LoadingState());
|
||||
final roomsList = await SpacesAPI.getRoomsBySpaceId(event.unitId);
|
||||
emit(FetchRoomsState(devicesList: allDevices, roomsList: roomsList));
|
||||
} catch (e) {
|
||||
emit(const ErrorState(message: 'Something went wrong'));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void _fetchDevicesByRoomId(FetchDevicesByRoomIdEvent event, Emitter<ManageUnitState> emit) async {
|
||||
try {
|
||||
Map<String, bool> roomDevicesId = {};
|
||||
emit(LoadingState());
|
||||
final devicesList = await DevicesAPI.getDevicesByRoomId(event.roomId);
|
||||
allDevices = await HomeManagementAPI.fetchDevicesByUserId();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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};
|
||||
await HomeManagementAPI.assignDeviceToRoom(body);
|
||||
final devicesList = await DevicesAPI.getDevicesByRoomId(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 = {'roomName': event.roomName, 'unitUuid': event.unitId};
|
||||
try {
|
||||
emit(LoadingState());
|
||||
final response = await HomeCreation.createRoom(body);
|
||||
if (response['data']['uuid'] != '') {
|
||||
final roomsList = await SpacesAPI.getRoomsBySpaceId(event.unitId);
|
||||
allDevices = await HomeManagementAPI.fetchDevicesByUserId();
|
||||
emit(FetchRoomsState(devicesList: allDevices, roomsList: roomsList));
|
||||
} else {
|
||||
emit(const ErrorState(message: 'Something went wrong'));
|
||||
}
|
||||
} catch (_) {
|
||||
emit(const ErrorState(message: 'Something went wrong'));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
abstract class ManageUnitEvent extends Equatable {
|
||||
const ManageUnitEvent();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
class InitialEvent extends ManageUnitEvent {}
|
||||
|
||||
class LoadingEvent extends ManageUnitEvent {}
|
||||
|
||||
class FetchRoomsEvent extends ManageUnitEvent {
|
||||
final String unitId;
|
||||
|
||||
const FetchRoomsEvent({required this.unitId});
|
||||
|
||||
@override
|
||||
List<Object> get props => [unitId];
|
||||
}
|
||||
|
||||
class FetchDevicesByRoomIdEvent extends ManageUnitEvent {
|
||||
final String roomId;
|
||||
|
||||
const FetchDevicesByRoomIdEvent({required this.roomId});
|
||||
|
||||
@override
|
||||
List<Object> get props => [roomId];
|
||||
}
|
||||
|
||||
class AddNewRoom extends ManageUnitEvent {
|
||||
final String roomName;
|
||||
final String unitId;
|
||||
|
||||
const AddNewRoom({required this.roomName, required this.unitId});
|
||||
|
||||
@override
|
||||
List<Object> get props => [roomName, unitId];
|
||||
}
|
||||
|
||||
class AssignRoomEvent extends ManageUnitEvent {
|
||||
final String roomId;
|
||||
final String deviceId;
|
||||
|
||||
const AssignRoomEvent({required this.roomId, required this.deviceId});
|
||||
|
||||
@override
|
||||
List<Object> get props => [roomId];
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:syncrow_app/features/devices/model/device_model.dart';
|
||||
import 'package:syncrow_app/features/devices/model/room_model.dart';
|
||||
|
||||
abstract class ManageUnitState extends Equatable {
|
||||
const ManageUnitState();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
class InitialState extends ManageUnitState {}
|
||||
|
||||
class LoadingState extends ManageUnitState {}
|
||||
|
||||
class FetchRoomsState extends ManageUnitState {
|
||||
final List<RoomModel> roomsList;
|
||||
final List<DeviceModel> devicesList;
|
||||
|
||||
const FetchRoomsState({required this.devicesList, required this.roomsList});
|
||||
|
||||
@override
|
||||
List<Object> get props => [devicesList];
|
||||
}
|
||||
|
||||
class FetchDeviceByRoomIdState extends ManageUnitState {
|
||||
final List<DeviceModel> roomDevices;
|
||||
final List<DeviceModel> allDevices;
|
||||
final Map<String, bool> roomDevicesId;
|
||||
final String roomId;
|
||||
|
||||
const FetchDeviceByRoomIdState(
|
||||
{required this.roomDevices,
|
||||
required this.allDevices,
|
||||
required this.roomDevicesId,
|
||||
required this.roomId});
|
||||
|
||||
@override
|
||||
List<Object> get props => [roomDevices, allDevices, roomDevicesId, roomId];
|
||||
}
|
||||
|
||||
class AssignRoomState extends ManageUnitState {}
|
||||
|
||||
class ErrorState extends ManageUnitState {
|
||||
final String message;
|
||||
|
||||
const ErrorState({required this.message});
|
||||
|
||||
@override
|
||||
List<Object> get props => [message];
|
||||
}
|
||||
Reference in New Issue
Block a user