From fd54708c709cc2d62d08f99fa7ada101bdb3802f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Thu, 2 Feb 2023 15:20:32 +0100 Subject: [PATCH] feat: add packet document that loads packet providers --- .../ViewModels/LogTabViewModel.cs | 11 +- .../ViewModels/PacketLogDocumentViewModel.cs | 105 ++++++++++++++++++ ...FilterTab.axaml => LogFilterTabView.axaml} | 2 +- ...Tab.axaml.cs => LogFilterTabView.axaml.cs} | 8 +- .../Views/{LogTab.axaml => LogTabView.axaml} | 4 +- .../{LogTab.axaml.cs => LogTabView.axaml.cs} | 2 +- .../Views/PacketLogDocumentView.axaml | 22 ++++ .../Views/PacketLogDocumentView.axaml.cs | 29 +++++ 8 files changed, 171 insertions(+), 12 deletions(-) create mode 100644 src/PacketLogger/ViewModels/PacketLogDocumentViewModel.cs rename src/PacketLogger/Views/{LogFilterTab.axaml => LogFilterTabView.axaml} (97%) rename src/PacketLogger/Views/{LogFilterTab.axaml.cs => LogFilterTabView.axaml.cs} (84%) rename src/PacketLogger/Views/{LogTab.axaml => LogTabView.axaml} (96%) rename src/PacketLogger/Views/{LogTab.axaml.cs => LogTabView.axaml.cs} (96%) create mode 100644 src/PacketLogger/Views/PacketLogDocumentView.axaml create mode 100644 src/PacketLogger/Views/PacketLogDocumentView.axaml.cs diff --git a/src/PacketLogger/ViewModels/LogTabViewModel.cs b/src/PacketLogger/ViewModels/LogTabViewModel.cs index 2e6de37..67a4513 100644 --- a/src/PacketLogger/ViewModels/LogTabViewModel.cs +++ b/src/PacketLogger/ViewModels/LogTabViewModel.cs @@ -29,8 +29,11 @@ public class LogTabViewModel : ViewModelBase, IDisposable /// /// Initializes a new instance of the class. /// - public LogTabViewModel() + /// The packet provider. + public LogTabViewModel(IPacketProvider packetProvider) { + Provider = packetProvider; + var dynamicFilter = this.WhenValueChanged(@this => @this.CurrentFilter) .Select ( @@ -58,12 +61,12 @@ public class LogTabViewModel : ViewModelBase, IDisposable CopyPackets = ReactiveCommand.CreateFromObservable ( - (l) => Observable.StartAsync + list => Observable.StartAsync ( async () => { var clipboardString = string.Join - ('\n', l.OfType().Select(x => x.PacketString)); + ('\n', list.OfType().Select(x => x.PacketString)); await Application.Current!.Clipboard!.SetTextAsync(clipboardString); } ) @@ -123,7 +126,7 @@ public class LogTabViewModel : ViewModelBase, IDisposable /// /// Gets packet provider. /// - public IPacketProvider Provider { get; } = new DummyPacketProvider(); + public IPacketProvider Provider { get; } /// /// Gets whether the pane is open. diff --git a/src/PacketLogger/ViewModels/PacketLogDocumentViewModel.cs b/src/PacketLogger/ViewModels/PacketLogDocumentViewModel.cs new file mode 100644 index 0000000..933f68e --- /dev/null +++ b/src/PacketLogger/ViewModels/PacketLogDocumentViewModel.cs @@ -0,0 +1,105 @@ +// +// PacketLogDocumentViewModel.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.ComponentModel; +using System.Linq; +using System.Reactive; +using System.Reactive.Disposables; +using System.Reactive.Linq; +using System.Reflection; +using System.Threading.Tasks; +using Avalonia.Controls; +using Avalonia.Controls.ApplicationLifetimes; +using Dock.Model.Mvvm.Controls; +using PacketLogger.Models.Packets; +using ReactiveUI; + +namespace PacketLogger.ViewModels; + +/// +public class PacketLogDocumentViewModel : Document, INotifyPropertyChanged +{ + /// + /// Initializes a new instance of the class. + /// + public PacketLogDocumentViewModel() + { + OpenDummy = ReactiveCommand.CreateFromTask + ( + () => Task.Run(() => + { + Loading = true; + Name = "Dummy"; + LogViewModel = new LogTabViewModel(new DummyPacketProvider()); + Loaded = true; + }) + ); + OpenFile = ReactiveCommand.CreateFromTask + ( + async () => + { + var mainWindow = (App.Current!.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime) + ?.MainWindow; + var result = await new OpenFileDialog() + { + AllowMultiple = false, + InitialFileName = Assembly.GetEntryAssembly()?.GetModules().FirstOrDefault()?.FullyQualifiedName + }.ShowAsync(mainWindow!); + + if (result is null || result.Length == 0) + { + return; + } + Loading = true; + + var path = result[0]; + var provider = new FilePacketProvider(path); + + var openResult = await provider.Open(); + if (!openResult.IsSuccess) + { + Console.WriteLine("Could not open the file."); + return; + } + + LogViewModel = new LogTabViewModel(provider); + Loaded = true; + Loading = false; + } + ); + } + + /// + /// Gets or sets the name of the tab. + /// + public string Name { get; set; } = "New tab"; + + /// + /// Gets whether the document is currently being loaded. + /// + public bool Loading { get; private set; } = false; + + /// + /// Gets whether a document has been loaded. + /// + public bool Loaded { get; private set; } + + /// + /// Gets the log tab view model. + /// + public LogTabViewModel? LogViewModel { get; private set; } + + /// + /// Gets command for opening a dummy. + /// + public ReactiveCommand OpenDummy { get; } + + /// + /// Gets command for opening a file. + /// + public ReactiveCommand OpenFile { get; } +} \ No newline at end of file diff --git a/src/PacketLogger/Views/LogFilterTab.axaml b/src/PacketLogger/Views/LogFilterTabView.axaml similarity index 97% rename from src/PacketLogger/Views/LogFilterTab.axaml rename to src/PacketLogger/Views/LogFilterTabView.axaml index 31e28ee..3eb94f5 100644 --- a/src/PacketLogger/Views/LogFilterTab.axaml +++ b/src/PacketLogger/Views/LogFilterTabView.axaml @@ -4,7 +4,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:vm="clr-namespace:PacketLogger.ViewModels" mc:Ignorable="d" d:DesignWidth="400" d:DesignHeight="450" - x:Class="PacketLogger.Views.LogFilterTab"> + x:Class="PacketLogger.Views.LogFilterTabView"> diff --git a/src/PacketLogger/Views/LogFilterTab.axaml.cs b/src/PacketLogger/Views/LogFilterTabView.axaml.cs similarity index 84% rename from src/PacketLogger/Views/LogFilterTab.axaml.cs rename to src/PacketLogger/Views/LogFilterTabView.axaml.cs index f54898a..35698ed 100644 --- a/src/PacketLogger/Views/LogFilterTab.axaml.cs +++ b/src/PacketLogger/Views/LogFilterTabView.axaml.cs @@ -1,5 +1,5 @@ // -// LogFilterTab.axaml.cs +// LogFilterTabView.axaml.cs // // Copyright (c) František Boháček. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. @@ -14,12 +14,12 @@ using PropertyChanged; namespace PacketLogger.Views; [DoNotNotify] -public partial class LogFilterTab : UserControl +public partial class LogFilterTabView : UserControl { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// - public LogFilterTab() + public LogFilterTabView() { InitializeComponent(); this.FindControl("FilterType").Items = Enum.GetValues(); diff --git a/src/PacketLogger/Views/LogTab.axaml b/src/PacketLogger/Views/LogTabView.axaml similarity index 96% rename from src/PacketLogger/Views/LogTab.axaml rename to src/PacketLogger/Views/LogTabView.axaml index f869094..d9cd529 100644 --- a/src/PacketLogger/Views/LogTab.axaml +++ b/src/PacketLogger/Views/LogTabView.axaml @@ -41,14 +41,14 @@ Recv - + Send - + diff --git a/src/PacketLogger/Views/LogTab.axaml.cs b/src/PacketLogger/Views/LogTabView.axaml.cs similarity index 96% rename from src/PacketLogger/Views/LogTab.axaml.cs rename to src/PacketLogger/Views/LogTabView.axaml.cs index ae33b6e..9b9d7dd 100644 --- a/src/PacketLogger/Views/LogTab.axaml.cs +++ b/src/PacketLogger/Views/LogTabView.axaml.cs @@ -1,5 +1,5 @@ // -// LogTab.axaml.cs +// LogTabView.axaml.cs // // Copyright (c) František Boháček. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. diff --git a/src/PacketLogger/Views/PacketLogDocumentView.axaml b/src/PacketLogger/Views/PacketLogDocumentView.axaml new file mode 100644 index 0000000..27fb008 --- /dev/null +++ b/src/PacketLogger/Views/PacketLogDocumentView.axaml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/PacketLogger/Views/PacketLogDocumentView.axaml.cs b/src/PacketLogger/Views/PacketLogDocumentView.axaml.cs new file mode 100644 index 0000000..169c29a --- /dev/null +++ b/src/PacketLogger/Views/PacketLogDocumentView.axaml.cs @@ -0,0 +1,29 @@ +// +// PacketLogDocumentView.axaml.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Avalonia; +using Avalonia.Controls; +using Avalonia.Markup.Xaml; +using PropertyChanged; + +namespace PacketLogger.Views; + +[DoNotNotify] +public partial class PacketLogDocumentView : UserControl +{ + /// + /// Initializes a new instance of the class. + /// + public PacketLogDocumentView() + { + InitializeComponent(); + } + + private void InitializeComponent() + { + AvaloniaXamlLoader.Load(this); + } +} \ No newline at end of file -- 2.49.0