-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugViewModel.cs
More file actions
124 lines (100 loc) · 3.54 KB
/
DebugViewModel.cs
File metadata and controls
124 lines (100 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using NodeSharp.NodeEngine;
using NodeSharp.Nodes.Common.Helper;
namespace NodeSharp.Client.ViewModel;
public partial class DebugViewModel : ObservableObject
{
private readonly NodeIo nodeIo;
public DebugViewModel(NodeIo nodeIo)
{
this.nodeIo = nodeIo;
DebugInfo = new ObservableCollection<DebugData>();
FileLogger.OnLogAdded += (sender, tuple) =>
{
System.Diagnostics.Debug.WriteLine($"OnLogAdded fired: {tuple.level} - {tuple.message}");
MainThread.BeginInvokeOnMainThread(() =>
{
System.Diagnostics.Debug.WriteLine($"Adding to DebugInfo collection. Count before: {DebugInfo.Count}");
DebugInfo.Add(new DebugData(tuple.message, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss:}: {tuple.level}", tuple.entry));
System.Diagnostics.Debug.WriteLine($"Count after: {DebugInfo.Count}");
});
};
WeakReferenceMessenger.Default.Register<NodeActionEvent>(this, (sender, args) =>
{
if (args.ActionEventType == NodeActionEventType.Add ||
args.ActionEventType == NodeActionEventType.Delete)
{
UpdateToolbarCommandStates();
}
});
}
public void UpdateToolbarCommandStates()
{
RunCommand.NotifyCanExecuteChanged();
StopCommand.NotifyCanExecuteChanged();
ClearCommand.NotifyCanExecuteChanged();
}
public ObservableCollection<DebugData>? DebugInfo { get; }
[RelayCommand(CanExecute = nameof(CanDoClear))]
private void Clear()
{
Debug.WriteLine("Clear debug window!");
DebugInfo?.Clear();
UpdateToolbarCommandStates();
}
[RelayCommand(CanExecute = nameof(CanDoRun))]
private async Task Run()
{
Debug.WriteLine("Run executed!");
await nodeIo.Run();
UpdateToolbarCommandStates();
}
[RelayCommand(CanExecute = nameof(CanDoStop))]
private void Stop()
{
Debug.WriteLine("Stop executed!");
nodeIo.Abort();
UpdateToolbarCommandStates();
}
private bool CanDoClear()
{
return nodeIo.IsFlowRunning;
}
private bool CanDoStop()
{
return nodeIo.IsFlowRunning;
}
private bool CanDoRun()
{
return nodeIo.Nodes.Count > 0 && !nodeIo.IsFlowRunning;
}
}
[SuppressMessage("CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "MVVMTK0045:Using [ObservableProperty] on fields is not AOT compatible for WinRT")]
public partial class DebugData : ObservableObject
{
private static int colorIndex = 0;
[ObservableProperty]
private string message;
[ObservableProperty]
private string level;
[ObservableProperty]
private string entry;
[ObservableProperty]
private Color backgroundColor;
[ObservableProperty]
private List<string> messages = ["qwerty", "asdfgh", "zxcvbn"];
public DebugData(string message, string level, string entry)
{
Message = message;
Level = level;
Entry = entry;
BackgroundColor = colorIndex % 2 == 0 ? Colors.White : Colors.LightGray;
colorIndex++;
messages.Insert(0, $"{DateTime.Now:yyyy-MM-dd HH:mm:ss:}: {message}");
}
}