Files
syncrow-web/lib/pages/routines/bloc/functions_bloc/functions_bloc_bloc.dart
mohammad 0d45a155e3 add step parameter in onTapFunction.
Add dialogType parameter in WaterHeaterPresenceSensor and CeilingSensorDialog.
Update step parameter in FlushValueSelectorWidget.
Update step parameter in FunctionBloc and WaterHeaterFunctions.
Update step, unit, min, and max parameters in ACFunction subclasses.
2025-05-19 11:22:15 +03:00

71 lines
2.3 KiB
Dart

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,
step: event.functionData.step ?? existingData.step,
);
} else {
functions.clear();
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));
}
}