|
| 1 | +using System; |
| 2 | +using System.Collections.ObjectModel; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using CommunityToolkit.Mvvm.ComponentModel; |
| 6 | +using CommunityToolkit.Mvvm.Input; |
| 7 | +using MsBox.Avalonia; |
| 8 | +using MsBox.Avalonia.Enums; |
| 9 | +using PostCodeSerialMonitor.Models; |
| 10 | +using PostCodeSerialMonitor.Services; |
| 11 | + |
| 12 | +namespace PostCodeSerialMonitor.ViewModels; |
| 13 | + |
| 14 | +public partial class DebugDialogViewModel : ViewModelBase |
| 15 | +{ |
| 16 | + private readonly ObservableCollection<LogEntry> _logEntries; |
| 17 | + private readonly SerialLineDecoder _serialLineDecoder; |
| 18 | + |
| 19 | + public ObservableCollection<ConsoleType> ConsoleTypes { get; } |
| 20 | + public ObservableCollection<CodeFlavor> CodeFlavors { get; } = new(new[] |
| 21 | + { |
| 22 | + CodeFlavor.SMC, CodeFlavor.SP, CodeFlavor.CPU, CodeFlavor.OS |
| 23 | + }); |
| 24 | + |
| 25 | + [ObservableProperty] |
| 26 | + private ConsoleType selectedConsoleType; |
| 27 | + |
| 28 | + [ObservableProperty] |
| 29 | + private decimal entryCount = 20; |
| 30 | + |
| 31 | + [ObservableProperty] |
| 32 | + private string codeInput = string.Empty; |
| 33 | + |
| 34 | + [ObservableProperty] |
| 35 | + private string decodedResultText = string.Empty; |
| 36 | + |
| 37 | + public DebugDialogViewModel( |
| 38 | + ObservableCollection<LogEntry> logEntries, |
| 39 | + SerialLineDecoder serialLineDecoder, |
| 40 | + ObservableCollection<ConsoleType> consoleTypes) |
| 41 | + { |
| 42 | + _logEntries = logEntries; |
| 43 | + _serialLineDecoder = serialLineDecoder; |
| 44 | + ConsoleTypes = consoleTypes; |
| 45 | + |
| 46 | + SelectedConsoleType = ConsoleTypes.FirstOrDefault(); |
| 47 | + } |
| 48 | + |
| 49 | + [RelayCommand] |
| 50 | + private void FillDummyData() |
| 51 | + { |
| 52 | + for (int i = 0; i < (int)EntryCount; i++) |
| 53 | + { |
| 54 | + _logEntries.Add(new LogEntry |
| 55 | + { |
| 56 | + DecodedCode = new DecodedCode |
| 57 | + { |
| 58 | + Flavor = CodeFlavors[i % CodeFlavors.Count], |
| 59 | + Index = i, |
| 60 | + Code = i * 0x11, |
| 61 | + SeverityLevel = (CodeSeverity)(i % 3), |
| 62 | + Name = $"DEBUG_CODE_{i}", |
| 63 | + Description = i % 4 == 0 |
| 64 | + ? $"This is a long debug description for entry {i}, used to verify that the log window wraps text correctly and fills the full width of the resized main window instead of being clipped at a fixed maximum width." |
| 65 | + : $"Debug entry {i}" |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + [RelayCommand] |
| 72 | + private async Task DecodeStandaloneAsync() |
| 73 | + { |
| 74 | + int code; |
| 75 | + try |
| 76 | + { |
| 77 | + var hex = CodeInput.Trim(); |
| 78 | + if (hex.StartsWith("0x", StringComparison.OrdinalIgnoreCase)) |
| 79 | + hex = hex[2..]; |
| 80 | + code = Convert.ToInt32(hex, 16) & 0xFFFF; |
| 81 | + } |
| 82 | + catch (Exception) |
| 83 | + { |
| 84 | + await MessageBoxManager |
| 85 | + .GetMessageBoxStandard(Assets.Resources.Error, string.Format(Assets.Resources.InvalidCodeFormatMessageBoxError, CodeInput), ButtonEnum.Ok) |
| 86 | + .ShowAsPopupAsync(GetParentWindow()); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + DecodedResultText = String.Empty; |
| 91 | + foreach (var codeFlavor in CodeFlavors) |
| 92 | + { |
| 93 | + var line = $"{codeFlavor} (0): 0x{code:X4}"; |
| 94 | + var decoded = _serialLineDecoder.DecodeLine(line, SelectedConsoleType); |
| 95 | + DecodedResultText += decoded != null |
| 96 | + ? new LogEntry { DecodedCode = decoded }.FormattedText |
| 97 | + : $"Decoding '{line}' failed"; |
| 98 | + DecodedResultText += "\n"; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + [RelayCommand] |
| 103 | + private void Close(Avalonia.Controls.Window window) |
| 104 | + { |
| 105 | + window.Close(); |
| 106 | + } |
| 107 | +} |
0 commit comments