Skip to content

Commit

Permalink
[Release 0.1.2] Update
Browse files Browse the repository at this point in the history
[Release 0.1.2] Update
  • Loading branch information
AuroraZiling authored Oct 26, 2024
2 parents a9e82db + d07a5ee commit c38219f
Show file tree
Hide file tree
Showing 46 changed files with 699 additions and 656 deletions.
125 changes: 11 additions & 114 deletions src/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,16 @@
using Microsoft.Extensions.Logging;
using Serilog;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Runtime.InteropServices;
using Windows.Win32;
using System.Windows.Threading;
using PipManager.Core.Services.PackageSearchService;
using PipManager.Core.Services;
using PipManager.Windows.Extensions;
using PipManager.Windows.Services;
using PipManager.Windows.Services.Action;
using PipManager.Windows.Services.Environment;
using PipManager.Windows.Services.Mask;
using PipManager.Windows.Services.Overlay;
using PipManager.Windows.Services.Toast;
using PipManager.Windows.ViewModels.Pages.Action;
using PipManager.Windows.ViewModels.Pages.Environment;
using PipManager.Windows.ViewModels.Pages.Lab;
using PipManager.Windows.ViewModels.Pages.Library;
using PipManager.Windows.ViewModels.Pages.Overlay;
using PipManager.Windows.ViewModels.Pages.Search;
using PipManager.Windows.ViewModels.Pages.Tools;
using PipManager.Windows.ViewModels.Windows;
using PipManager.Windows.Views.Pages.About;
using PipManager.Windows.Views.Pages.Action;
using PipManager.Windows.Views.Pages.Environment;
using PipManager.Windows.Views.Pages.Lab;
using PipManager.Windows.Views.Pages.Library;
using PipManager.Windows.Views.Pages.Overlay;
using PipManager.Windows.Views.Pages.Search;
using PipManager.Windows.Views.Pages.Settings;
using PipManager.Windows.Views.Pages.Tools;
using PipManager.Windows.Views.Windows;
using Wpf.Ui;

namespace PipManager.Windows;

using AboutViewModel = ViewModels.Pages.About.AboutViewModel;
using SettingsViewModel = ViewModels.Pages.Settings.SettingsViewModel;

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App
{
private static readonly IHost Host = Microsoft.Extensions.Hosting.Host
Expand All @@ -57,90 +28,25 @@ public partial class App
{
services.AddHostedService<ApplicationHostService>();

services.AddTransient(_ =>
{
var client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.All }) { DefaultRequestVersion = HttpVersion.Version20 };
client.DefaultRequestHeaders.Add("User-Agent", $"PipManager.Windows/{AppInfo.AppVersion}");
client.Timeout = TimeSpan.FromSeconds(6);
return client;
});
services.AddHttpClient(AppInfo.AppVersion);

// Window
services.AddSingleton<MainWindow>();
services.AddSingleton<MainWindowViewModel>();

// Services
services.AddSingleton<INavigationService, NavigationService>();
services.AddSingleton<ISnackbarService, SnackbarService>();
services.AddSingleton<IMaskService, MaskService>();
services.AddSingleton<IToastService, ToastService>();
services.AddSingleton<IEnvironmentService, EnvironmentService>();
services.AddSingleton<IActionService, ActionService>();
services.AddSingleton<IThemeService, ThemeService>();
services.AddSingleton<IContentDialogService, ContentDialogService>();
services.AddSingleton<ITaskBarService, TaskBarService>();
services.AddSingleton<IPackageSearchService, PackageSearchService>();
services.AddSingleton<IOverlayService, OverlayService>();

// Pages
services.AddSingleton<LibraryPage>();
services.AddSingleton<LibraryViewModel>();
services.AddSingleton<LibraryDetailPage>();
services.AddSingleton<LibraryDetailViewModel>();
services.AddSingleton<LibraryInstallPage>();
services.AddSingleton<LibraryInstallViewModel>();

services.AddSingleton<OverlayPage>();
services.AddSingleton<OverlayViewModel>();

services.AddSingleton<ActionPage>();
services.AddSingleton<ActionViewModel>();
services.AddSingleton<ActionExceptionPage>();
services.AddSingleton<ActionExceptionViewModel>();

services.AddSingleton<SearchPage>();
services.AddSingleton<SearchViewModel>();
services.AddSingleton<SearchDetailPage>();
services.AddSingleton<SearchDetailViewModel>();

services.AddSingleton<ToolsPage>();
services.AddSingleton<ToolsViewModel>();

services.AddSingleton<LabPage>();
services.AddSingleton<LabViewModel>();

services.AddSingleton<EnvironmentPage>();
services.AddSingleton<EnvironmentViewModel>();
services.AddSingleton<AddEnvironmentPage>();
services.AddSingleton<AddEnvironmentViewModel>();

