Skip to content

Commit

Permalink
event log filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
exp111 committed Nov 12, 2023
1 parent 62133b6 commit c700dd4
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Turbulence.Core/ViewModels/Design/DesignLogViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class DesignLogViewModel : LogViewModel

public DesignLogViewModel()
{
Logs.AddRange(new LogEntry[]
_logs.AddRange(new LogEntry[]
{
new("Request 1", LogType.Images, LogLevel.Debug, DateTime.Now),
new("Request 2", LogType.Networking, LogLevel.Info, DateTime.Now),
Expand All @@ -21,6 +21,6 @@ public override void Refresh()
{
var type = (LogType)types.GetValue(random.Next(types.Length))!;
var lvl = (LogLevel)levels.GetValue(random.Next(levels.Length))!;
Logs.Add(new($"New Request {Logs.Count}", type, lvl, DateTime.Now));
_logs.Add(new($"New Request {_logs.Count}", type, lvl, DateTime.Now));
}
}
37 changes: 34 additions & 3 deletions Turbulence.Core/ViewModels/LogViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkit.Mvvm.Input;
using Turbulence.Discord.Services;

Expand All @@ -7,16 +8,46 @@ namespace Turbulence.Core.ViewModels;
public partial class LogViewModel : ViewModelBase
{
private readonly ILogger _logger = Ioc.Default.GetService<ILogger>()!;
public ObservableList<LogEntry> Logs { get; } = new();
public ObservableList<LogEntry> _logs { get; } = new();
public IEnumerable<LogEntry>? Logs { get; set; }

[ObservableProperty]
public int _selectedLevel = (int)LogLevel.Info;

private LogType _selectedType = LogType.Any;

public LogViewModel()
{
_logs.CollectionChanged += (_, _) => UpdateFilters();
PropertyChanged += (_, args) =>
{
if (args.PropertyName == nameof(SelectedLevel))
UpdateFilters();
};
Refresh();
}

public void SelectType(LogType t)
{
_selectedType = t;
UpdateFilters();
}

private void UpdateFilters()
{
Logs = _logs.Where(LogFilter);
OnPropertyChanged(nameof(Logs));
}

private bool LogFilter(LogEntry entry)
{
return entry.Level >= (LogLevel)SelectedLevel &&
(_selectedType == LogType.Any || entry.Type == _selectedType);
}

[RelayCommand]
public virtual void Refresh()
{
Logs.ReplaceAll(_logger.GetLogs());
_logs.ReplaceAll(_logger.GetLogs());
}
}
17 changes: 9 additions & 8 deletions Turbulence.Desktop/LogWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@
<Menu DockPanel.Dock="Top" Background="#23272A">
<MenuItem Header="Refresh" Command="{Binding RefreshCommand}" />
</Menu>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Spacing="5">
<!--TODO: Make these do something-->
<ComboBox Name="LevelBox">
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Spacing="10">
<ComboBox Name="LevelBox" SelectedIndex="{Binding SelectedLevel}">
<ComboBoxItem>Debug</ComboBoxItem>
<ComboBoxItem IsSelected="True">Info</ComboBoxItem>
<ComboBoxItem>Info</ComboBoxItem>
<ComboBoxItem>Warning</ComboBoxItem>
</ComboBox>
<RadioButton GroupName="Types" IsChecked="True">All</RadioButton>
<RadioButton GroupName="Types">Application</RadioButton>
<RadioButton GroupName="Types">Networking</RadioButton>
<RadioButton GroupName="Types">Images</RadioButton>
<StackPanel Orientation="Horizontal" Spacing="5">
<RadioButton GroupName="Types" Checked="RadioChecked" Content="Any" IsChecked="True" />
<RadioButton GroupName="Types" Checked="RadioChecked" Content="Application" />
<RadioButton GroupName="Types" Checked="RadioChecked" Content="Networking" />
<RadioButton GroupName="Types" Checked="RadioChecked" Content="Images" />
</StackPanel>
</StackPanel>
<DataGrid Name="EventList" ItemsSource="{Binding Logs}" IsReadOnly="True" Focusable="False">
<DataGrid.Columns>
Expand Down
23 changes: 23 additions & 0 deletions Turbulence.Desktop/LogWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using Avalonia.Controls;
using Avalonia.Interactivity;
using Turbulence.Core.ViewModels;
using Turbulence.Core.ViewModels.Design;
using Turbulence.Discord.Services;

namespace Turbulence.Desktop;

public partial class LogWindow : Window
{
private LogViewModel _vm;
public LogWindow()
{
InitializeComponent();
Expand All @@ -13,5 +17,24 @@ public LogWindow()
// Workaround to fix design data context getting overwritten
DataContext = new DesignLogViewModel();
}
_vm = (LogViewModel)DataContext!;
}

public void RadioChecked(object sender, RoutedEventArgs args)
{
if (_vm == null) //FIXME: why is the vm null :weary:
return;

try
{
var button = (RadioButton)args.Source!;
var val = button.Content as string;
var type = Enum.Parse<LogType>(val!);
_vm.SelectType(type);
}
catch (Exception e)
{
throw new Exception($"Sender is invalid or has invalid content: {e}");
}
}
}
1 change: 1 addition & 0 deletions Turbulence.Discord/Services/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public interface ILogger

public enum LogType
{
Any = -1,
Application,
Networking,
Images
Expand Down

0 comments on commit c700dd4

Please sign in to comment.