mirror of
https://github.com/BlossomiShymae/Needlework.Net.git
synced 2025-12-06 18:20:47 +01:00
Update event viewer to have colored text
This commit is contained in:
22
Needlework.Net/ViewModels/EventViewModel.cs
Normal file
22
Needlework.Net/ViewModels/EventViewModel.cs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
using BlossomiShymae.GrrrLCU;
|
||||||
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Needlework.Net.ViewModels
|
||||||
|
{
|
||||||
|
public class EventViewModel : ObservableObject
|
||||||
|
{
|
||||||
|
public string Time { get; }
|
||||||
|
public string Type { get; }
|
||||||
|
public string Uri { get; }
|
||||||
|
|
||||||
|
public string Key => $"{Time} {Type} {Uri}";
|
||||||
|
|
||||||
|
public EventViewModel(EventData eventData)
|
||||||
|
{
|
||||||
|
Time = $"{DateTime.Now:HH:mm:ss.fff}";
|
||||||
|
Type = eventData?.EventType.ToUpper() ?? string.Empty;
|
||||||
|
Uri = eventData?.Uri ?? string.Empty;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,20 +16,20 @@ namespace Needlework.Net.ViewModels
|
|||||||
{
|
{
|
||||||
public partial class WebsocketViewModel : PageBase
|
public partial class WebsocketViewModel : PageBase
|
||||||
{
|
{
|
||||||
public ObservableCollection<string> EventLog { get; } = [];
|
public ObservableCollection<EventViewModel> EventLog { get; } = [];
|
||||||
public SemaphoreSlim EventLogLock { get; } = new(1, 1);
|
public SemaphoreSlim EventLogLock { get; } = new(1, 1);
|
||||||
|
|
||||||
[NotifyPropertyChangedFor(nameof(FilteredEventLog))]
|
[NotifyPropertyChangedFor(nameof(FilteredEventLog))]
|
||||||
[ObservableProperty] private string _search = string.Empty;
|
[ObservableProperty] private string _search = string.Empty;
|
||||||
[ObservableProperty] private bool _isAttach = true;
|
[ObservableProperty] private bool _isAttach = true;
|
||||||
[ObservableProperty] private bool _isTail = false;
|
[ObservableProperty] private bool _isTail = false;
|
||||||
[ObservableProperty] private string? _selectedEventLog = null;
|
[ObservableProperty] private EventViewModel? _selectedEventLog = null;
|
||||||
|
|
||||||
private Dictionary<string, EventMessage> _events = [];
|
private Dictionary<string, EventMessage> _events = [];
|
||||||
|
|
||||||
public WebsocketClient? Client { get; set; }
|
public WebsocketClient? Client { get; set; }
|
||||||
|
|
||||||
public IReadOnlyList<string> FilteredEventLog => string.IsNullOrWhiteSpace(Search) ? EventLog : [.. EventLog.Where(x => x.Contains(Search, StringComparison.InvariantCultureIgnoreCase))];
|
public IReadOnlyList<EventViewModel> FilteredEventLog => string.IsNullOrWhiteSpace(Search) ? EventLog : [.. EventLog.Where(x => x.Key.Contains(Search, StringComparison.InvariantCultureIgnoreCase))];
|
||||||
|
|
||||||
public WebsocketViewModel() : base("Event Viewer", "plug", -100)
|
public WebsocketViewModel() : base("Event Viewer", "plug", -100)
|
||||||
{
|
{
|
||||||
@@ -59,17 +59,10 @@ namespace Needlework.Net.ViewModels
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[RelayCommand]
|
partial void OnSelectedEventLogChanged(EventViewModel? value)
|
||||||
private void Clear()
|
|
||||||
{
|
|
||||||
_events.Clear();
|
|
||||||
EventLog.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
partial void OnSelectedEventLogChanged(string? value)
|
|
||||||
{
|
{
|
||||||
if (value == null) return;
|
if (value == null) return;
|
||||||
if (_events.TryGetValue(value, out var message))
|
if (_events.TryGetValue(value.Key, out var message))
|
||||||
{
|
{
|
||||||
var text = JsonSerializer.Serialize(message, App.JsonSerializerOptions);
|
var text = JsonSerializer.Serialize(message, App.JsonSerializerOptions);
|
||||||
if (text.Length >= App.MaxCharacters) WeakReferenceMessenger.Default.Send(new OopsiesDialogRequestedMessage(text));
|
if (text.Length >= App.MaxCharacters) WeakReferenceMessenger.Default.Send(new OopsiesDialogRequestedMessage(text));
|
||||||
@@ -77,6 +70,13 @@ namespace Needlework.Net.ViewModels
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[RelayCommand]
|
||||||
|
private void Clear()
|
||||||
|
{
|
||||||
|
_events.Clear();
|
||||||
|
EventLog.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
private void OnReconnection(ReconnectionInfo info)
|
private void OnReconnection(ReconnectionInfo info)
|
||||||
{
|
{
|
||||||
Trace.WriteLine($"-- Reconnection --\nType{info.Type}");
|
Trace.WriteLine($"-- Reconnection --\nType{info.Type}");
|
||||||
@@ -96,8 +96,7 @@ namespace Needlework.Net.ViewModels
|
|||||||
{
|
{
|
||||||
if (!IsAttach) return;
|
if (!IsAttach) return;
|
||||||
|
|
||||||
var line = $"{DateTime.Now:HH:mm:ss.fff} {message.Data?.EventType.ToUpper()} {message.Data?.Uri}";
|
var line = new EventViewModel(message.Data!);
|
||||||
Trace.WriteLine($"Message: {line}");
|
|
||||||
|
|
||||||
await EventLogLock.WaitAsync();
|
await EventLogLock.WaitAsync();
|
||||||
try
|
try
|
||||||
@@ -105,16 +104,16 @@ namespace Needlework.Net.ViewModels
|
|||||||
if (EventLog.Count < 1000)
|
if (EventLog.Count < 1000)
|
||||||
{
|
{
|
||||||
EventLog.Add(line);
|
EventLog.Add(line);
|
||||||
_events[line] = message;
|
_events[line.Key] = message;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var key = EventLog[0];
|
var _event = EventLog[0];
|
||||||
EventLog.RemoveAt(0);
|
EventLog.RemoveAt(0);
|
||||||
_events.Remove(key);
|
_events.Remove(_event.Key);
|
||||||
|
|
||||||
EventLog.Add(line);
|
EventLog.Add(line);
|
||||||
_events[line] = message;
|
_events[line.Key] = message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
|
|||||||
@@ -42,7 +42,26 @@
|
|||||||
Name="EventViewer"
|
Name="EventViewer"
|
||||||
Margin="0 8 0 0"
|
Margin="0 8 0 0"
|
||||||
ItemsSource="{Binding FilteredEventLog}"
|
ItemsSource="{Binding FilteredEventLog}"
|
||||||
SelectedItem="{Binding SelectedEventLog}"/>
|
SelectedItem="{Binding SelectedEventLog}"
|
||||||
|
ScrollViewer.HorizontalScrollBarVisibility="Auto">
|
||||||
|
<ListBox.ItemTemplate>
|
||||||
|
<DataTemplate>
|
||||||
|
<Grid ColumnDefinitions="auto,auto,*">
|
||||||
|
<TextBlock Text="{Binding Time}"
|
||||||
|
Margin="0 0 4 0"
|
||||||
|
Grid.Column="0"
|
||||||
|
Foreground="#8be9fd"/>
|
||||||
|
<TextBlock Text="{Binding Type}"
|
||||||
|
Grid.Column="1"
|
||||||
|
Margin="0 0 4 0"
|
||||||
|
Foreground="#ffb86c"/>
|
||||||
|
<TextBlock Text="{Binding Uri}"
|
||||||
|
Grid.Column="2"
|
||||||
|
Foreground="#f8f8f2"/>
|
||||||
|
</Grid>
|
||||||
|
</DataTemplate>
|
||||||
|
</ListBox.ItemTemplate>
|
||||||
|
</ListBox>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Border>
|
</Border>
|
||||||
<GridSplitter Grid.Row="1" ResizeDirection="Rows" Background="Gray"/>
|
<GridSplitter Grid.Row="1" ResizeDirection="Rows" Background="Gray"/>
|
||||||
|
|||||||
Reference in New Issue
Block a user