Files
HC_APTBS/ViewModels/Dialogs/UnlockProgressViewModel.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

153 lines
6.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HC_APTBS.Services;
namespace HC_APTBS.ViewModels.Dialogs
{
/// <summary>
/// ViewModel for the Ford VP44 immobilizer unlock progress dialog.
/// Tracks Phase 1 (CAN flood ~600 s), Phase 2 (handshake ~4 s), and verification.
/// Equivalent to the old <c>WUnlocker</c> window.
/// </summary>
public sealed partial class UnlockProgressViewModel : ObservableObject, IDisposable
{
private readonly IUnlockService _unlockService;
private readonly ILocalizationService _loc;
private readonly CancellationTokenSource _cts;
/// <summary>Regex to extract percentage and elapsed time from Phase 1 status messages.</summary>
private static readonly Regex ProgressRegex =
new(@"Unlocking\.\.\. (\d+)% \((\d{2}:\d{2})\)", RegexOptions.Compiled);
/// <summary>Creates the ViewModel and subscribes to unlock service events.</summary>
/// <param name="unlockService">The unlock service to monitor.</param>
/// <param name="unlockType">Pump unlock type (1 or 2).</param>
/// <param name="cts">Cancellation token source to cancel the unlock.</param>
public UnlockProgressViewModel(IUnlockService unlockService, int unlockType, CancellationTokenSource cts, ILocalizationService loc)
{
_unlockService = unlockService;
_loc = loc;
_cts = cts;
_unlockTypeLabel = string.Format(_loc.GetString("Dialog.Unlock.TypeLabel"), unlockType);
_phaseText = _loc.GetString("Dialog.Unlock.Phase1");
_elapsedTime = "00:00";
_isCancellable = true;
_unlockService.StatusChanged += OnStatusChanged;
_unlockService.UnlockCompleted += OnUnlockCompleted;
}
// ── Observable properties ────────────────────────────────────────────────
/// <summary>Progress percentage (0100).</summary>
[ObservableProperty] private int _progress;
/// <summary>Elapsed time formatted as MM:SS.</summary>
[ObservableProperty] private string _elapsedTime;
/// <summary>Current phase description.</summary>
[ObservableProperty] private string _phaseText;
/// <summary>Result text shown after completion.</summary>
[ObservableProperty] private string _resultText = string.Empty;
/// <summary>Label for unlock type (e.g. "Type 1").</summary>
[ObservableProperty] private string _unlockTypeLabel;
/// <summary>True when the unlock sequence has finished (success, failure, or cancelled).</summary>
[NotifyCanExecuteChangedFor(nameof(CloseCommand))]
[ObservableProperty] private bool _isComplete;
/// <summary>True while cancellation is allowed (Phase 1 only).</summary>
[NotifyCanExecuteChangedFor(nameof(CancelCommand))]
[ObservableProperty] private bool _isCancellable;
/// <summary>Tri-state result: null = in progress, true = success, false = failure.</summary>
[ObservableProperty] private bool? _isSuccess;
// ── Commands ─────────────────────────────────────────────────────────────
/// <summary>Cancels the unlock sequence (only available during Phase 1).</summary>
[RelayCommand(CanExecute = nameof(IsCancellable))]
private void Cancel()
{
_cts.Cancel();
IsCancellable = false;
IsComplete = true;
IsSuccess = false;
ResultText = _loc.GetString("Dialog.Unlock.Cancelled");
}
/// <summary>Closes the dialog (only available after completion).</summary>
[RelayCommand(CanExecute = nameof(IsComplete))]
private void Close()
{
RequestClose?.Invoke();
}
// ── Events ───────────────────────────────────────────────────────────────
/// <summary>Raised when the dialog should close itself.</summary>
public event Action? RequestClose;
// ── Service event handlers ───────────────────────────────────────────────
private void OnStatusChanged(string msg)
{
Application.Current?.Dispatcher?.Invoke(() =>
{
var match = ProgressRegex.Match(msg);
if (match.Success)
{
Progress = int.Parse(match.Groups[1].Value);
ElapsedTime = match.Groups[2].Value;
return;
}
if (msg == "Fast unlock attempt...")
{
PhaseText = _loc.GetString("Dialog.Unlock.FastAttempt");
}
else if (msg == "Unlocking...")
{
PhaseText = _loc.GetString("Dialog.Unlock.Phase1");
}
else if (msg == "Testing unlock...")
{
PhaseText = _loc.GetString("Dialog.Unlock.Phase2Testing");
IsCancellable = false;
Progress = 100;
}
else if (msg == "Sending...")
{
PhaseText = _loc.GetString("Dialog.Unlock.Phase2Sending");
}
});
}
private void OnUnlockCompleted(bool success)
{
Application.Current?.Dispatcher?.Invoke(() =>
{
IsComplete = true;
IsCancellable = false;
IsSuccess = success;
ResultText = success ? _loc.GetString("Dialog.Unlock.Unlocked") : _loc.GetString("Dialog.Unlock.Failed");
});
}
// ── IDisposable ──────────────────────────────────────────────────────────
/// <summary>Unsubscribes from service events to prevent leaks.</summary>
public void Dispose()
{
_unlockService.StatusChanged -= OnStatusChanged;
_unlockService.UnlockCompleted -= OnUnlockCompleted;
}
}
}