- shit: Cleanup this mess

This commit is contained in:
2025-01-14 22:10:16 +03:00
parent a0b2cfd677
commit 08e518602b
71 changed files with 1022 additions and 943 deletions

View File

@@ -6,10 +6,7 @@ using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Threading;
using Microsoft.Extensions.DependencyInjection;
using Nebula.Launcher.ViewHelper;
using Nebula.Launcher.ViewModels;
using Nebula.Launcher.Views;
using Nebula.Launcher.Views.Pages;
namespace Nebula.Launcher;
@@ -18,14 +15,20 @@ public static class ServiceCollectionExtensions
public static void AddAvaloniaServices(this IServiceCollection services)
{
services.AddSingleton<IDispatcher>(_ => Dispatcher.UIThread);
services.AddSingleton(_ => Application.Current?.ApplicationLifetime ?? throw new InvalidOperationException("No application lifetime is set"));
services.AddSingleton(_ =>
Application.Current?.ApplicationLifetime ??
throw new InvalidOperationException("No application lifetime is set"));
services.AddSingleton(sp =>
sp.GetRequiredService<IApplicationLifetime>() switch
{
IClassicDesktopStyleApplicationLifetime desktop => desktop.MainWindow ?? throw new InvalidOperationException("No main window set"),
ISingleViewApplicationLifetime singleViewPlatform => TopLevel.GetTopLevel(singleViewPlatform.MainView) ?? throw new InvalidOperationException("Could not find top level element for single view"),
_ => throw new InvalidOperationException($"Could not find {nameof(TopLevel)} element"),
IClassicDesktopStyleApplicationLifetime desktop => desktop.MainWindow ??
throw new InvalidOperationException(
"No main window set"),
ISingleViewApplicationLifetime singleViewPlatform =>
TopLevel.GetTopLevel(singleViewPlatform.MainView) ??
throw new InvalidOperationException("Could not find top level element for single view"),
_ => throw new InvalidOperationException($"Could not find {nameof(TopLevel)} element")
}
);
@@ -38,20 +41,18 @@ public static class ServiceCollectionExtensions
foreach (var (viewModel, view, isSingleton) in GetTypesWithHelpAttribute(Assembly.GetExecutingAssembly()))
{
if(isSingleton)services.AddSingleton(viewModel);
if (isSingleton) services.AddSingleton(viewModel);
else services.AddTransient(viewModel);
if (view != null) services.AddTransient(view);
}
}
private static IEnumerable<(Type,Type?,bool)> GetTypesWithHelpAttribute(Assembly assembly) {
foreach(Type type in assembly.GetTypes())
private static IEnumerable<(Type, Type?, bool)> GetTypesWithHelpAttribute(Assembly assembly)
{
foreach (var type in assembly.GetTypes())
{
var attr = type.GetCustomAttribute<ViewModelRegisterAttribute>();
if (attr is not null) {
yield return (type, attr.Type, attr.IsSingleton);
}
if (attr is not null) yield return (type, attr.Type, attr.IsSingleton);
}
}
}
}