From ea26a5fc512b90bbe71167e3f9951d45499702e1 Mon Sep 17 00:00:00 2001 From: Mccree Lee <2935876049@qq.com> Date: Mon, 22 Apr 2024 11:39:28 +0800 Subject: [PATCH] perf: refactor --- src/PipManager/App.xaml.cs | 2 ++ src/PipManager/Models/Package/PackageItem.cs | 25 ++++++++-------- .../Environment/EnvironmentService.cs | 29 ++++++++++--------- .../Services/Overlay/IOverlayService.cs | 9 ++++++ .../Services/Overlay/OverlayService.cs | 23 +++++++++++++++ .../ViewModels/Pages/Lab/LabViewModel.cs | 9 ++---- .../Pages/Library/LibraryDetailViewModel.cs | 7 +++-- .../Pages/Library/LibraryViewModel.cs | 9 +++--- .../Pages/Overlay/OverlayViewModel.cs | 17 ++--------- .../ViewModels/Windows/MainWindowViewModel.cs | 3 ++ src/PipManager/Views/Windows/MainWindow.xaml | 23 +++++++++------ 11 files changed, 91 insertions(+), 65 deletions(-) create mode 100644 src/PipManager/Services/Overlay/IOverlayService.cs create mode 100644 src/PipManager/Services/Overlay/OverlayService.cs diff --git a/src/PipManager/App.xaml.cs b/src/PipManager/App.xaml.cs index a343dbf..f942969 100644 --- a/src/PipManager/App.xaml.cs +++ b/src/PipManager/App.xaml.cs @@ -31,6 +31,7 @@ using System.Net.Http; using System.Runtime.InteropServices; using System.Windows.Threading; +using PipManager.Services.Overlay; using PipManager.ViewModels.Pages.Overlay; using PipManager.Views.Pages.Overlay; using Python.Runtime; @@ -84,6 +85,7 @@ public partial class App services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); // Pages services.AddSingleton(); diff --git a/src/PipManager/Models/Package/PackageItem.cs b/src/PipManager/Models/Package/PackageItem.cs index 3f019cf..8dadf0a 100644 --- a/src/PipManager/Models/Package/PackageItem.cs +++ b/src/PipManager/Models/Package/PackageItem.cs @@ -1,21 +1,20 @@ using PipManager.Models.Pages; -using PipManager.Services.Environment.Response; namespace PipManager.Models.Package; public class PackageItem { - public string? Name { get; set; } - public string? Version { get; set; } - public PackageVersion? DetailedVersion { get; set; } - public string? Path { get; set; } - public string? DistInfoPath { get; set; } - public string? Summary { get; set; } - public List? Author { get; set; } - public string? AuthorEmail { get; set; } - public List? ProjectUrl { get; set; } - public Dictionary>? Classifier { get; set; } + public string? Name { get; init; } + public string? Version { get; init; } + public PackageVersion? DetailedVersion { get; init; } + public string? Path { get; init; } + public string? DistInfoPath { get; init; } + public string? Summary { get; init; } + public List? Author { get; init; } + public string? AuthorEmail { get; init; } + public List? ProjectUrl { get; init; } + public Dictionary>? Classifier { get; init; } public List? RequiresDist { get; init; } - public Dictionary>? Metadata { get; set; } - public List? Record { get; set; } + public Dictionary>? Metadata { get; init; } + public List? Record { get; init; } } \ No newline at end of file diff --git a/src/PipManager/Services/Environment/EnvironmentService.cs b/src/PipManager/Services/Environment/EnvironmentService.cs index e3cddfa..1de9d65 100644 --- a/src/PipManager/Services/Environment/EnvironmentService.cs +++ b/src/PipManager/Services/Environment/EnvironmentService.cs @@ -96,7 +96,7 @@ public ActionResponse PurgeEnvironmentCache(EnvironmentItem environmentItem) // Metadata var metadataDict = new Dictionary>(); var lastValidKey = ""; - var lastValidPos = 0; + var lastValidIndex = 0; var classifiers = new Dictionary>(); await foreach (var line in File.ReadLinesAsync(Path.Combine(distInfoDirectoryFullName, "METADATA"))) { @@ -114,18 +114,18 @@ public ActionResponse PurgeEnvironmentCache(EnvironmentItem environmentItem) { metadataDict.Add(key, []); lastValidKey = key; - lastValidPos = 0; + lastValidIndex = 0; } else { - lastValidPos++; + lastValidIndex++; } metadataDict[key].Add(value); } else { - metadataDict[lastValidKey][lastValidPos] = string.Join(metadataDict[lastValidKey][lastValidPos], "\n", value); + metadataDict[lastValidKey][lastValidIndex] += "\n" + value; } } @@ -133,19 +133,21 @@ public ActionResponse PurgeEnvironmentCache(EnvironmentItem environmentItem) { var keyValues = item.Split(" :: "); - if (keyValues.Length >= 2) + if (keyValues.Length < 2) { - var key = keyValues[0]; - var value = string.Join(" :: ", keyValues[1..]); + continue; + } - if (!classifiers.TryGetValue(key, out var existingList)) - { - existingList = []; - classifiers.Add(key, existingList); - } + var key = keyValues[0]; + var value = string.Join(" :: ", keyValues[1..]); - existingList.Add(value); + if (!classifiers.TryGetValue(key, out var existingList)) + { + existingList = []; + classifiers.Add(key, existingList); } + + existingList.Add(value); } // Record @@ -260,7 +262,6 @@ public ParseRequirementsResponse ParseRequirements(IEnumerable requireme Log.Error(ex.Message); } } - return parsedRequirements; } diff --git a/src/PipManager/Services/Overlay/IOverlayService.cs b/src/PipManager/Services/Overlay/IOverlayService.cs new file mode 100644 index 0000000..c040dd1 --- /dev/null +++ b/src/PipManager/Services/Overlay/IOverlayService.cs @@ -0,0 +1,9 @@ +using PipManager.Models.Package; + +namespace PipManager.Services.Overlay; + +public interface IOverlayService +{ + public void ShowPackageUpdateOverlay(List packageUpdates, System.Action callback); + +} diff --git a/src/PipManager/Services/Overlay/OverlayService.cs b/src/PipManager/Services/Overlay/OverlayService.cs new file mode 100644 index 0000000..1f54e10 --- /dev/null +++ b/src/PipManager/Services/Overlay/OverlayService.cs @@ -0,0 +1,23 @@ +using System.Collections.ObjectModel; +using PipManager.Models.Package; +using PipManager.ViewModels.Pages.Overlay; +using PipManager.ViewModels.Windows; +using PipManager.Views.Windows; + +namespace PipManager.Services.Overlay; + +public class OverlayService(OverlayViewModel overlayViewModel, MainWindowViewModel mainWindowViewModel): IOverlayService +{ + private void ShowOverlay() + { + overlayViewModel.IsOverlayVisible = true; + mainWindowViewModel.IsTitleBarCoverageGridVisible = false; + } + + public void ShowPackageUpdateOverlay(List packageUpdates, System.Action callback) + { + overlayViewModel.ConfirmCallback = callback; + overlayViewModel.PackageUpdateItems = new ObservableCollection(packageUpdates); + ShowOverlay(); + } +} diff --git a/src/PipManager/ViewModels/Pages/Lab/LabViewModel.cs b/src/PipManager/ViewModels/Pages/Lab/LabViewModel.cs index 203eaf4..86cdde5 100644 --- a/src/PipManager/ViewModels/Pages/Lab/LabViewModel.cs +++ b/src/PipManager/ViewModels/Pages/Lab/LabViewModel.cs @@ -1,11 +1,6 @@ -using System.IO; -using System.Windows.Shapes; -using Microsoft.Extensions.Configuration; -using PipManager.Models.Action; +using PipManager.Models.Action; using PipManager.Services.Action; -using PipManager.Services.Configuration; using PipManager.Services.Environment; -using Python.Runtime; using Serilog; using Wpf.Ui.Controls; @@ -20,7 +15,7 @@ public partial class LabViewModel(IActionService actionService, IEnvironmentServ private void ParseTest() { var parsed = environmentService.ParseRequirements(["requests", "numpy"]); - parsed.Requirements.ForEach(item => Log.Information(item.Specifier)); + parsed.Requirements?.ForEach(item => Log.Information(item.Specifier)); } [RelayCommand] diff --git a/src/PipManager/ViewModels/Pages/Library/LibraryDetailViewModel.cs b/src/PipManager/ViewModels/Pages/Library/LibraryDetailViewModel.cs index 652b018..ac98f47 100644 --- a/src/PipManager/ViewModels/Pages/Library/LibraryDetailViewModel.cs +++ b/src/PipManager/ViewModels/Pages/Library/LibraryDetailViewModel.cs @@ -5,7 +5,6 @@ using System.Collections.ObjectModel; using PipManager.Services.Environment; using PipManager.Services.Environment.Response; -using Serilog; using Wpf.Ui.Controls; namespace PipManager.ViewModels.Pages.Library; @@ -75,8 +74,10 @@ private void Receive(object recipient, LibraryDetailMessage message) #endregion Contact - RequireDist = new ObservableCollection(_environmentService.ParseRequirements(Package.RequiresDist).Requirements); - + if (Package.RequiresDist != null) + { + RequireDist = new ObservableCollection(_environmentService.ParseRequirements(Package.RequiresDist).Requirements!); + } #region Classifier diff --git a/src/PipManager/ViewModels/Pages/Library/LibraryViewModel.cs b/src/PipManager/ViewModels/Pages/Library/LibraryViewModel.cs index 4bb123d..9a1b419 100644 --- a/src/PipManager/ViewModels/Pages/Library/LibraryViewModel.cs +++ b/src/PipManager/ViewModels/Pages/Library/LibraryViewModel.cs @@ -14,6 +14,7 @@ using PipManager.Views.Pages.Library; using Serilog; using System.Collections.ObjectModel; +using PipManager.Services.Overlay; using PipManager.ViewModels.Pages.Overlay; using Wpf.Ui; using Wpf.Ui.Appearance; @@ -34,9 +35,9 @@ public partial class LibraryViewModel : ObservableObject, INavigationAware private readonly IMaskService _maskService; private readonly IToastService _toastService; private readonly IContentDialogService _contentDialogService; - private readonly OverlayViewModel _overlayViewModel; + private readonly IOverlayService _overlayService; - public LibraryViewModel(INavigationService navigationService, IEnvironmentService environmentService, OverlayViewModel overlayViewModel, + public LibraryViewModel(INavigationService navigationService, IEnvironmentService environmentService, IOverlayService overlayService, IConfigurationService configurationService, IActionService actionService, IThemeService themeService, IMaskService maskService, IToastService toastService, IContentDialogService contentDialogService) { _navigationService = navigationService; @@ -46,7 +47,7 @@ public LibraryViewModel(INavigationService navigationService, IEnvironmentServic _maskService = maskService; _toastService = toastService; _contentDialogService = contentDialogService; - _overlayViewModel = overlayViewModel; + _overlayService = overlayService; themeService.SetTheme(_configurationService.AppConfig.Personalization.Theme switch { @@ -137,7 +138,7 @@ await Task.Run(() => } else { - _overlayViewModel.ShowPackageUpdateOverlay(msgList, () => + _overlayService.ShowPackageUpdateOverlay(msgList, () => { _actionService.AddOperation(new ActionListItem ( diff --git a/src/PipManager/ViewModels/Pages/Overlay/OverlayViewModel.cs b/src/PipManager/ViewModels/Pages/Overlay/OverlayViewModel.cs index fcfc453..4a535d9 100644 --- a/src/PipManager/ViewModels/Pages/Overlay/OverlayViewModel.cs +++ b/src/PipManager/ViewModels/Pages/Overlay/OverlayViewModel.cs @@ -6,7 +6,7 @@ namespace PipManager.ViewModels.Pages.Overlay; public partial class OverlayViewModel: ObservableObject { - private System.Action? _confirmCallback; + public System.Action? ConfirmCallback { get; set; } [ObservableProperty] private bool _isOverlayVisible; @@ -17,26 +17,13 @@ private void CloseOverlay() IsOverlayVisible = false; App.GetService().TitleBarCoverageGrid.Visibility = Visibility.Collapsed; } - - private void ShowOverlay() - { - IsOverlayVisible = true; - App.GetService().TitleBarCoverageGrid.Visibility = Visibility.Visible; - } [ObservableProperty] private ObservableCollection _packageUpdateItems = []; - public void ShowPackageUpdateOverlay(List packageUpdates, System.Action callback) - { - _confirmCallback = callback; - PackageUpdateItems = new ObservableCollection(packageUpdates); - ShowOverlay(); - } - [RelayCommand] private void Confirm() { CloseOverlay(); - _confirmCallback?.Invoke(); + ConfirmCallback?.Invoke(); } } diff --git a/src/PipManager/ViewModels/Windows/MainWindowViewModel.cs b/src/PipManager/ViewModels/Windows/MainWindowViewModel.cs index 37a7ae3..69d825c 100644 --- a/src/PipManager/ViewModels/Windows/MainWindowViewModel.cs +++ b/src/PipManager/ViewModels/Windows/MainWindowViewModel.cs @@ -23,6 +23,9 @@ public MainWindowViewModel(IConfigurationService configurationService, IEnvironm ApplicationTitle = "Pip Manager"; } } + + [ObservableProperty] + private bool _isTitleBarCoverageGridVisible; [ObservableProperty] private string _applicationTitle = "Pip Manager"; diff --git a/src/PipManager/Views/Windows/MainWindow.xaml b/src/PipManager/Views/Windows/MainWindow.xaml index 1c0d1c6..9d05125 100644 --- a/src/PipManager/Views/Windows/MainWindow.xaml +++ b/src/PipManager/Views/Windows/MainWindow.xaml @@ -29,13 +29,13 @@ xmlns:local="clr-namespace:PipManager.Views.Windows" xmlns:mask="clr-namespace:PipManager.Controls.Mask" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:overlay="clr-namespace:PipManager.Views.Pages.Overlay" xmlns:search="clr-namespace:PipManager.Views.Pages.Search" xmlns:settings="clr-namespace:PipManager.Views.Pages.Settings" xmlns:tools="clr-namespace:PipManager.Views.Pages.Tools" xmlns:tray="http://schemas.lepo.co/wpfui/2022/xaml/tray" xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml" - xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" - xmlns:overlay="clr-namespace:PipManager.Views.Pages.Overlay"> + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> @@ -99,8 +99,8 @@ Grid.Row="0" Grid.RowSpan="2" x:Name="MaskPresenter" /> - - + + - - - - + + + +