Финализация рефакторинга
This commit is contained in:
@@ -1,216 +0,0 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Windows;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Pf2e.Database;
|
||||
using Pf2e.Scrapper.Helpers;
|
||||
using Pf2e.Scrapper.Processors;
|
||||
using Pf2e.Scrapper.Processors.Collection;
|
||||
using Slugify;
|
||||
|
||||
namespace Pf2e.Scrapper;
|
||||
|
||||
public sealed class ImportApplication : Application
|
||||
{
|
||||
public ImportApplication(
|
||||
IServiceProvider serviceProvider,
|
||||
ApplicationSettings settings,
|
||||
ProcessorCollection processors
|
||||
)
|
||||
{
|
||||
ServiceProvider = serviceProvider;
|
||||
Settings = settings;
|
||||
Processors = processors;
|
||||
|
||||
CurrentStage = ApplicationStage.InitialSettings;
|
||||
//CurrentStage = ApplicationStage.Viewer;
|
||||
}
|
||||
|
||||
#region "Поля"
|
||||
|
||||
/// <summary>
|
||||
/// Текущий этап выполнения программы.
|
||||
/// </summary>
|
||||
public ApplicationStage CurrentStage
|
||||
{
|
||||
get => _currentStage;
|
||||
set
|
||||
{
|
||||
ApplicationStage oldValue = _currentStage;
|
||||
_currentStage = value;
|
||||
|
||||
CloseStage(oldValue);
|
||||
StartStage(_currentStage);
|
||||
}
|
||||
}
|
||||
private ApplicationStage _currentStage = ApplicationStage.BootUp;
|
||||
|
||||
#endregion
|
||||
|
||||
#region "Жизненный цикл"
|
||||
|
||||
private StartForm? _startFormWpf;
|
||||
|
||||
private ProgressForm? _progressFormWpf;
|
||||
|
||||
private EntityForm? _entityForm;
|
||||
|
||||
private Window? CurrentWpfForm { get; set; }
|
||||
|
||||
public delegate void ApplicationStateChangeDelegate(object sender, ApplicationStage nextStage);
|
||||
|
||||
private void StartStage(ApplicationStage stage)
|
||||
{
|
||||
switch (stage)
|
||||
{
|
||||
case ApplicationStage.BootUp:
|
||||
throw new(
|
||||
$"{CurrentStage} никогда не должен быть установлен через поле \"CurrentStage\"."
|
||||
);
|
||||
|
||||
case ApplicationStage.InitialSettings:
|
||||
_startFormWpf = ActivatorUtilities.CreateInstance<StartForm>(ServiceProvider);
|
||||
_startFormWpf.WindowStartupLocation = WindowStartupLocation.CenterScreen;
|
||||
_startFormWpf.Closing += Wpf_HandleFormClosing;
|
||||
_startFormWpf.ApplicationStateChange += ApplicationStateChange;
|
||||
_startFormWpf.Show();
|
||||
|
||||
CurrentWpfForm = _startFormWpf;
|
||||
break;
|
||||
|
||||
case ApplicationStage.ImportProcess:
|
||||
ProcessorExecutor executor = ActivatorUtilities.CreateInstance<ProcessorExecutor>(
|
||||
ServiceProvider
|
||||
);
|
||||
|
||||
_progressFormWpf = ActivatorUtilities.CreateInstance<ProgressForm>(ServiceProvider);
|
||||
_progressFormWpf.WindowStartupLocation = WindowStartupLocation.CenterScreen;
|
||||
_progressFormWpf.Closing += Wpf_HandleFormClosing;
|
||||
_progressFormWpf.ApplicationStateChange += ApplicationStateChange;
|
||||
_progressFormWpf.Show();
|
||||
|
||||
CurrentWpfForm = _progressFormWpf;
|
||||
|
||||
executor.Start();
|
||||
break;
|
||||
|
||||
case ApplicationStage.Viewer:
|
||||
_entityForm = ActivatorUtilities.CreateInstance<EntityForm>(ServiceProvider);
|
||||
_entityForm.WindowStartupLocation = WindowStartupLocation.CenterScreen;
|
||||
_entityForm.Closing += Wpf_HandleFormClosing;
|
||||
_entityForm.ApplicationStateChange += ApplicationStateChange;
|
||||
_entityForm.Show();
|
||||
|
||||
CurrentWpfForm = _entityForm;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
private void CloseStage(ApplicationStage stage)
|
||||
{
|
||||
CurrentWpfForm = null;
|
||||
|
||||
switch (stage)
|
||||
{
|
||||
case ApplicationStage.BootUp:
|
||||
// Нечего закрывать
|
||||
break;
|
||||
|
||||
case ApplicationStage.InitialSettings:
|
||||
_startFormWpf!.Hide();
|
||||
_startFormWpf!.Close();
|
||||
_startFormWpf = null;
|
||||
break;
|
||||
|
||||
case ApplicationStage.ImportProcess:
|
||||
_progressFormWpf!.Hide();
|
||||
_progressFormWpf!.Close();
|
||||
_progressFormWpf = null;
|
||||
break;
|
||||
|
||||
case ApplicationStage.Viewer:
|
||||
_entityForm!.Hide();
|
||||
_entityForm!.Close();
|
||||
_entityForm = null;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(stage));
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region "События"
|
||||
|
||||
public void ApplicationStateChange(object sender, ApplicationStage nextStage)
|
||||
{
|
||||
if (sender?.Equals(CurrentWpfForm) ?? false)
|
||||
{
|
||||
CurrentStage = nextStage;
|
||||
}
|
||||
}
|
||||
|
||||
private void Wpf_HandleFormClosing(object? sender, CancelEventArgs e)
|
||||
{
|
||||
if (sender?.Equals(CurrentWpfForm) ?? false)
|
||||
{
|
||||
Shutdown();
|
||||
}
|
||||
else
|
||||
{
|
||||
e.Cancel = true;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region "Внедрение зависимостей"
|
||||
|
||||
public IServiceProvider ServiceProvider { get; }
|
||||
public ApplicationSettings Settings { get; }
|
||||
public ProcessorCollection Processors { get; }
|
||||
|
||||
public static void RegisterServices(IServiceCollection services)
|
||||
{
|
||||
// Контекст базы данных
|
||||
services.AddEntityFrameworkNpgsql();
|
||||
|
||||
services.AddDbContext<Pf2eDbContext>();
|
||||
//services.AddDbContextPool<Pf2eDbContext>(options =>
|
||||
//{
|
||||
// options.Us
|
||||
//});
|
||||
|
||||
// Основная программа
|
||||
services.AddSingleton<ImportApplication>();
|
||||
|
||||
// Настройки программы
|
||||
services.AddSingleton<ApplicationSettings>();
|
||||
|
||||
// Набор процессоров
|
||||
services.AddSingleton<ProcessorCollection>();
|
||||
|
||||
// Помощник идентификации базы данных
|
||||
services.AddSingleton<DatabaseHelper>();
|
||||
|
||||
// Помощник создание URL-совместимых названий
|
||||
services.AddTransient<SlugHelper>();
|
||||
|
||||
// Счётчик прогресса
|
||||
services.AddSingleton<ProcessProgress>();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
public enum ApplicationStage
|
||||
{
|
||||
BootUp,
|
||||
InitialSettings,
|
||||
ImportProcess,
|
||||
Viewer,
|
||||
Rules,
|
||||
}
|
||||
@@ -108,7 +108,7 @@ public class RuleTocCollection
|
||||
/// <summary>
|
||||
/// Класс представляет собой книгу, содержащую правила.
|
||||
/// </summary>
|
||||
internal class RuleTocBook(Source source)
|
||||
public class RuleTocBook(Source source)
|
||||
{
|
||||
/// <summary>
|
||||
/// Идентификатор источника.
|
||||
@@ -222,7 +222,7 @@ internal class RuleTocBook(Source source)
|
||||
/// <summary>
|
||||
/// Класс представляет собой элемент дерева правил.
|
||||
/// </summary>
|
||||
internal class RuleTocItem(string name, RuleTocItem? parent)
|
||||
public class RuleTocItem(string name, RuleTocItem? parent)
|
||||
{
|
||||
public string? Id { get; set; } = null;
|
||||
|
||||
|
||||
+1
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Pf2e.Common;
|
||||
using Pf2e.Database;
|
||||
using Pf2e.Database.Model;
|
||||
using Pf2e.Database.Model.Base;
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Pf2e.Scrapper.Importers.Implementations.Aonprd;
|
||||
using Pf2e.Scrapper.Importers.Implementations.FoundryEn;
|
||||
using Pf2e.Scrapper.WPF;
|
||||
|
||||
namespace Pf2e.Scrapper;
|
||||
|
||||
public static class Program
|
||||
{
|
||||
/// <summary>
|
||||
/// The main entry point for the application.
|
||||
/// </summary>
|
||||
[STAThread]
|
||||
public static void Main()
|
||||
{
|
||||
// To customize application configuration such as set high DPI settings or default font,
|
||||
// see https://aka.ms/applicationconfiguration.
|
||||
ApplicationConfiguration.Initialize();
|
||||
|
||||
// Application.SetHighDpiMode(HighDpiMode.SystemAware);
|
||||
/*
|
||||
#pragma warning disable WFO5001
|
||||
Application.SetColorMode(SystemColorMode.System);
|
||||
#pragma warning restore WFO5001
|
||||
*/
|
||||
IServiceCollection services = RegisterServices();
|
||||
|
||||
IServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||
|
||||
ImportApplication app = ActivatorUtilities.CreateInstance<ImportApplication>(
|
||||
serviceProvider
|
||||
);
|
||||
|
||||
app.Run();
|
||||
// Application.Run(app);
|
||||
}
|
||||
|
||||
private static IServiceCollection RegisterServices()
|
||||
{
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
|
||||
services.RegisterWpfServices();
|
||||
|
||||
ImportApplication.RegisterServices(services);
|
||||
|
||||
AonprdImporter.RegisterServices(services);
|
||||
FoundryEnImporter.RegisterServices(services);
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user