finished rooms call with all devices and single devices call

This commit is contained in:
ashrafzarkanisala
2024-06-23 20:12:38 +03:00
parent b58af41b88
commit 759888857d
8 changed files with 446 additions and 72 deletions
@@ -0,0 +1,158 @@
import 'package:bloc/bloc.dart';
import 'package:flutter/material.dart';
import 'package:syncrow_app/features/devices/bloc/device_manager_bloc/device_manager_event.dart';
import 'package:syncrow_app/features/devices/bloc/device_manager_bloc/device_manager_state.dart';
import 'package:syncrow_app/features/devices/model/device_category_model.dart';
import 'package:syncrow_app/services/api/devices_api.dart';
import 'package:syncrow_app/services/api/home_management_api.dart';
class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
DeviceManagerBloc() : super(DeviceManagerInitial()) {
on<FetchAllDevices>(_onFetchAllDevices);
on<FetchDevicesByRoomId>(_onFetchDevicesByRoomId);
on<SelectCategory>(_onSelectCategory);
on<UnselectAllCategories>(_onUnselectAllCategories);
on<SelectDevice>(_onSelectDevice);
on<ChangeCategorySwitchValue>(_onChangeCategorySwitchValue);
on<TurnOnOffDevice>(_onTurnOnOffDevice);
on<ClearCategoriesSelection>(_onClearCategoriesSelection);
// on<DeviceControl>(_onDeviceControl);
}
static List<DevicesCategoryModel>? allCategories;
Future<void> _onFetchAllDevices(
FetchAllDevices event, Emitter<DeviceManagerState> emit) async {
emit(GetDevicesLoading());
try {
final allDevices = await HomeManagementAPI.fetchDevicesByUserId();
emit(GetDevicesSuccess(allDevices));
} catch (e) {
emit(GetDevicesError(e.toString()));
}
}
Future<void> _onFetchDevicesByRoomId(
FetchDevicesByRoomId event, Emitter<DeviceManagerState> emit) async {
emit(GetDevicesLoading());
try {
final devices = await DevicesAPI.getDevicesByRoomId(event.roomId);
emit(GetDevicesSuccess(devices));
} catch (e) {
emit(GetDevicesError(e.toString()));
}
}
void _onSelectCategory(
SelectCategory event, Emitter<DeviceManagerState> emit) {
for (var i = 0; i < allCategories!.length; i++) {
allCategories![i].isSelected = i == event.index;
}
emit(DevicesCategoryChanged());
}
void _onUnselectAllCategories(
UnselectAllCategories event, Emitter<DeviceManagerState> emit) {
for (var category in allCategories!) {
category.isSelected = false;
}
emit(DevicesCategoryChanged());
}
void _onSelectDevice(SelectDevice event, Emitter<DeviceManagerState> emit) {
for (var category in allCategories!) {
if (category.devices != null) {
for (var device in category.devices!) {
if (device.isSelected) {
category.isSelected = false;
emit(DeviceSelected());
return;
}
}
}
}
event.device.isSelected = !event.device.isSelected;
emit(DeviceSelected());
}
void _onChangeCategorySwitchValue(
ChangeCategorySwitchValue event, Emitter<DeviceManagerState> emit) {
var category = event.category;
if (category.devicesStatus != null) {
category.devicesStatus = !category.devicesStatus!;
if (category.devices != null) {
for (var device in category.devices!) {
device.isOnline = category.devicesStatus;
}
}
} else {
category.devicesStatus = true;
if (category.devices != null) {
for (var device in category.devices!) {
device.isOnline = true;
}
}
}
_updateDevicesStatus(category, emit);
}
void _onTurnOnOffDevice(
TurnOnOffDevice event, Emitter<DeviceManagerState> emit) {
var device = event.device;
device.isOnline = !device.isOnline!;
DevicesCategoryModel category = allCategories!.firstWhere((category) {
return category.devices != null && category.devices!.contains(device);
});
_updateDevicesStatus(category, emit);
}
void _onClearCategoriesSelection(
ClearCategoriesSelection event, Emitter<DeviceManagerState> emit) {
for (var category in allCategories!) {
category.isSelected = false;
if (category.devices != null) {
for (var device in category.devices!) {
device.isSelected = false;
}
}
}
Navigator.popUntil(event.context, (route) => route.isFirst);
emit(DevicesCategoryChanged());
}
// Future<void> _onDeviceControl(
// DeviceControl event, Emitter<DeviceManagerState> emit) async {
// emit(DeviceControlLoading(code: event.control.code));
// try {
// var response =
// await DevicesAPI.controlDevice(event.control, event.deviceId);
// if (response['success'] ?? false) {
// emit(DeviceControlSuccess(code: event.control.code));
// await Future.delayed(const Duration(milliseconds: 400), () {
// add(FetchDevicesByRoomId(event.deviceId));
// });
// } else {
// emit(DeviceControlError('Failed to control the device'));
// }
// } catch (e) {
// emit(DeviceControlError(e.toString()));
// }
// }
void _updateDevicesStatus(
DevicesCategoryModel category, Emitter<DeviceManagerState> emit) {
if (category.devices != null && category.devices!.isNotEmpty) {
bool? tempStatus = category.devices![0].isOnline;
for (var device in category.devices!) {
if (device.isOnline != tempStatus) {
category.devicesStatus = null;
break;
}
category.devicesStatus = tempStatus;
}
emit(CategorySwitchChanged());
}
}
}
@@ -0,0 +1,80 @@
import 'package:equatable/equatable.dart';
import 'package:flutter/material.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_model.dart';
abstract class DeviceManagerEvent extends Equatable {
const DeviceManagerEvent();
@override
List<Object> get props => [];
}
class FetchAllDevices extends DeviceManagerEvent {}
class FetchDevicesByRoomId extends DeviceManagerEvent {
final String roomId;
const FetchDevicesByRoomId(this.roomId);
@override
List<Object> get props => [roomId];
}
class SelectCategory extends DeviceManagerEvent {
final int index;
const SelectCategory(this.index);
@override
List<Object> get props => [index];
}
class UnselectAllCategories extends DeviceManagerEvent {}
class SelectDevice extends DeviceManagerEvent {
final DeviceModel device;
const SelectDevice(this.device);
@override
List<Object> get props => [device];
}
class ChangeCategorySwitchValue extends DeviceManagerEvent {
final DevicesCategoryModel category;
const ChangeCategorySwitchValue(this.category);
@override
List<Object> get props => [category];
}
class TurnOnOffDevice extends DeviceManagerEvent {
final DeviceModel device;
const TurnOnOffDevice(this.device);
@override
List<Object> get props => [device];
}
class ClearCategoriesSelection extends DeviceManagerEvent {
final BuildContext context;
const ClearCategoriesSelection(this.context);
@override
List<Object> get props => [context];
}
class DeviceControl extends DeviceManagerEvent {
final DeviceControlModel control;
final String deviceId;
const DeviceControl(this.control, this.deviceId);
@override
List<Object> get props => [control, deviceId];
}
@@ -0,0 +1,112 @@
import 'package:equatable/equatable.dart';
import 'package:syncrow_app/features/devices/model/device_model.dart';
abstract class DeviceManagerState extends Equatable {
const DeviceManagerState();
@override
List<Object> get props => [];
}
class DeviceManagerInitial extends DeviceManagerState {}
class DevicesCategoryChanged extends DeviceManagerState {}
class DeviceSelected extends DeviceManagerState {}
class DeviceControlLoading extends DeviceManagerState {
final String code;
const DeviceControlLoading({required this.code});
@override
List<Object> get props => [code];
}
class DeviceControlSuccess extends DeviceManagerState {
final String code;
const DeviceControlSuccess({required this.code});
@override
List<Object> get props => [code];
}
class DeviceControlError extends DeviceManagerState {
final String message;
const DeviceControlError(this.message);
@override
List<Object> get props => [message];
}
class DevicesCategoriesLoading extends DeviceManagerState {}
class DevicesCategoriesSuccess extends DeviceManagerState {}
class DevicesCategoriesError extends DeviceManagerState {
final String error;
const DevicesCategoriesError(this.error);
@override
List<Object> get props => [error];
}
class GetDevicesLoading extends DeviceManagerState {}
class GetDevicesSuccess extends DeviceManagerState {
final List<DeviceModel>? devices;
const GetDevicesSuccess(this.devices);
@override
List<Object> get props => [devices ?? []];
}
class GetDevicesError extends DeviceManagerState {
final String error;
const GetDevicesError(this.error);
@override
List<Object> get props => [error];
}
class GetDeviceStatusLoading extends DeviceManagerState {
final String? code;
const GetDeviceStatusLoading({this.code});
@override
List<Object> get props => [code ?? ''];
}
class GetDeviceStatusSuccess extends DeviceManagerState {
final String? code;
const GetDeviceStatusSuccess({this.code});
@override
List<Object> get props => [code ?? ''];
}
class GetDeviceStatusError extends DeviceManagerState {
final String error;
const GetDeviceStatusError(this.error);
@override
List<Object> get props => [error];
}
class CategorySwitchChanged extends DeviceManagerState {}
class DeviceSwitchChanged extends DeviceManagerState {}
class CurtainsIsOpening extends DeviceManagerState {}
class CurtainsIsClosing extends DeviceManagerState {}
class CurtainsStopped extends DeviceManagerState {}
+15 -4
View File
@@ -99,6 +99,20 @@ class DevicesCubit extends Cubit<DevicesState> {
return null;
}
// Getter to retrieve all devices from HomeCubit
List<DeviceModel> get allDevices {
List<DeviceModel> devices = [];
if (HomeCubit.getInstance().selectedSpace != null &&
HomeCubit.getInstance().selectedSpace!.rooms != null) {
for (var room in HomeCubit.getInstance().selectedSpace!.rooms!) {
if (room.devices != null) {
devices.addAll(room.devices!);
}
}
}
return devices;
}
// DevicesCategoryModel? get chosenCategory {
// for (var category in allCategories!) {
// if (category.isSelected) {
@@ -290,9 +304,6 @@ class DevicesCubit extends Cubit<DevicesState> {
fetchDevicesByRoomId(String? roomId) async {
if (roomId == null) return;
if (roomId == '-1') {
return fetchAllDevices(roomId);
}
emitSafe(GetDevicesLoading());
int roomIndex = HomeCubit.getInstance()
@@ -320,7 +331,7 @@ class DevicesCubit extends Cubit<DevicesState> {
// }
}
void fetchAllDevices(String roomId) async {
void fetchAllDevices() async {
emitSafe(GetDevicesLoading());
try {
final allDevices = await HomeManagementAPI.fetchDevicesByUserId();