mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2026-10-25 10:47:55 +00:00
working on functions view
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
@@ -9,7 +11,7 @@ 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()) {
|
||||
DeviceManagerBloc() : super(DeviceManagerState.initial()) {
|
||||
on<FetchAllDevices>(_onFetchAllDevices);
|
||||
on<FetchDevicesByRoomId>(_onFetchDevicesByRoomId);
|
||||
on<SelectCategory>(_onSelectCategory);
|
||||
@@ -18,30 +20,30 @@ class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
|
||||
on<ChangeCategorySwitchValue>(_onChangeCategorySwitchValue);
|
||||
on<TurnOnOffDevice>(_onTurnOnOffDevice);
|
||||
on<ClearCategoriesSelection>(_onClearCategoriesSelection);
|
||||
// on<DeviceControl>(_onDeviceControl);
|
||||
on<DeviceFunctionsEvent>(_getDeviceFunctions);
|
||||
}
|
||||
|
||||
static List<DevicesCategoryModel>? allCategories;
|
||||
|
||||
Future<void> _onFetchAllDevices(
|
||||
FetchAllDevices event, Emitter<DeviceManagerState> emit) async {
|
||||
emit(GetDevicesLoading());
|
||||
emit(state.copyWith(loading: true));
|
||||
try {
|
||||
final allDevices = await HomeManagementAPI.fetchDevicesByUserId();
|
||||
emit(GetDevicesSuccess(allDevices));
|
||||
emit(state.copyWith(devices: allDevices, loading: false));
|
||||
} catch (e) {
|
||||
emit(GetDevicesError(e.toString()));
|
||||
emit(state.copyWith(error: e.toString(), loading: false));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onFetchDevicesByRoomId(
|
||||
FetchDevicesByRoomId event, Emitter<DeviceManagerState> emit) async {
|
||||
emit(GetDevicesLoading());
|
||||
emit(state.copyWith(loading: true));
|
||||
try {
|
||||
final devices = await DevicesAPI.getDevicesByRoomId(event.roomId);
|
||||
emit(GetDevicesSuccess(devices));
|
||||
emit(state.copyWith(devices: devices, loading: false));
|
||||
} catch (e) {
|
||||
emit(GetDevicesError(e.toString()));
|
||||
emit(state.copyWith(error: e.toString(), loading: false));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,7 +52,7 @@ class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
|
||||
for (var i = 0; i < allCategories!.length; i++) {
|
||||
allCategories![i].isSelected = i == event.index;
|
||||
}
|
||||
emit(DevicesCategoryChanged());
|
||||
emit(state.copyWith(categoryChanged: true));
|
||||
}
|
||||
|
||||
void _onUnselectAllCategories(
|
||||
@@ -58,7 +60,7 @@ class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
|
||||
for (var category in allCategories!) {
|
||||
category.isSelected = false;
|
||||
}
|
||||
emit(DevicesCategoryChanged());
|
||||
emit(state.copyWith(categoryChanged: true));
|
||||
}
|
||||
|
||||
void _onSelectDevice(SelectDevice event, Emitter<DeviceManagerState> emit) {
|
||||
@@ -67,14 +69,14 @@ class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
|
||||
for (var device in category.devices!) {
|
||||
if (device.isSelected) {
|
||||
category.isSelected = false;
|
||||
emit(DeviceSelected());
|
||||
emit(state.copyWith(deviceSelected: true));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
event.device.isSelected = !event.device.isSelected;
|
||||
emit(DeviceSelected());
|
||||
emit(state.copyWith(deviceSelected: true));
|
||||
}
|
||||
|
||||
void _onChangeCategorySwitchValue(
|
||||
@@ -119,28 +121,9 @@ class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
|
||||
}
|
||||
}
|
||||
Navigator.popUntil(event.context, (route) => route.isFirst);
|
||||
emit(DevicesCategoryChanged());
|
||||
emit(state.copyWith(categoryChanged: true)); // Set category changed state
|
||||
}
|
||||
|
||||
// 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) {
|
||||
@@ -152,7 +135,20 @@ class DeviceManagerBloc extends Bloc<DeviceManagerEvent, DeviceManagerState> {
|
||||
}
|
||||
category.devicesStatus = tempStatus;
|
||||
}
|
||||
emit(CategorySwitchChanged());
|
||||
emit(state.copyWith(categoryChanged: true));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _getDeviceFunctions(
|
||||
DeviceFunctionsEvent event, Emitter<DeviceManagerState> emit) async {
|
||||
emit(state.copyWith(functionsLoading: true));
|
||||
try {
|
||||
final deviceFunctions = await DevicesAPI.deviceFunctions(event.deviceId);
|
||||
|
||||
emit(state.copyWith(
|
||||
functionsLoading: false, deviceFunctions: deviceFunctions));
|
||||
} catch (e) {
|
||||
emit(state.copyWith(functionsLoading: false, error: e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,3 +78,11 @@ class DeviceControl extends DeviceManagerEvent {
|
||||
@override
|
||||
List<Object> get props => [control, deviceId];
|
||||
}
|
||||
|
||||
class DeviceFunctionsEvent extends DeviceManagerEvent {
|
||||
final String deviceId;
|
||||
const DeviceFunctionsEvent(this.deviceId);
|
||||
|
||||
@override
|
||||
List<Object> get props => [deviceId];
|
||||
}
|
||||
|
||||
@@ -1,112 +1,73 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:syncrow_app/features/devices/model/device_category_model.dart';
|
||||
import 'package:syncrow_app/features/devices/model/device_model.dart';
|
||||
import 'package:syncrow_app/features/devices/model/functions.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 {
|
||||
class DeviceManagerState extends Equatable {
|
||||
final bool loading;
|
||||
final String? error;
|
||||
final List<DevicesCategoryModel>? allCategories;
|
||||
final List<DeviceModel>? devices;
|
||||
final bool categoryChanged;
|
||||
final bool deviceSelected;
|
||||
final bool functionsLoading;
|
||||
final FunctionsEntity? deviceFunctions;
|
||||
|
||||
const GetDevicesSuccess(this.devices);
|
||||
const DeviceManagerState({
|
||||
required this.loading,
|
||||
this.error,
|
||||
this.allCategories,
|
||||
this.devices,
|
||||
this.categoryChanged = false,
|
||||
this.deviceSelected = false,
|
||||
this.functionsLoading = false,
|
||||
this.deviceFunctions,
|
||||
});
|
||||
|
||||
factory DeviceManagerState.initial() {
|
||||
return const DeviceManagerState(
|
||||
loading: false,
|
||||
error: null,
|
||||
allCategories: null,
|
||||
devices: null,
|
||||
categoryChanged: false,
|
||||
deviceSelected: false,
|
||||
functionsLoading: false,
|
||||
deviceFunctions: null,
|
||||
);
|
||||
}
|
||||
|
||||
DeviceManagerState copyWith({
|
||||
bool? loading,
|
||||
String? error,
|
||||
List<DevicesCategoryModel>? allCategories,
|
||||
List<DeviceModel>? devices,
|
||||
bool? categoryChanged,
|
||||
bool? deviceSelected,
|
||||
bool? functionsLoading,
|
||||
FunctionsEntity? deviceFunctions,
|
||||
}) {
|
||||
return DeviceManagerState(
|
||||
loading: loading ?? this.loading,
|
||||
error: error ?? this.error,
|
||||
allCategories: allCategories ?? this.allCategories,
|
||||
devices: devices ?? this.devices,
|
||||
categoryChanged: categoryChanged ?? this.categoryChanged,
|
||||
deviceSelected: deviceSelected ?? this.deviceSelected,
|
||||
functionsLoading: functionsLoading ?? this.functionsLoading,
|
||||
deviceFunctions: deviceFunctions ?? this.deviceFunctions,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object> get props => [devices ?? []];
|
||||
List<Object?> get props => [
|
||||
loading,
|
||||
error,
|
||||
allCategories,
|
||||
devices,
|
||||
categoryChanged,
|
||||
deviceSelected,
|
||||
functionsLoading,
|
||||
deviceFunctions,
|
||||
];
|
||||
}
|
||||
|
||||
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 {}
|
||||
|
||||
@@ -331,18 +331,6 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
// }
|
||||
}
|
||||
|
||||
void fetchAllDevices() async {
|
||||
emitSafe(GetDevicesLoading());
|
||||
try {
|
||||
final allDevices = await HomeManagementAPI.fetchDevicesByUserId();
|
||||
|
||||
emitSafe(GetDevicesSuccess(allDevices));
|
||||
} catch (e) {
|
||||
emitSafe(GetDevicesError(e.toString()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
fetchDevicesStatues(String deviceUuid, int roomIndex, {String? code}) async {
|
||||
emitSafe(GetDeviceStatusLoading(code: code));
|
||||
int deviceIndex = HomeCubit.getInstance()
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
class FunctionsEntity {
|
||||
final String productUuid;
|
||||
final String productType;
|
||||
final List<DeviceFunction> functions;
|
||||
|
||||
FunctionsEntity({
|
||||
required this.productUuid,
|
||||
required this.productType,
|
||||
required this.functions,
|
||||
});
|
||||
|
||||
factory FunctionsEntity.fromJson(Map<String, dynamic> json) =>
|
||||
FunctionsEntity(
|
||||
productUuid: json["productUuid"],
|
||||
productType: json["productType"],
|
||||
functions: List<DeviceFunction>.from(
|
||||
json["functions"].map((x) => DeviceFunction.fromJson(x)),
|
||||
),
|
||||
);
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
"productUuid": productUuid,
|
||||
"productType": productType,
|
||||
"functions": List<dynamic>.from(functions.map((x) => x.toJson())),
|
||||
};
|
||||
}
|
||||
|
||||
class DeviceFunction {
|
||||
final String code;
|
||||
final String values;
|
||||
final String dataType;
|
||||
|
||||
DeviceFunction({
|
||||
required this.code,
|
||||
required this.values,
|
||||
required this.dataType,
|
||||
});
|
||||
|
||||
factory DeviceFunction.fromJson(Map<String, dynamic> json) => DeviceFunction(
|
||||
code: _formatCode(json["code"]),
|
||||
values: json["values"],
|
||||
dataType: json["dataType"],
|
||||
);
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
"code": code,
|
||||
"values": values,
|
||||
"dataType": dataType,
|
||||
};
|
||||
|
||||
static String _formatCode(String originalCode) {
|
||||
return originalCode
|
||||
.replaceAll('_', ' ')
|
||||
.split(' ')
|
||||
.map((word) => word[0].toUpperCase() + word.substring(1))
|
||||
.join(' ');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user