mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-17 02:25:16 +00:00
added unit devices
This commit is contained in:
@ -260,6 +260,7 @@ class HomeCubit extends Cubit<HomeState> {
|
|||||||
try {
|
try {
|
||||||
spaces = await SpacesAPI.getSpacesByUserId();
|
spaces = await SpacesAPI.getSpacesByUserId();
|
||||||
} catch (failure) {
|
} catch (failure) {
|
||||||
|
print(failure);
|
||||||
emitSafe(GetSpacesError("No units found"));
|
emitSafe(GetSpacesError("No units found"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,8 @@ class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
|
|||||||
|
|
||||||
static List<DevicesCategoryModel>? allCategories;
|
static List<DevicesCategoryModel>? allCategories;
|
||||||
|
|
||||||
Future<void> _onFetchAllDevices(FetchAllDevices event, Emitter<DeviceManagerState> emit) async {
|
Future<void> _onFetchAllDevices(
|
||||||
|
FetchAllDevices event, Emitter<DeviceManagerState> emit) async {
|
||||||
emit(state.copyWith(loading: true));
|
emit(state.copyWith(loading: true));
|
||||||
try {
|
try {
|
||||||
final allDevices = await HomeManagementAPI.fetchDevicesByUnitId();
|
final allDevices = await HomeManagementAPI.fetchDevicesByUnitId();
|
||||||
@ -39,21 +40,27 @@ class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
|
|||||||
FetchDevicesByRoomId event, Emitter<DeviceManagerState> emit) async {
|
FetchDevicesByRoomId event, Emitter<DeviceManagerState> emit) async {
|
||||||
emit(state.copyWith(loading: true));
|
emit(state.copyWith(loading: true));
|
||||||
try {
|
try {
|
||||||
final devices = await DevicesAPI.getDevicesByRoomId(event.roomId);
|
final devices = await DevicesAPI.getDevicesByRoomId(
|
||||||
|
communityUuid: event.unit.community.uuid,
|
||||||
|
spaceUuid: event.unit.id,
|
||||||
|
roomId: event.roomId,
|
||||||
|
);
|
||||||
emit(state.copyWith(devices: devices, loading: false));
|
emit(state.copyWith(devices: devices, loading: false));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
emit(state.copyWith(error: e.toString(), loading: false));
|
emit(state.copyWith(error: e.toString(), loading: false));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onSelectCategory(SelectCategory event, Emitter<DeviceManagerState> emit) {
|
void _onSelectCategory(
|
||||||
|
SelectCategory event, Emitter<DeviceManagerState> emit) {
|
||||||
for (var i = 0; i < allCategories!.length; i++) {
|
for (var i = 0; i < allCategories!.length; i++) {
|
||||||
allCategories![i].isSelected = i == event.index;
|
allCategories![i].isSelected = i == event.index;
|
||||||
}
|
}
|
||||||
emit(state.copyWith(categoryChanged: true));
|
emit(state.copyWith(categoryChanged: true));
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onUnselectAllCategories(UnselectAllCategories event, Emitter<DeviceManagerState> emit) {
|
void _onUnselectAllCategories(
|
||||||
|
UnselectAllCategories event, Emitter<DeviceManagerState> emit) {
|
||||||
for (var category in allCategories!) {
|
for (var category in allCategories!) {
|
||||||
category.isSelected = false;
|
category.isSelected = false;
|
||||||
}
|
}
|
||||||
@ -97,7 +104,8 @@ class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
|
|||||||
_updateDevicesStatus(category, emit);
|
_updateDevicesStatus(category, emit);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onTurnOnOffDevice(TurnOnOffDevice event, Emitter<DeviceManagerState> emit) {
|
void _onTurnOnOffDevice(
|
||||||
|
TurnOnOffDevice event, Emitter<DeviceManagerState> emit) {
|
||||||
var device = event.device;
|
var device = event.device;
|
||||||
device.isOnline = !device.isOnline!;
|
device.isOnline = !device.isOnline!;
|
||||||
DevicesCategoryModel category = allCategories!.firstWhere((category) {
|
DevicesCategoryModel category = allCategories!.firstWhere((category) {
|
||||||
@ -120,7 +128,8 @@ class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
|
|||||||
emit(state.copyWith(categoryChanged: true)); // Set category changed state
|
emit(state.copyWith(categoryChanged: true)); // Set category changed state
|
||||||
}
|
}
|
||||||
|
|
||||||
void _updateDevicesStatus(DevicesCategoryModel category, Emitter<DeviceManagerState> emit) {
|
void _updateDevicesStatus(
|
||||||
|
DevicesCategoryModel category, Emitter<DeviceManagerState> emit) {
|
||||||
if (category.devices != null && category.devices!.isNotEmpty) {
|
if (category.devices != null && category.devices!.isNotEmpty) {
|
||||||
bool? tempStatus = category.devices![0].isOnline;
|
bool? tempStatus = category.devices![0].isOnline;
|
||||||
for (var device in category.devices!) {
|
for (var device in category.devices!) {
|
||||||
@ -140,7 +149,8 @@ class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
|
|||||||
try {
|
try {
|
||||||
final deviceFunctions = await DevicesAPI.deviceFunctions(event.deviceId);
|
final deviceFunctions = await DevicesAPI.deviceFunctions(event.deviceId);
|
||||||
|
|
||||||
emit(state.copyWith(functionsLoading: false, deviceFunctions: deviceFunctions));
|
emit(state.copyWith(
|
||||||
|
functionsLoading: false, deviceFunctions: deviceFunctions));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
emit(state.copyWith(functionsLoading: false, error: e.toString()));
|
emit(state.copyWith(functionsLoading: false, error: e.toString()));
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import 'package:equatable/equatable.dart';
|
import 'package:equatable/equatable.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:syncrow_app/features/app_layout/model/space_model.dart';
|
||||||
import 'package:syncrow_app/features/devices/model/device_category_model.dart';
|
import 'package:syncrow_app/features/devices/model/device_category_model.dart';
|
||||||
import 'package:syncrow_app/features/devices/model/device_control_model.dart';
|
import 'package:syncrow_app/features/devices/model/device_control_model.dart';
|
||||||
import 'package:syncrow_app/features/devices/model/device_model.dart';
|
import 'package:syncrow_app/features/devices/model/device_model.dart';
|
||||||
@ -15,8 +16,9 @@ class FetchAllDevices extends DeviceManagerEvent {}
|
|||||||
|
|
||||||
class FetchDevicesByRoomId extends DeviceManagerEvent {
|
class FetchDevicesByRoomId extends DeviceManagerEvent {
|
||||||
final String roomId;
|
final String roomId;
|
||||||
|
final SpaceModel unit;
|
||||||
|
|
||||||
const FetchDevicesByRoomId(this.roomId);
|
const FetchDevicesByRoomId(this.roomId, this.unit);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<Object> get props => [roomId];
|
List<Object> get props => [roomId];
|
||||||
|
@ -23,14 +23,22 @@ import 'package:syncrow_app/utils/resource_manager/constants.dart';
|
|||||||
part 'devices_state.dart';
|
part 'devices_state.dart';
|
||||||
|
|
||||||
class DevicesCubit extends Cubit<DevicesState> {
|
class DevicesCubit extends Cubit<DevicesState> {
|
||||||
|
Future<void> _initializeDevices(SpaceModel selectedSpace) async {
|
||||||
|
// Fetch devices for each room in the selected space
|
||||||
|
for (var room in selectedSpace.subspaces ?? []) {
|
||||||
|
await fetchDevicesByRoomId(selectedSpace, room.id!);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch groups based on the selected space ID
|
||||||
|
await fetchGroups(selectedSpace.id ?? '');
|
||||||
|
}
|
||||||
|
|
||||||
DevicesCubit._() : super(DevicesInitial()) {
|
DevicesCubit._() : super(DevicesInitial()) {
|
||||||
if (HomeCubit.getInstance().selectedSpace != null &&
|
final selectedSpace = HomeCubit.getInstance().selectedSpace;
|
||||||
HomeCubit.getInstance().spaces!.isNotEmpty) {
|
final spaces = HomeCubit.getInstance().spaces;
|
||||||
// fetchGroups(HomeCubit.getInstance().selectedSpace!.id!);
|
|
||||||
for (var room in HomeCubit.getInstance().selectedSpace!.subspaces!) {
|
if (selectedSpace != null && spaces != null && spaces.isNotEmpty) {
|
||||||
fetchDevicesByRoomId(room.id!);
|
_initializeDevices(selectedSpace);
|
||||||
}
|
|
||||||
fetchGroups(HomeCubit.getInstance().selectedSpace?.id ?? '');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,10 +95,10 @@ class DevicesCubit extends Cubit<DevicesState> {
|
|||||||
return const DoorsListView();
|
return const DoorsListView();
|
||||||
case DeviceType.Curtain:
|
case DeviceType.Curtain:
|
||||||
return const CurtainListView();
|
return const CurtainListView();
|
||||||
// case DeviceType.ThreeGang:
|
// case DeviceType.ThreeGang:
|
||||||
// return const ThreeGangSwitchesView();
|
// return const ThreeGangSwitchesView();
|
||||||
// case DeviceType.Gateway:
|
// case DeviceType.Gateway:
|
||||||
// return const GateWayView();
|
// return const GateWayView();
|
||||||
default:
|
default:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -303,20 +311,26 @@ class DevicesCubit extends Cubit<DevicesState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchDevicesByRoomId(String? roomId) async {
|
fetchDevicesByRoomId(SpaceModel? unit, String? roomId) async {
|
||||||
if (roomId == null) return;
|
if (roomId == null) return;
|
||||||
|
|
||||||
emitSafe(GetDevicesLoading());
|
emitSafe(GetDevicesLoading());
|
||||||
int roomIndex =
|
int roomIndex = HomeCubit.getInstance()
|
||||||
HomeCubit.getInstance().selectedSpace!.subspaces!.indexWhere((element) => element.id == roomId);
|
.selectedSpace!
|
||||||
|
.subspaces!
|
||||||
|
.indexWhere((element) => element.id == roomId);
|
||||||
try {
|
try {
|
||||||
HomeCubit.getInstance().selectedSpace!.subspaces![roomIndex].devices =
|
HomeCubit.getInstance().selectedSpace!.subspaces![roomIndex].devices =
|
||||||
await DevicesAPI.getDevicesByRoomId(roomId);
|
await DevicesAPI.getDevicesByRoomId(
|
||||||
|
communityUuid: unit!.community.uuid,
|
||||||
|
spaceUuid: unit.id,
|
||||||
|
roomId: roomId);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
emitSafe(GetDevicesError(e.toString()));
|
emitSafe(GetDevicesError(e.toString()));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final devices = HomeCubit.getInstance().selectedSpace!.subspaces![roomIndex].devices;
|
final devices =
|
||||||
|
HomeCubit.getInstance().selectedSpace!.subspaces![roomIndex].devices;
|
||||||
emitSafe(GetDevicesSuccess(devices));
|
emitSafe(GetDevicesSuccess(devices));
|
||||||
|
|
||||||
//get status for each device
|
//get status for each device
|
||||||
@ -346,8 +360,11 @@ class DevicesCubit extends Cubit<DevicesState> {
|
|||||||
emitSafe(GetDeviceStatusError(e.toString()));
|
emitSafe(GetDeviceStatusError(e.toString()));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
HomeCubit.getInstance().selectedSpace!.subspaces![roomIndex].devices![deviceIndex].status =
|
HomeCubit.getInstance()
|
||||||
statuses;
|
.selectedSpace!
|
||||||
|
.subspaces![roomIndex]
|
||||||
|
.devices![deviceIndex]
|
||||||
|
.status = statuses;
|
||||||
emitSafe(GetDeviceStatusSuccess(code: code));
|
emitSafe(GetDeviceStatusSuccess(code: code));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -396,8 +413,6 @@ class DevicesCubit extends Cubit<DevicesState> {
|
|||||||
// emitSafe(LightBrightnessChanged(value));
|
// emitSafe(LightBrightnessChanged(value));
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
enum LightMode {
|
enum LightMode {
|
||||||
|
@ -19,10 +19,12 @@ class ManageUnitBloc extends Bloc<ManageUnitEvent, ManageUnitState> {
|
|||||||
on<AddNewRoom>(_addNewRoom);
|
on<AddNewRoom>(_addNewRoom);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _fetchRoomsAndDevices(FetchRoomsEvent event, Emitter<ManageUnitState> emit) async {
|
void _fetchRoomsAndDevices(
|
||||||
|
FetchRoomsEvent event, Emitter<ManageUnitState> emit) async {
|
||||||
try {
|
try {
|
||||||
emit(LoadingState());
|
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));
|
emit(FetchRoomsState(devicesList: allDevices, roomsList: roomsList));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
emit(const ErrorState(message: 'Something went wrong'));
|
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 {
|
try {
|
||||||
Map<String, bool> roomDevicesId = {};
|
Map<String, bool> roomDevicesId = {};
|
||||||
emit(LoadingState());
|
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();
|
allDevices = await HomeManagementAPI.fetchDevicesByUserId();
|
||||||
|
|
||||||
List<String> allDevicesIds = [];
|
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 {
|
try {
|
||||||
Map<String, bool> roomDevicesId = {};
|
Map<String, bool> roomDevicesId = {};
|
||||||
emit(LoadingState());
|
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);
|
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 = [];
|
List<String> allDevicesIds = [];
|
||||||
|
|
||||||
@ -99,7 +112,8 @@ class ManageUnitBloc extends Bloc<ManageUnitEvent, ManageUnitState> {
|
|||||||
emit(LoadingState());
|
emit(LoadingState());
|
||||||
final response = await HomeCreation.createRoom(body);
|
final response = await HomeCreation.createRoom(body);
|
||||||
if (response['data']['uuid'] != '') {
|
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();
|
allDevices = await HomeManagementAPI.fetchDevicesByUserId();
|
||||||
emit(FetchRoomsState(devicesList: allDevices, roomsList: roomsList));
|
emit(FetchRoomsState(devicesList: allDevices, roomsList: roomsList));
|
||||||
await HomeCubit.getInstance().fetchUnitsByUserId();
|
await HomeCubit.getInstance().fetchUnitsByUserId();
|
||||||
|
@ -23,11 +23,12 @@ class FetchRoomsEvent extends ManageUnitEvent {
|
|||||||
|
|
||||||
class FetchDevicesByRoomIdEvent extends ManageUnitEvent {
|
class FetchDevicesByRoomIdEvent extends ManageUnitEvent {
|
||||||
final String roomId;
|
final String roomId;
|
||||||
|
final SpaceModel unit;
|
||||||
|
|
||||||
const FetchDevicesByRoomIdEvent({required this.roomId});
|
const FetchDevicesByRoomIdEvent({required this.roomId, required this.unit});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<Object> get props => [roomId];
|
List<Object> get props => [roomId, unit];
|
||||||
}
|
}
|
||||||
|
|
||||||
class AddNewRoom extends ManageUnitEvent {
|
class AddNewRoom extends ManageUnitEvent {
|
||||||
@ -43,9 +44,11 @@ class AddNewRoom extends ManageUnitEvent {
|
|||||||
class AssignRoomEvent extends ManageUnitEvent {
|
class AssignRoomEvent extends ManageUnitEvent {
|
||||||
final String roomId;
|
final String roomId;
|
||||||
final String deviceId;
|
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
|
@override
|
||||||
List<Object> get props => [roomId];
|
List<Object> get props => [roomId, unit];
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:flutter_svg/svg.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_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_event.dart';
|
||||||
import 'package:syncrow_app/features/menu/bloc/manage_unit_bloc/manage_unit_state.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 {
|
class AssignDeviceView extends StatelessWidget {
|
||||||
final String unitId;
|
final String unitId;
|
||||||
final String roomId;
|
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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return BlocProvider(
|
return BlocProvider(
|
||||||
create: (context) => ManageUnitBloc()..add(FetchDevicesByRoomIdEvent(roomId: roomId)),
|
create: (context) => ManageUnitBloc()
|
||||||
child: BlocConsumer<ManageUnitBloc, ManageUnitState>(listener: (context, state) {
|
..add(FetchDevicesByRoomIdEvent(roomId: roomId, unit: unit)),
|
||||||
|
child: BlocConsumer<ManageUnitBloc, ManageUnitState>(
|
||||||
|
listener: (context, state) {
|
||||||
if (state is FetchDeviceByRoomIdState) {
|
if (state is FetchDeviceByRoomIdState) {
|
||||||
if (state.allDevices.isEmpty) {
|
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();
|
Navigator.of(context).pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -34,7 +43,8 @@ class AssignDeviceView extends StatelessWidget {
|
|||||||
width: MediaQuery.sizeOf(context).width,
|
width: MediaQuery.sizeOf(context).width,
|
||||||
height: MediaQuery.sizeOf(context).height,
|
height: MediaQuery.sizeOf(context).height,
|
||||||
child: GridView.builder(
|
child: GridView.builder(
|
||||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
gridDelegate:
|
||||||
|
const SliverGridDelegateWithFixedCrossAxisCount(
|
||||||
crossAxisCount: 2,
|
crossAxisCount: 2,
|
||||||
crossAxisSpacing: 10.0,
|
crossAxisSpacing: 10.0,
|
||||||
mainAxisSpacing: 10.0,
|
mainAxisSpacing: 10.0,
|
||||||
@ -52,11 +62,14 @@ class AssignDeviceView extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
child: Column(
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment:
|
||||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
CrossAxisAlignment.start,
|
||||||
|
mainAxisAlignment:
|
||||||
|
MainAxisAlignment.spaceBetween,
|
||||||
children: [
|
children: [
|
||||||
Row(
|
Row(
|
||||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
mainAxisAlignment:
|
||||||
|
MainAxisAlignment.spaceBetween,
|
||||||
children: [
|
children: [
|
||||||
SvgPicture.asset(
|
SvgPicture.asset(
|
||||||
state.allDevices[index].icon!,
|
state.allDevices[index].icon!,
|
||||||
@ -64,19 +77,27 @@ class AssignDeviceView extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
GestureDetector(
|
GestureDetector(
|
||||||
onTap: () {
|
onTap: () {
|
||||||
if (state.roomDevicesId[
|
if (state.roomDevicesId[state
|
||||||
state.allDevices[index].uuid!] ??
|
.allDevices[index]
|
||||||
|
.uuid!] ??
|
||||||
false == false) {
|
false == false) {
|
||||||
BlocProvider.of<ManageUnitBloc>(context).add(
|
BlocProvider.of<
|
||||||
AssignRoomEvent(
|
ManageUnitBloc>(
|
||||||
deviceId:
|
context)
|
||||||
state.allDevices[index].uuid ?? '',
|
.add(AssignRoomEvent(
|
||||||
|
deviceId: state
|
||||||
|
.allDevices[
|
||||||
|
index]
|
||||||
|
.uuid ??
|
||||||
|
'',
|
||||||
|
unit: unit,
|
||||||
roomId: roomId));
|
roomId: roomId));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
child: SvgPicture.asset(
|
child: SvgPicture.asset(
|
||||||
state.roomDevicesId[
|
state.roomDevicesId[state
|
||||||
state.allDevices[index].uuid!] ??
|
.allDevices[index]
|
||||||
|
.uuid!] ??
|
||||||
false
|
false
|
||||||
? Assets.blueCheckboxIcon
|
? Assets.blueCheckboxIcon
|
||||||
: Assets.emptyCheckboxIcon,
|
: Assets.emptyCheckboxIcon,
|
||||||
|
@ -64,6 +64,7 @@ class RoomsView extends StatelessWidget {
|
|||||||
builder: (context) => AssignDeviceView(
|
builder: (context) => AssignDeviceView(
|
||||||
roomId: state.roomsList[index].id ?? '',
|
roomId: state.roomsList[index].id ?? '',
|
||||||
unitId: unit.id,
|
unitId: unit.id,
|
||||||
|
unit: unit,
|
||||||
)),
|
)),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
@ -17,7 +17,7 @@ class TabBarBloc extends Bloc<TabBarEvent, TabBarState> {
|
|||||||
if (event.roomId == "-1") {
|
if (event.roomId == "-1") {
|
||||||
deviceManagerBloc.add(FetchAllDevices());
|
deviceManagerBloc.add(FetchAllDevices());
|
||||||
} else {
|
} else {
|
||||||
deviceManagerBloc.add(FetchDevicesByRoomId(event.roomId));
|
deviceManagerBloc.add(FetchDevicesByRoomId(event.roomId,event.unit));
|
||||||
}
|
}
|
||||||
emit(TabSelected(
|
emit(TabSelected(
|
||||||
roomId: event.roomId, selectedTabIndex: event.selectedIndex));
|
roomId: event.roomId, selectedTabIndex: event.selectedIndex));
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import 'package:syncrow_app/features/app_layout/model/space_model.dart';
|
||||||
|
|
||||||
abstract class TabBarEvent {
|
abstract class TabBarEvent {
|
||||||
const TabBarEvent();
|
const TabBarEvent();
|
||||||
}
|
}
|
||||||
@ -5,5 +7,7 @@ abstract class TabBarEvent {
|
|||||||
class TabChanged extends TabBarEvent {
|
class TabChanged extends TabBarEvent {
|
||||||
final int selectedIndex;
|
final int selectedIndex;
|
||||||
final String roomId;
|
final String roomId;
|
||||||
const TabChanged({required this.selectedIndex, required this.roomId});
|
final SpaceModel unit;
|
||||||
|
const TabChanged(
|
||||||
|
{required this.selectedIndex, required this.roomId, required this.unit});
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
|
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
|
||||||
|
import 'package:syncrow_app/features/app_layout/model/space_model.dart';
|
||||||
import 'package:syncrow_app/features/devices/bloc/devices_cubit.dart';
|
import 'package:syncrow_app/features/devices/bloc/devices_cubit.dart';
|
||||||
import 'package:syncrow_app/features/devices/model/room_model.dart';
|
import 'package:syncrow_app/features/devices/model/room_model.dart';
|
||||||
import 'package:syncrow_app/features/scene/bloc/tab_change/tab_change_bloc.dart';
|
import 'package:syncrow_app/features/scene/bloc/tab_change/tab_change_bloc.dart';
|
||||||
@ -27,9 +28,11 @@ class _SceneRoomsTabBarDevicesViewState
|
|||||||
with SingleTickerProviderStateMixin {
|
with SingleTickerProviderStateMixin {
|
||||||
late final TabController _tabController;
|
late final TabController _tabController;
|
||||||
List<RoomModel>? rooms = [];
|
List<RoomModel>? rooms = [];
|
||||||
|
late final SpaceModel selectedSpace;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
|
selectedSpace = HomeCubit.getInstance().selectedSpace!;
|
||||||
rooms = List.from(HomeCubit.getInstance().selectedSpace?.subspaces ?? []);
|
rooms = List.from(HomeCubit.getInstance().selectedSpace?.subspaces ?? []);
|
||||||
if (rooms != null) {
|
if (rooms != null) {
|
||||||
if (rooms![0].id != '-1') {
|
if (rooms![0].id != '-1') {
|
||||||
@ -56,8 +59,10 @@ class _SceneRoomsTabBarDevicesViewState
|
|||||||
final value = _tabController.index;
|
final value = _tabController.index;
|
||||||
|
|
||||||
/// select tab
|
/// select tab
|
||||||
context.read<TabBarBloc>().add(
|
context.read<TabBarBloc>().add(TabChanged(
|
||||||
TabChanged(selectedIndex: value, roomId: rooms?[value].id ?? ''));
|
selectedIndex: value,
|
||||||
|
roomId: rooms?[value].id ?? '',
|
||||||
|
unit: selectedSpace));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:syncrow_app/features/app_layout/model/community_model.dart';
|
||||||
|
import 'package:syncrow_app/features/app_layout/model/space_model.dart';
|
||||||
import 'package:syncrow_app/features/app_layout/view/app_layout.dart';
|
import 'package:syncrow_app/features/app_layout/view/app_layout.dart';
|
||||||
import 'package:syncrow_app/features/auth/view/otp_view.dart';
|
import 'package:syncrow_app/features/auth/view/otp_view.dart';
|
||||||
import 'package:syncrow_app/features/auth/view/login_view.dart';
|
import 'package:syncrow_app/features/auth/view/login_view.dart';
|
||||||
@ -84,9 +86,18 @@ class Router {
|
|||||||
DeviceManagerBloc()..add(FetchAllDevices()),
|
DeviceManagerBloc()..add(FetchAllDevices()),
|
||||||
),
|
),
|
||||||
BlocProvider(
|
BlocProvider(
|
||||||
create: (BuildContext context) => TabBarBloc(
|
create: (BuildContext context) =>
|
||||||
context.read<DeviceManagerBloc>())
|
TabBarBloc(context.read<DeviceManagerBloc>())
|
||||||
..add(const TabChanged(selectedIndex: 0, roomId: '-1')),
|
..add(TabChanged(
|
||||||
|
selectedIndex: 0,
|
||||||
|
roomId: '-1',
|
||||||
|
unit: SpaceModel(
|
||||||
|
id: '-1',
|
||||||
|
name: '',
|
||||||
|
community: Community(
|
||||||
|
uuid: '-1',
|
||||||
|
name: '',
|
||||||
|
)))),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
child: const SceneRoomsTabBarDevicesView(),
|
child: const SceneRoomsTabBarDevicesView(),
|
||||||
|
@ -55,7 +55,8 @@ abstract class ApiEndpoints {
|
|||||||
static const String addUnitToUser = '/unit/user';
|
static const String addUnitToUser = '/unit/user';
|
||||||
//GET
|
//GET
|
||||||
static const String unitByUuid = '/unit/';
|
static const String unitByUuid = '/unit/';
|
||||||
static const String listSubspace = '/communities/{communityUuid}/spaces/{spaceUuid}/subspaces';
|
static const String listSubspace =
|
||||||
|
'/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 = '/unit/{unitUuid}/invitation-code';
|
||||||
@ -75,10 +76,11 @@ abstract class ApiEndpoints {
|
|||||||
//PUT
|
//PUT
|
||||||
static const String renameRoom = '/room/{roomUuid}';
|
static const String renameRoom = '/room/{roomUuid}';
|
||||||
|
|
||||||
|
|
||||||
//SPACE Module
|
//SPACE Module
|
||||||
//GET
|
//GET
|
||||||
static const String userSpaces = '/user/{userUuid}/spaces';
|
static const String userSpaces = '/user/{userUuid}/spaces';
|
||||||
|
static const String spaceDevices =
|
||||||
|
'/communities/{communityUuid}/spaces/{spaceUuid}/devices';
|
||||||
|
|
||||||
///Group Module
|
///Group Module
|
||||||
//POST
|
//POST
|
||||||
@ -106,7 +108,8 @@ abstract class ApiEndpoints {
|
|||||||
static const String openDoorLock = '/door-lock/open/{doorLockUuid}';
|
static const String openDoorLock = '/door-lock/open/{doorLockUuid}';
|
||||||
|
|
||||||
//GET
|
//GET
|
||||||
static const String deviceByRoom = '/device/room';
|
static const String deviceByRoom =
|
||||||
|
'/communities/:communityUuid/spaces/:spaceUuid/subspaces/:subSpaceUuid/devices';
|
||||||
static const String deviceByUuid = '/device/{deviceUuid}';
|
static const String deviceByUuid = '/device/{deviceUuid}';
|
||||||
static const String deviceFunctions = '/device/{deviceUuid}/functions';
|
static const String deviceFunctions = '/device/{deviceUuid}/functions';
|
||||||
static const String gatewayApi = '/device/gateway/{gatewayUuid}/devices';
|
static const String gatewayApi = '/device/gateway/{gatewayUuid}/devices';
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:developer';
|
|
||||||
import 'package:syncrow_app/features/devices/model/device_category_model.dart';
|
import 'package:syncrow_app/features/devices/model/device_category_model.dart';
|
||||||
import 'package:syncrow_app/features/devices/model/device_control_model.dart';
|
import 'package:syncrow_app/features/devices/model/device_control_model.dart';
|
||||||
import 'package:syncrow_app/features/devices/model/device_model.dart';
|
import 'package:syncrow_app/features/devices/model/device_model.dart';
|
||||||
@ -130,23 +129,43 @@ class DevicesAPI {
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future<List<DeviceModel>> getDevicesByRoomId(String roomId) async {
|
static Future<List<DeviceModel>> getDevicesByRoomId({
|
||||||
final response = await _httpService.get(
|
required String communityUuid,
|
||||||
path: ApiEndpoints.deviceByRoom,
|
required String spaceUuid,
|
||||||
queryParameters: {"roomUuid": roomId},
|
required String roomId,
|
||||||
showServerMessage: false,
|
}) async {
|
||||||
expectedResponseModel: (json) {
|
try {
|
||||||
if (json == null || json.isEmpty || json == []) {
|
final String path = ApiEndpoints.deviceByRoom
|
||||||
return <DeviceModel>[];
|
.replaceAll(':communityUuid', communityUuid)
|
||||||
}
|
.replaceAll(':spaceUuid', spaceUuid)
|
||||||
List<DeviceModel> devices = [];
|
.replaceAll(':subSpaceUuid', roomId);
|
||||||
for (var device in json) {
|
|
||||||
devices.add(DeviceModel.fromJson(device));
|
final response = await _httpService.get(
|
||||||
}
|
path: path,
|
||||||
return devices;
|
showServerMessage: false,
|
||||||
},
|
expectedResponseModel: (json) {
|
||||||
);
|
final data = json['data'];
|
||||||
return response;
|
|
||||||
|
if (data == null || data.isEmpty) {
|
||||||
|
return <DeviceModel>[];
|
||||||
|
}
|
||||||
|
if (json == null || json.isEmpty || json == []) {
|
||||||
|
return <DeviceModel>[];
|
||||||
|
}
|
||||||
|
return data
|
||||||
|
.map<DeviceModel>((device) => DeviceModel.fromJson(device))
|
||||||
|
.toList();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
return response;
|
||||||
|
} catch (e) {
|
||||||
|
// Log the error if needed
|
||||||
|
print("Error fetching devices for room: $e");
|
||||||
|
|
||||||
|
// Return an empty list in case of error
|
||||||
|
return <DeviceModel>[];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future<List<DeviceModel>> getDevicesByGatewayId(
|
static Future<List<DeviceModel>> getDevicesByGatewayId(
|
||||||
|
@ -28,15 +28,38 @@ class HomeManagementAPI {
|
|||||||
|
|
||||||
static Future<List<DeviceModel>> fetchDevicesByUnitId() async {
|
static Future<List<DeviceModel>> fetchDevicesByUnitId() async {
|
||||||
List<DeviceModel> list = [];
|
List<DeviceModel> list = [];
|
||||||
await _httpService.get(
|
|
||||||
path: ApiEndpoints.getDevicesByUnitId.replaceAll(
|
try {
|
||||||
"{unitUuid}", HomeCubit.getInstance().selectedSpace?.id ?? ''),
|
// Retrieve selected space details
|
||||||
|
final selectedSpace = HomeCubit.getInstance().selectedSpace;
|
||||||
|
final communityUuid = selectedSpace?.community?.uuid ?? '';
|
||||||
|
final spaceUuid = selectedSpace?.id ?? '';
|
||||||
|
|
||||||
|
// Ensure both placeholders are replaced
|
||||||
|
final path = ApiEndpoints.spaceDevices
|
||||||
|
.replaceAll("{communityUuid}", communityUuid)
|
||||||
|
.replaceAll("{spaceUuid}", spaceUuid);
|
||||||
|
|
||||||
|
// Debugging: Log the path
|
||||||
|
print("Fetching devices with path: $path");
|
||||||
|
|
||||||
|
await _httpService.get(
|
||||||
|
path: path,
|
||||||
showServerMessage: false,
|
showServerMessage: false,
|
||||||
expectedResponseModel: (json) {
|
expectedResponseModel: (json) {
|
||||||
json.forEach((value) {
|
json.forEach((value) {
|
||||||
list.add(DeviceModel.fromJson(value));
|
list.add(DeviceModel.fromJson(value));
|
||||||
});
|
});
|
||||||
});
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
// Debugging: Log successful fetch
|
||||||
|
print("Successfully fetched ${list.length} devices.");
|
||||||
|
} catch (e) {
|
||||||
|
// Log the error for debugging
|
||||||
|
print("Error fetching devices for the unit: $e");
|
||||||
|
}
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user