mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-09 22:57:21 +00:00
149 lines
4.6 KiB
Dart
149 lines
4.6 KiB
Dart
import 'package:bloc/bloc.dart';
|
|
import 'package:equatable/equatable.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:syncrow_web/pages/routiens/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<UpdateFunction>(_onUpdateFunctions);
|
|
on<UpdateFunctionValue>(_onUpdateFunctionValue);
|
|
on<UpdateFunctionCondition>(_onUpdateFunctionCondition);
|
|
on<RemoveFunction>(_onRemoveFunction);
|
|
}
|
|
|
|
void _onAddFunction(AddFunction event, Emitter<FunctionBlocState> emit) {
|
|
debugPrint('Adding function: ${event.functionData.function}');
|
|
final functions = List<DeviceFunctionData>.from(state.functions);
|
|
|
|
// Find existing function data
|
|
final existingIndex = functions.indexWhere(
|
|
(f) => f.function == event.functionData.function,
|
|
);
|
|
|
|
// If function exists, preserve its value and condition
|
|
if (existingIndex != -1) {
|
|
final existingData = functions[existingIndex];
|
|
functions[existingIndex] = DeviceFunctionData(
|
|
entityId: event.functionData.entityId,
|
|
function: event.functionData.function,
|
|
operationName: event.functionData.operationName,
|
|
value: existingData.value, // Preserve the existing value
|
|
condition: existingData.condition, // Preserve the existing condition
|
|
);
|
|
} else {
|
|
functions.add(event.functionData);
|
|
}
|
|
|
|
debugPrint('Functions after add: $functions');
|
|
emit(state.copyWith(
|
|
functions: functions,
|
|
selectedFunction: event.functionData.function,
|
|
));
|
|
}
|
|
|
|
void _onUpdateFunctions(
|
|
UpdateFunction event, Emitter<FunctionBlocState> emit) {
|
|
final functions = state.functions.map((data) {
|
|
return data.function == event.functionData.function
|
|
? event.functionData
|
|
: data;
|
|
}).toList();
|
|
|
|
emit(state.copyWith(functions: functions));
|
|
}
|
|
|
|
void _onUpdateFunctionValue(
|
|
UpdateFunctionValue event,
|
|
Emitter<FunctionBlocState> emit,
|
|
) {
|
|
debugPrint('Updating function value: ${event.function} -> ${event.value}');
|
|
|
|
// Create a new list to ensure state immutability
|
|
final functions = List<DeviceFunctionData>.from(state.functions);
|
|
|
|
// Find the index of the function to update
|
|
final functionIndex = functions.indexWhere(
|
|
(data) => data.function == event.function,
|
|
);
|
|
|
|
if (functionIndex != -1) {
|
|
// Update the existing function data while preserving other fields
|
|
final existingData = functions[functionIndex];
|
|
functions[functionIndex] = DeviceFunctionData(
|
|
entityId: existingData.entityId,
|
|
function: existingData.function,
|
|
operationName: existingData.operationName,
|
|
value: event.value,
|
|
condition: existingData.condition,
|
|
);
|
|
} else {
|
|
// If function doesn't exist, add it
|
|
functions.add(DeviceFunctionData(
|
|
entityId: '',
|
|
function: event.function,
|
|
operationName: '',
|
|
value: event.value,
|
|
));
|
|
}
|
|
|
|
debugPrint('Functions after update: $functions');
|
|
emit(state.copyWith(functions: functions));
|
|
}
|
|
|
|
void _onUpdateFunctionCondition(
|
|
UpdateFunctionCondition event,
|
|
Emitter<FunctionBlocState> emit,
|
|
) {
|
|
final functions = state.functions.map((data) {
|
|
if (data.function == event.function) {
|
|
return DeviceFunctionData(
|
|
entityId: data.entityId,
|
|
function: data.function,
|
|
operationName: data.operationName,
|
|
value: data.value,
|
|
condition: event.condition,
|
|
);
|
|
}
|
|
return data;
|
|
}).toList();
|
|
|
|
emit(state.copyWith(functions: functions));
|
|
}
|
|
|
|
void _onRemoveFunction(
|
|
RemoveFunction event, Emitter<FunctionBlocState> emit) {
|
|
final functions = state.functions
|
|
.where((data) => data.function != event.functionCode)
|
|
.toList();
|
|
|
|
emit(state.copyWith(
|
|
functions: functions,
|
|
selectedFunction: functions.isEmpty ? null : state.selectedFunction,
|
|
));
|
|
}
|
|
|
|
void _onInitializeFunctions(
|
|
InitializeFunctions event,
|
|
Emitter<FunctionBlocState> emit,
|
|
) {
|
|
emit(state.copyWith(functions: event.functions));
|
|
}
|
|
|
|
DeviceFunctionData? getFunction(String functionCode) {
|
|
return state.functions.firstWhere(
|
|
(data) => data.function == functionCode,
|
|
orElse: () => DeviceFunctionData(
|
|
entityId: '',
|
|
function: functionCode,
|
|
operationName: '',
|
|
value: null,
|
|
),
|
|
);
|
|
}
|
|
}
|