Files
HC_APTBS/ViewModels/TestSectionViewModel.cs
LucianoDev 37d099cdbd feat: add Ford VP44 unlock progress dialog, K-Line fast unlock, localization, safety dialogs, and settings
Unlock progress UI:
- UnlockProgressDialog with dark-themed progress ring, phase indicator, elapsed
  time, and cancel/close buttons (non-modal, draggable borderless window)
- UnlockProgressViewModel with event-driven progress tracking via IUnlockService
- Triggers on pump selection (manual or K-Line auto-detect), not test start

UnlockService rewrite:
- Persistent CAN senders that outlive the unlock sequence (StopSenders on pump change)
- Concurrent K-Line fast unlock: awaits session Connected, sends RAM timer shortcut
  ({02 88 02 03 A8 01 00}), verifies via CAN TestUnlock before skipping wait
- Fix Type 1 verification (Value == 0 means unlocked, was inverted)

K-Line fast unlock support:
- IKwpService.TryFastUnlockAsync / KwpService implementation

Additional features:
- ILocalizationService with ES/EN resource dictionaries and runtime switching
- Safety dialogs: VoltageWarning, OilPumpConfirm, RpmSafetyWarning
- SettingsDialog for app configuration
- BenchService enhancements, ConfigurationService improvements, PDF report updates
- All UI strings localized via DynamicResource

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-16 13:22:48 +02:00

182 lines
8.1 KiB
C#

using System.Collections.ObjectModel;
using System.Linq;
using CommunityToolkit.Mvvm.ComponentModel;
using HC_APTBS.Models;
using HC_APTBS.Services;
namespace HC_APTBS.ViewModels
{
/// <summary>
/// Represents one test type section (e.g. "F", "DFI", "SVME") containing
/// a header with metadata and a horizontal list of <see cref="PhaseCardViewModel"/> cards.
/// </summary>
public sealed partial class TestSectionViewModel : ObservableObject
{
private readonly ILocalizationService _loc;
/// <summary>Initialises a new test section with a localization service.</summary>
public TestSectionViewModel(ILocalizationService loc) => _loc = loc;
// ── Suppress cascade guard ────────────────────────────────────────────────
private bool _suppressCascade;
// ── Identity / metadata ───────────────────────────────────────────────────
/// <summary>Test type identifier (e.g. "F", "DFI", "SVME").</summary>
[ObservableProperty] private string _testName = string.Empty;
/// <summary>Human-readable description of the test type.</summary>
[ObservableProperty] private string _description = string.Empty;
/// <summary>Conditioning time in seconds.</summary>
[ObservableProperty] private int _conditioningTimeSec;
/// <summary>Measurement time in seconds.</summary>
[ObservableProperty] private int _measurementTimeSec;
/// <summary>Measurements per second during the measurement window.</summary>
[ObservableProperty] private double _measurementsPerSecond;
// ── UI state ──────────────────────────────────────────────────────────────
/// <summary>Whether this section's expander is open.</summary>
[ObservableProperty] private bool _isExpanded = true;
/// <summary>True when any phase in this test section is currently executing.</summary>
[ObservableProperty] private bool _isActiveTest;
/// <summary>
/// Bidirectional check state for all phases. Setting this cascades down to
/// all child <see cref="PhaseCardViewModel.IsEnabled"/> properties; child changes
/// cascade back up to recalculate this value.
/// </summary>
[ObservableProperty] private bool _allPhasesChecked = true;
// ── Phases ────────────────────────────────────────────────────────────────
/// <summary>Phase cards shown in the horizontal scroll area.</summary>
public ObservableCollection<PhaseCardViewModel> Phases { get; } = new();
// ── Back-reference ────────────────────────────────────────────────────────
/// <summary>Back-reference to the source model.</summary>
internal TestDefinition? Source { get; set; }
// ── Cascade: parent → children ────────────────────────────────────────────
partial void OnAllPhasesCheckedChanged(bool value)
{
if (_suppressCascade) return;
_suppressCascade = true;
try
{
foreach (var phase in Phases)
phase.IsEnabled = value;
}
finally
{
_suppressCascade = false;
}
}
// ── Cascade: child → parent ──────────────────────────────────────────────
/// <summary>
/// Called by a child <see cref="PhaseCardViewModel"/> when its
/// <see cref="PhaseCardViewModel.IsEnabled"/> changes.
/// Recalculates <see cref="AllPhasesChecked"/> without re-cascading.
/// </summary>
internal void OnChildEnabledChanged(PhaseCardViewModel _)
{
if (_suppressCascade) return;
_suppressCascade = true;
try
{
AllPhasesChecked = Phases.All(p => p.IsEnabled);
}
finally
{
_suppressCascade = false;
}
}
// ── Factory ───────────────────────────────────────────────────────────────
/// <summary>
/// Creates a <see cref="TestSectionViewModel"/> from a <see cref="TestDefinition"/> model,
/// populating all child <see cref="PhaseCardViewModel"/> instances.
/// </summary>
/// <param name="test">Source test definition.</param>
/// <param name="showValues">Initial show-operation-values state.</param>
/// <param name="loc">Localization service for user-facing strings.</param>
public static TestSectionViewModel FromDefinition(TestDefinition test, bool showValues, ILocalizationService loc)
{
var section = new TestSectionViewModel(loc)
{
TestName = test.Name,
Description = loc.GetString(MapDescriptionKey(test.Name)),
ConditioningTimeSec = test.ConditioningTimeSec,
MeasurementTimeSec = test.MeasurementTimeSec,
MeasurementsPerSecond = test.MeasurementsPerSecond,
Source = test
};
foreach (var phaseDef in test.Phases)
{
var card = new PhaseCardViewModel(loc)
{
Name = phaseDef.Name,
IsCritical = phaseDef.IsCritical,
IsEnabled = phaseDef.Enabled,
ResultText = phaseDef.Enabled ? "\u2013" : loc.GetString("Common.Disabled"),
ShowOperationValues = showValues,
Source = phaseDef,
EnabledChanged = section.OnChildEnabledChanged
};
// Populate operation values from Sends.
foreach (var tp in phaseDef.Sends)
card.OperationValues.Add(new OperationValueViewModel { Name = tp.Name, Value = tp.Value });
// Populate readiness conditions from Readies.
foreach (var tp in phaseDef.Readies)
card.ReadyValues.Add(new OperationValueViewModel { Name = tp.Name, Value = tp.Value });
// Populate graphic result indicators from Receives.
foreach (var tp in phaseDef.Receives)
{
card.ResultIndicators.Add(new GraphicIndicatorViewModel
{
ParameterName = tp.Name,
ExpectedValue = tp.Value,
Tolerance = tp.Tolerance
});
}
section.Phases.Add(card);
}
section.AllPhasesChecked = section.Phases.All(p => p.IsEnabled);
return section;
}
/// <summary>
/// Maps a test type identifier to a localization resource key.
/// Returns the test name itself for unknown types (fail-visible).
/// </summary>
private static string MapDescriptionKey(string testName) => testName switch
{
TestType.Wl => "TestType.Warmup",
TestType.Dfi => "TestType.Adjustment",
TestType.F => "TestType.Flow",
TestType.Svme => "TestType.ServoValve",
TestType.Up => "TestType.Upstroke",
TestType.Pfp => "TestType.PreInjection",
_ => testName
};
}
}