Skip to content

Commit

Permalink
perf: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
AuroraZiling committed Apr 22, 2024
1 parent 31c9ff1 commit ea26a5f
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 65 deletions.
2 changes: 2 additions & 0 deletions src/PipManager/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -84,6 +85,7 @@ public partial class App
services.AddSingleton<IContentDialogService, ContentDialogService>();
services.AddSingleton<ITaskBarService, TaskBarService>();
services.AddSingleton<IPackageSearchService, PackageSearchService>();
services.AddSingleton<IOverlayService, OverlayService>();
// Pages
services.AddSingleton<LibraryPage>();
Expand Down
25 changes: 12 additions & 13 deletions src/PipManager/Models/Package/PackageItem.cs
Original file line number Diff line number Diff line change
@@ -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<string>? Author { get; set; }
public string? AuthorEmail { get; set; }
public List<LibraryDetailProjectUrlModel>? ProjectUrl { get; set; }
public Dictionary<string, List<string>>? 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<string>? Author { get; init; }
public string? AuthorEmail { get; init; }
public List<LibraryDetailProjectUrlModel>? ProjectUrl { get; init; }
public Dictionary<string, List<string>>? Classifier { get; init; }
public List<string>? RequiresDist { get; init; }
public Dictionary<string, List<string>>? Metadata { get; set; }
public List<string>? Record { get; set; }
public Dictionary<string, List<string>>? Metadata { get; init; }
public List<string>? Record { get; init; }
}
29 changes: 15 additions & 14 deletions src/PipManager/Services/Environment/EnvironmentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public ActionResponse PurgeEnvironmentCache(EnvironmentItem environmentItem)
// Metadata
var metadataDict = new Dictionary<string, List<string>>();
var lastValidKey = "";
var lastValidPos = 0;
var lastValidIndex = 0;
var classifiers = new Dictionary<string, List<string>>();
await foreach (var line in File.ReadLinesAsync(Path.Combine(distInfoDirectoryFullName, "METADATA")))
{
Expand All @@ -114,38 +114,40 @@ 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;
}
}
foreach (var item in metadataDict.GetValueOrDefault("classifier", []))
{
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
Expand Down Expand Up @@ -260,7 +262,6 @@ public ParseRequirementsResponse ParseRequirements(IEnumerable<string> requireme
Log.Error(ex.Message);
}
}

return parsedRequirements;
}

Expand Down
9 changes: 9 additions & 0 deletions src/PipManager/Services/Overlay/IOverlayService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using PipManager.Models.Package;

namespace PipManager.Services.Overlay;

public interface IOverlayService
{
public void ShowPackageUpdateOverlay(List<PackageUpdateItem> packageUpdates, System.Action callback);

}
23 changes: 23 additions & 0 deletions src/PipManager/Services/Overlay/OverlayService.cs
Original file line number Diff line number Diff line change
@@ -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<PackageUpdateItem> packageUpdates, System.Action callback)
{
overlayViewModel.ConfirmCallback = callback;
overlayViewModel.PackageUpdateItems = new ObservableCollection<PackageUpdateItem>(packageUpdates);
ShowOverlay();
}
}
9 changes: 2 additions & 7 deletions src/PipManager/ViewModels/Pages/Lab/LabViewModel.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -75,8 +74,10 @@ private void Receive(object recipient, LibraryDetailMessage message)

#endregion Contact

RequireDist = new ObservableCollection<ParsedRequirement>(_environmentService.ParseRequirements(Package.RequiresDist).Requirements);

if (Package.RequiresDist != null)
{
RequireDist = new ObservableCollection<ParsedRequirement>(_environmentService.ParseRequirements(Package.RequiresDist).Requirements!);
}

#region Classifier

Expand Down
9 changes: 5 additions & 4 deletions src/PipManager/ViewModels/Pages/Library/LibraryViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -137,7 +138,7 @@ await Task.Run(() =>
}
else
{
_overlayViewModel.ShowPackageUpdateOverlay(msgList, () =>
_overlayService.ShowPackageUpdateOverlay(msgList, () =>
{
_actionService.AddOperation(new ActionListItem
(
Expand Down
17 changes: 2 additions & 15 deletions src/PipManager/ViewModels/Pages/Overlay/OverlayViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,26 +17,13 @@ private void CloseOverlay()
IsOverlayVisible = false;
App.GetService<MainWindow>().TitleBarCoverageGrid.Visibility = Visibility.Collapsed;
}

private void ShowOverlay()
{
IsOverlayVisible = true;
App.GetService<MainWindow>().TitleBarCoverageGrid.Visibility = Visibility.Visible;
}

[ObservableProperty] private ObservableCollection<PackageUpdateItem> _packageUpdateItems = [];

public void ShowPackageUpdateOverlay(List<PackageUpdateItem> packageUpdates, System.Action callback)
{
_confirmCallback = callback;
PackageUpdateItems = new ObservableCollection<PackageUpdateItem>(packageUpdates);
ShowOverlay();
}

[RelayCommand]
private void Confirm()
{
CloseOverlay();
_confirmCallback?.Invoke();
ConfirmCallback?.Invoke();
}
}
3 changes: 3 additions & 0 deletions src/PipManager/ViewModels/Windows/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public MainWindowViewModel(IConfigurationService configurationService, IEnvironm
ApplicationTitle = "Pip Manager";
}
}

[ObservableProperty]
private bool _isTitleBarCoverageGridVisible;

[ObservableProperty]
private string _applicationTitle = "Pip Manager";
Expand Down
23 changes: 14 additions & 9 deletions src/PipManager/Views/Windows/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<Grid>
<Grid.RowDefinitions>
Expand Down Expand Up @@ -99,26 +99,31 @@
Grid.Row="0"
Grid.RowSpan="2"
x:Name="MaskPresenter" />
<overlay:OverlayPage Grid.Row="0" Grid.RowSpan="2"/>

<overlay:OverlayPage Grid.Row="0" Grid.RowSpan="2" />

<ContentPresenter
Grid.Row="0"
Grid.RowSpan="2"
x:Name="RootContentDialog" />

<ui:TitleBar
Grid.Row="0"
CloseWindowByDoubleClickOnIcon="True"
Grid.Row="0"
Title="{Binding ViewModel.ApplicationTitle}">
<ui:TitleBar.Icon>
<ui:ImageIcon Source="pack://application:,,,/Assets/icon.png" />
</ui:TitleBar.Icon>
</ui:TitleBar>

<!-- Strange Workaround -->
<Grid x:Name="TitleBarCoverageGrid" Grid.Row="0" Margin="0, 0, 300, 15" Background="{DynamicResource ApplicationBackgroundBrush}" Visibility="Collapsed" />


<!-- Strange Workaround -->
<Grid
Background="{DynamicResource ApplicationBackgroundBrush}"
Grid.Row="0"
Margin="0,0,300,15"
Visibility="{Binding ViewModel.IsTitleBarCoverageGridVisible, Converter={StaticResource InverseBoolToVisibility}}"
x:Name="TitleBarCoverageGrid" />

<tray:NotifyIcon
Focusable="False"
Grid.Row="0"
Expand Down

0 comments on commit ea26a5f

Please sign in to comment.