96 lines
3.0 KiB
Dart
96 lines
3.0 KiB
Dart
import 'package:equatable/equatable.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:uto_manager/domain/entities/personnel_record.dart';
|
|
import 'package:uto_manager/domain/entities/position_slot.dart';
|
|
import 'package:uto_manager/domain/entities/unit_node.dart';
|
|
import 'package:uto_manager/domain/services/mtoe_validation_engine.dart';
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// State
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// State for the validation cubit.
|
|
sealed class ValidationState extends Equatable {
|
|
const ValidationState();
|
|
}
|
|
|
|
/// No validation has been run yet.
|
|
class ValidationInitial extends ValidationState {
|
|
const ValidationInitial();
|
|
|
|
@override
|
|
List<Object?> get props => [];
|
|
}
|
|
|
|
/// Validation is in progress.
|
|
class ValidationRunning extends ValidationState {
|
|
const ValidationRunning();
|
|
|
|
@override
|
|
List<Object?> get props => [];
|
|
}
|
|
|
|
/// Validation completed with a (possibly empty) list of findings.
|
|
class ValidationComplete extends ValidationState {
|
|
const ValidationComplete(this.findings);
|
|
|
|
final List<ValidationFinding> findings;
|
|
|
|
/// All critical findings.
|
|
List<ValidationFinding> get critical =>
|
|
findings.where((f) => f.severity == FindingSeverity.critical).toList();
|
|
|
|
/// All warning findings.
|
|
List<ValidationFinding> get warnings =>
|
|
findings.where((f) => f.severity == FindingSeverity.warning).toList();
|
|
|
|
/// All advisory findings.
|
|
List<ValidationFinding> get advisories =>
|
|
findings.where((f) => f.severity == FindingSeverity.advisory).toList();
|
|
|
|
/// Returns findings for a specific [unitId].
|
|
List<ValidationFinding> findingsForUnit(String unitId) =>
|
|
findings.where((f) => f.unitId == unitId).toList();
|
|
|
|
/// True if there are no findings.
|
|
bool get isClean => findings.isEmpty;
|
|
|
|
@override
|
|
List<Object?> get props => [findings];
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Cubit
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// Runs the [MtoeValidationEngine] against the current unit tree snapshot
|
|
/// and exposes the resulting [ValidationFinding]s to the UI.
|
|
///
|
|
/// Call [validate] whenever the unit tree, slots, or personnel change.
|
|
class ValidationCubit extends Cubit<ValidationState> {
|
|
ValidationCubit() : super(const ValidationInitial());
|
|
|
|
final _engine = const MtoeValidationEngine();
|
|
|
|
/// Runs all MTOE validation rules against the provided snapshot.
|
|
///
|
|
/// Emits [ValidationRunning] while computing, then [ValidationComplete]
|
|
/// with the results.
|
|
void validate({
|
|
required List<UnitNode> units,
|
|
required List<PositionSlot> slots,
|
|
required List<PersonnelRecord> personnel,
|
|
}) {
|
|
emit(const ValidationRunning());
|
|
final findings = _engine.validate(
|
|
units: units,
|
|
slots: slots,
|
|
personnel: personnel,
|
|
);
|
|
emit(ValidationComplete(findings));
|
|
}
|
|
|
|
/// Clears all findings and resets to the initial state.
|
|
void clear() => emit(const ValidationInitial());
|
|
}
|