services.AddSingleton<SettingsPage>();
services.AddSingleton<SettingsViewModel>();
services.AddSingleton<AboutPage>();
services.AddSingleton<AboutViewModel>();
services.RegisterServices();
services.RegisterViews();
services.RegisterViewModels();
}).Build();

/// <summary>
/// Gets registered service.
/// </summary>
/// <typeparam name="T">Type of the service to get.</typeparam>
/// <returns>Instance of the service or <see langword="null"/>.</returns>
public static T GetService<T>()
where T : class
{
return Host.Services.GetService(typeof(T)) as T ?? throw new InvalidOperationException("Service not found.");
}

[LibraryImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial void FreeConsole();

private bool _showConsoleWindow;
public static bool IsDebugMode { get; private set; }

/// <summary>
/// Occurs when the application is loading.
/// </summary>
private void OnStartup(object sender, StartupEventArgs e)
{
Environment.SetEnvironmentVariable("WEBVIEW2_USER_DATA_FOLDER", AppInfo.CachesDir);
Expand All @@ -154,35 +60,26 @@ private void OnStartup(object sender, StartupEventArgs e)
break;
}
}
IsDebugMode = _showConsoleWindow;
var appStarting = new AppStarting
{
ShowConsoleWindow = _showConsoleWindow
};
appStarting.StartLogging();
appStarting.LoadLanguage();
appStarting.CachesDeletion();
AppStarting.LoadLanguage();
AppStarting.CachesDeletion();
Host.Start();
IsDebugMode = _showConsoleWindow;
}

/// <summary>
/// Occurs when the application is closing.
/// </summary>
private async void OnExit(object sender, ExitEventArgs e)
{
if (_showConsoleWindow)
{
FreeConsole();
}
if (_showConsoleWindow) PInvoke.FreeConsole();
await Host.StopAsync();
Host.Dispose();
Log.Information("Logging ended");
await Log.CloseAndFlushAsync();
}

/// <summary>
/// Occurs when an exception is thrown by an application but not handled.
/// </summary>
private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
Log.Error($"Exception: {e.Exception}");
Expand Down
14 changes: 5 additions & 9 deletions src/AppStarting.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
using Serilog;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using Windows.Win32;
using PipManager.Core.Configuration;

namespace PipManager.Windows;

