mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 07:07:19 +00:00
Implemented side tree to devices and rountines screen
This commit is contained in:
@ -0,0 +1,65 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:syncrow_web/pages/routines/models/device_functions.dart';
|
||||
|
||||
part 'functions_bloc_event.dart';
|
||||
part 'functions_bloc_state.dart';
|
||||
|
||||
class FunctionBloc extends Bloc<FunctionBlocEvent, FunctionBlocState> {
|
||||
FunctionBloc() : super(const FunctionBlocState()) {
|
||||
on<InitializeFunctions>(_onInitializeFunctions);
|
||||
on<AddFunction>(_onAddFunction);
|
||||
on<SelectFunction>(_onSelectFunction);
|
||||
}
|
||||
void _onAddFunction(AddFunction event, Emitter<FunctionBlocState> emit) {
|
||||
final functions = List<DeviceFunctionData>.from(state.addedFunctions);
|
||||
final existingIndex = functions.indexWhere(
|
||||
(f) => f.functionCode == event.functionData.functionCode,
|
||||
);
|
||||
|
||||
if (existingIndex != -1) {
|
||||
final existingData = functions[existingIndex];
|
||||
functions[existingIndex] = DeviceFunctionData(
|
||||
entityId: event.functionData.entityId,
|
||||
functionCode: event.functionData.functionCode,
|
||||
operationName: event.functionData.operationName,
|
||||
value: event.functionData.value ?? existingData.value,
|
||||
valueDescription: event.functionData.valueDescription ?? existingData.valueDescription,
|
||||
condition: event.functionData.condition ?? existingData.condition,
|
||||
);
|
||||
} else {
|
||||
functions.add(event.functionData);
|
||||
}
|
||||
|
||||
emit(state.copyWith(
|
||||
addedFunctions: functions,
|
||||
selectedFunction: event.functionData.functionCode,
|
||||
));
|
||||
}
|
||||
|
||||
void _onInitializeFunctions(
|
||||
InitializeFunctions event,
|
||||
Emitter<FunctionBlocState> emit,
|
||||
) {
|
||||
emit(state.copyWith(addedFunctions: event.functions));
|
||||
}
|
||||
|
||||
DeviceFunctionData? getFunction(String functionCode) {
|
||||
return state.addedFunctions.firstWhere(
|
||||
(data) => data.functionCode == functionCode,
|
||||
orElse: () => DeviceFunctionData(
|
||||
entityId: '',
|
||||
functionCode: functionCode,
|
||||
operationName: '',
|
||||
value: null,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
FutureOr<void> _onSelectFunction(SelectFunction event, Emitter<FunctionBlocState> emit) {
|
||||
emit(state.copyWith(
|
||||
selectedFunction: event.functionCode, selectedOperationName: event.operationName));
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
part of 'functions_bloc_bloc.dart';
|
||||
|
||||
abstract class FunctionBlocEvent extends Equatable {
|
||||
const FunctionBlocEvent();
|
||||
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
class AddFunction extends FunctionBlocEvent {
|
||||
final DeviceFunctionData functionData;
|
||||
|
||||
const AddFunction({
|
||||
required this.functionData,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [functionData];
|
||||
}
|
||||
|
||||
class SelectFunction extends FunctionBlocEvent {
|
||||
final String functionCode;
|
||||
final String operationName;
|
||||
|
||||
const SelectFunction({
|
||||
required this.functionCode,
|
||||
required this.operationName,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [functionCode, operationName];
|
||||
}
|
||||
|
||||
class InitializeFunctions extends FunctionBlocEvent {
|
||||
final List<DeviceFunctionData> functions;
|
||||
|
||||
const InitializeFunctions(this.functions);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [functions];
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
part of 'functions_bloc_bloc.dart';
|
||||
|
||||
class FunctionBlocState extends Equatable {
|
||||
final List<DeviceFunctionData> addedFunctions;
|
||||
final String? selectedFunction;
|
||||
final String? selectedOperationName;
|
||||
const FunctionBlocState({
|
||||
this.addedFunctions = const [],
|
||||
this.selectedFunction,
|
||||
this.selectedOperationName,
|
||||
});
|
||||
|
||||
FunctionBlocState copyWith({
|
||||
List<DeviceFunctionData>? addedFunctions,
|
||||
String? selectedFunction,
|
||||
String? selectedOperationName,
|
||||
}) {
|
||||
return FunctionBlocState(
|
||||
addedFunctions: addedFunctions ?? this.addedFunctions,
|
||||
selectedFunction: selectedFunction ?? this.selectedFunction,
|
||||
selectedOperationName:
|
||||
selectedOperationName ?? this.selectedOperationName,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props =>
|
||||
[addedFunctions, selectedFunction, selectedOperationName];
|
||||
}
|
Reference in New Issue
Block a user