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 { FunctionBloc() : super(const FunctionBlocState()) { on(_onInitializeFunctions); on(_onAddFunction); on(_onSelectFunction); } // void _onAddFunction(AddFunction event, Emitter emit) { // final functions = List.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 _onAddFunction(AddFunction event, Emitter emit) { final functions = List.from(state.addedFunctions); final existingIndex = functions.indexWhere( (f) => f.functionCode == event.functionData.functionCode, ); if (existingIndex != -1) { // Update the function value functions[existingIndex] = event.functionData; } else { // Add new function value functions.add(event.functionData); } emit(state.copyWith( addedFunctions: functions, selectedFunction: event.functionData.functionCode, )); } void _onInitializeFunctions( InitializeFunctions event, Emitter 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 _onSelectFunction( SelectFunction event, Emitter emit) { emit(state.copyWith( selectedFunction: event.functionCode, selectedOperationName: event.operationName)); } }