public partial class AppStarting
public class AppStarting
{
[LibraryImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial void AllocConsole();

public bool ShowConsoleWindow = false;

public AppStarting()
Expand All @@ -22,7 +18,7 @@ public AppStarting()
Directory.CreateDirectory(AppInfo.CachesDir);
}

public void LoadLanguage()
public static void LoadLanguage()
{
var language = Configuration.AppConfig!.Personalization.Language;
if (language != "Auto")
Expand All @@ -36,7 +32,7 @@ public void StartLogging()
{
if (ShowConsoleWindow)
{
AllocConsole();
PInvoke.AllocConsole();
}
Log.Logger = new LoggerConfiguration()
.Enrich.WithProperty("Version", AppInfo.AppVersion)
Expand All @@ -47,7 +43,7 @@ public void StartLogging()
Log.Information("Logging started");
}

public void CachesDeletion()
public static void CachesDeletion()
{
if (!Directory.Exists(AppInfo.CachesDir)) return;
var directoryInfo = new DirectoryInfo(AppInfo.CachesDir);
Expand Down
5 changes: 1 addition & 4 deletions src/Controls/Toast.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
Margin="1"
VerticalAlignment="Stretch">

<Grid
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
x:Name="Grid">
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">

<StackPanel
HorizontalAlignment="Left"
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/Toast.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private static void ShowToast(Toast toast, int time)
{
PopupAnimation = PopupAnimation.Fade,
AllowsTransparency = true,
StaysOpen = true,
StaysOpen = false,
Placement = PlacementMode.Top,
IsOpen = false,
Child = toast,
Expand Down
1 change: 0 additions & 1 deletion src/Converters/PackageSourceEnumToBooleanConverter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Globalization;
using System.Windows.Data;
using PipManager.Windows.Models.Package;

namespace PipManager.Windows.Converters;

Expand Down
85 changes: 85 additions & 0 deletions src/Extensions/ServiceRegisterExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using Microsoft.Extensions.DependencyInjection;
using PipManager.Core.Services.PackageSearchService;
using PipManager.Windows.Services.Action;
using PipManager.Windows.Services.Environment;
using PipManager.Windows.Services.Mask;
using PipManager.Windows.Services.Overlay;
using PipManager.Windows.Services.Page;
using PipManager.Windows.Services.Toast;
using PipManager.Windows.ViewModels.Pages.About;
using PipManager.Windows.ViewModels.Pages.Action;
using PipManager.Windows.ViewModels.Pages.Environment;
using PipManager.Windows.ViewModels.Pages.Lab;
using PipManager.Windows.ViewModels.Pages.Library;
using PipManager.Windows.ViewModels.Pages.Overlay;
using PipManager.Windows.ViewModels.Pages.Search;
using PipManager.Windows.ViewModels.Pages.Settings;
using PipManager.Windows.ViewModels.Pages.Tools;
using PipManager.Windows.Views.Pages.About;
using PipManager.Windows.Views.Pages.Action;
using PipManager.Windows.Views.Pages.Environment;
using PipManager.Windows.Views.Pages.Lab;
using PipManager.Windows.Views.Pages.Library;
using PipManager.Windows.Views.Pages.Overlay;
using PipManager.Windows.Views.Pages.Search;
using PipManager.Windows.Views.Pages.Settings;
using PipManager.Windows.Views.Pages.Tools;
using Wpf.Ui;
using Wpf.Ui.Abstractions;

namespace PipManager.Windows.Extensions;

public static class ServiceRegisterExtensions
{
public static void RegisterServices(this IServiceCollection services)
{
services.AddSingleton<INavigationViewPageProvider, PageService>();
services.AddSingleton<INavigationService, NavigationService>();
services.AddSingleton<ISnackbarService, SnackbarService>();
services.AddSingleton<IMaskService, MaskService>();
services.AddSingleton<IToastService, ToastService>();
services.AddSingleton<IEnvironmentService, EnvironmentService>();
services.AddSingleton<IActionService, ActionService>();
services.AddSingleton<IThemeService, ThemeService>();
services.AddSingleton<IContentDialogService, ContentDialogService>();
services.AddSingleton<ITaskBarService, TaskBarService>();
services.AddSingleton<IPackageSearchService, PackageSearchService>();
services.AddSingleton<IOverlayService, OverlayService>();
}

public static void RegisterViews(this IServiceCollection services)
{
services.AddSingleton<LibraryPage>();
services.AddSingleton<LibraryDetailPage>();
services.AddSingleton<LibraryInstallPage>();
services.AddSingleton<OverlayPage>();
services.AddSingleton<ActionPage>();
services.AddSingleton<ActionExceptionPage>();
services.AddSingleton<SearchPage>();
services.AddSingleton<SearchDetailPage>();
services.AddSingleton<ToolsPage>();
services.AddSingleton<LabPage>();
services.AddSingleton<EnvironmentPage>();
services.AddSingleton<AddEnvironmentPage>();
services.AddSingleton<SettingsPage>();
services.AddSingleton<AboutPage>();
}

public static void RegisterViewModels(this IServiceCollection services)
{
services.AddSingleton<LibraryViewModel>();
services.AddSingleton<LibraryDetailViewModel>();
services.AddSingleton<LibraryInstallViewModel>();
services.AddSingleton<OverlayViewModel>();
services.AddSingleton<ActionViewModel>();
services.AddSingleton<ActionExceptionViewModel>();
services.AddSingleton<SearchViewModel>();
services.AddSingleton<SearchDetailViewModel>();
services.AddSingleton<ToolsViewModel>();
services.AddSingleton<LabViewModel>();
services.AddSingleton<EnvironmentViewModel>();
services.AddSingleton<AddEnvironmentViewModel>();
services.AddSingleton<SettingsViewModel>();
services.AddSingleton<AboutViewModel>();
}
}
6 changes: 3 additions & 3 deletions src/Models/Action/ActionListItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public ActionListItem(ActionType operationType, string[] operationCommand, strin
DisplayCommand = displayCommand switch
{
"" => string.Join(' ', operationCommand),
_ => displayCommand,
_ => displayCommand
};
OperationDescription = operationType switch
{
Expand All @@ -25,7 +25,7 @@ public ActionListItem(ActionType operationType, string[] operationCommand, strin
ActionType.InstallByRequirements => Lang.Action_Operation_InstallByRequirements,
ActionType.Download => Lang.Action_Operation_Download,
ActionType.Update => Lang.Action_Operation_Update,
_ => "Unknown",
_ => "Unknown"
};

OperationIcon = operationType switch
Expand All @@ -41,7 +41,7 @@ public ActionListItem(ActionType operationType, string[] operationCommand, strin
ActionType.Uninstall => "Danger",
ActionType.Install or ActionType.InstallByRequirements => "Success",
ActionType.Update or ActionType.Download => "Caution",
_ => "Primary",
_ => "Primary"
};
ConsoleOutput = "";
ConsoleError = Lang.Action_ConsoleError_Empty;
Expand Down
2 changes: 2 additions & 0 deletions src/NativeMethods.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
AllocConsole
FreeConsole
Loading

0 comments on commit c38219f

Please sign in to comment.