mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2026-10-23 10:47:55 +00:00
added unit devices
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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];
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -64,6 +64,7 @@ class RoomsView extends StatelessWidget {
|
||||
builder: (context) => AssignDeviceView(
|
||||
roomId: state.roomsList[index].id ?? '',
|
||||
unitId: unit.id,
|
||||
unit: unit,
|
||||
)),
|
||||
);
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user