diff --git a/Azaion.Annotator/App.xaml.cs b/Azaion.Annotator/App.xaml.cs index bfac05a..f161719 100644 --- a/Azaion.Annotator/App.xaml.cs +++ b/Azaion.Annotator/App.xaml.cs @@ -8,6 +8,7 @@ using Azaion.Annotator.Extensions; using LibVLCSharp.Shared; using MediatR; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Serilog; using ILogger = Microsoft.Extensions.Logging.ILogger; @@ -17,54 +18,63 @@ namespace Azaion.Annotator; public partial class App : Application { - private readonly ServiceProvider _serviceProvider; + private readonly IHost _host; + private readonly ILogger _logger; private readonly IMediator _mediator; - + public App() { Log.Logger = new LoggerConfiguration() .Enrich.FromLogContext() - .MinimumLevel.Warning() + .MinimumLevel.Information() .WriteTo.Console() .WriteTo.File( path: "Logs/log.txt", rollingInterval: RollingInterval.Day) .CreateLogger(); - - var services = new ServiceCollection(); - services.AddSingleton(); - services.AddSingleton(); - services.AddMediatR(c => c.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly())); - services.AddSingleton(_ => new LibVLC()); - services.AddSingleton(); - services.AddSingleton(sp => - { - var libVLC = sp.GetRequiredService(); - return new MediaPlayer(libVLC); - }); - services.AddSingleton(_ => Config.Read()); - services.AddSingleton(); - _serviceProvider = services.BuildServiceProvider(); - _mediator = _serviceProvider.GetService()!; + + _host = Host.CreateDefaultBuilder() + .ConfigureServices((context, services) => + { + services.AddSingleton(); + services.AddSingleton(); + services.AddMediatR(c => c.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly())); + services.AddSingleton(_ => new LibVLC()); + services.AddSingleton(); + services.AddSingleton(sp => + { + var libVLC = sp.GetRequiredService(); + return new MediaPlayer(libVLC); + }); + services.AddSingleton(); + services.AddSingleton(sp => sp.GetRequiredService().Get()); + services.AddSingleton(); + }) + .UseSerilog() + .Build(); + _mediator = _host.Services.GetRequiredService(); + _logger = _host.Services.GetRequiredService>(); DispatcherUnhandledException += OnDispatcherUnhandledException; } private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) { - Log.Logger.Error(e.Exception, e.Exception.Message); + _logger.LogError(e.Exception, e.Exception.Message); e.Handled = true; } protected override void OnStartup(StartupEventArgs e) { EventManager.RegisterClassHandler(typeof(UIElement), UIElement.KeyDownEvent, new RoutedEventHandler(GlobalClick)); - _serviceProvider.GetRequiredService().Show(); + _host.Start(); + _host.Services.GetRequiredService().Show(); + + base.OnStartup(e); } private void GlobalClick(object sender, RoutedEventArgs e) { var args = (KeyEventArgs)e; - _mediator.Publish(new KeyEvent(sender, args)); } } \ No newline at end of file diff --git a/Azaion.Annotator/Azaion.Annotator.csproj b/Azaion.Annotator/Azaion.Annotator.csproj index 92c88fc..e590b0d 100644 --- a/Azaion.Annotator/Azaion.Annotator.csproj +++ b/Azaion.Annotator/Azaion.Annotator.csproj @@ -14,11 +14,14 @@ + + + diff --git a/Azaion.Annotator/DTO/Config.cs b/Azaion.Annotator/DTO/Config.cs index c4a9980..57263eb 100644 --- a/Azaion.Annotator/DTO/Config.cs +++ b/Azaion.Annotator/DTO/Config.cs @@ -1,6 +1,7 @@ using System.IO; using System.Reflection; using System.Text; +using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Size = System.Windows.Size; using Point = System.Windows.Point; @@ -9,22 +10,10 @@ namespace Azaion.Annotator.DTO; public class Config { - private const string CONFIG_PATH = "config.json"; - - private const string DEFAULT_VIDEO_DIR = "video"; - - private const string DEFAULT_LABELS_DIR = "labels"; - private const string DEFAULT_IMAGES_DIR = "images"; - private const string DEFAULT_RESULTS_DIR = "results"; - - private static readonly Size DefaultWindowSize = new(1280, 720); - private static readonly Point DefaultWindowLocation = new(100, 100); - - - public string VideosDirectory { get; set; } = DEFAULT_VIDEO_DIR; - public string LabelsDirectory { get; set; } = DEFAULT_LABELS_DIR; - public string ImagesDirectory { get; set; } = DEFAULT_IMAGES_DIR; - public string ResultsDirectory { get; set; } = DEFAULT_RESULTS_DIR; + public string VideosDirectory { get; set; } + public string LabelsDirectory { get; set; } + public string ImagesDirectory { get; set; } + public string ResultsDirectory { get; set; } public List AnnotationClasses { get; set; } = []; @@ -34,24 +23,44 @@ public class Config public Size WindowSize { get; set; } public Point WindowLocation { get; set; } public bool ShowHelpOnStart { get; set; } +} - public void Save() +public interface IConfigRepository +{ + public Config Get(); + public void Save(Config config); +} + +public class FileConfigRepository(ILogger logger) : IConfigRepository +{ + private const string CONFIG_PATH = "config.json"; + + private const string DEFAULT_VIDEO_DIR = "video"; + + private const string DEFAULT_LABELS_DIR = "labels"; + private const string DEFAULT_IMAGES_DIR = "images"; + private const string DEFAULT_RESULTS_DIR = "results"; + + private static readonly Size DefaultWindowSize = new(1280, 720); + private static readonly Point DefaultWindowLocation = new(100, 100); + + public Config Get() { - File.WriteAllText(CONFIG_PATH, JsonConvert.SerializeObject(this, Formatting.Indented), Encoding.UTF8); - } - - public static Config Read() - { - string configFilePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), CONFIG_PATH); + var exePath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)!; + var configFilePath = Path.Combine(exePath, CONFIG_PATH); + + logger.LogInformation($"exePath: {exePath}" ); + logger.LogInformation($"configFilePath: {configFilePath}"); if (!File.Exists(configFilePath)) { - var exePath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)!; return new Config { VideosDirectory = Path.Combine(exePath, DEFAULT_VIDEO_DIR), LabelsDirectory = Path.Combine(exePath, DEFAULT_LABELS_DIR), ImagesDirectory = Path.Combine(exePath, DEFAULT_IMAGES_DIR), + ResultsDirectory = Path.Combine(exePath, DEFAULT_RESULTS_DIR), + WindowLocation = DefaultWindowLocation, WindowSize = DefaultWindowSize, ShowHelpOnStart = true @@ -67,4 +76,9 @@ public class Config return new Config(); } } -} \ No newline at end of file + + public void Save(Config config) + { + File.WriteAllText(CONFIG_PATH, JsonConvert.SerializeObject(config, Formatting.Indented), Encoding.UTF8); + } +} diff --git a/Azaion.Annotator/MainWindow.xaml.cs b/Azaion.Annotator/MainWindow.xaml.cs index 4913018..2ca2c71 100644 --- a/Azaion.Annotator/MainWindow.xaml.cs +++ b/Azaion.Annotator/MainWindow.xaml.cs @@ -22,7 +22,7 @@ public partial class MainWindow private readonly IMediator _mediator; private readonly FormState _formState; - private readonly Config _config; + private readonly IConfigRepository _configRepository; private readonly HelpWindow _helpWindow; public ObservableCollection AnnotationClasses { get; set; } = new(); @@ -30,6 +30,7 @@ public partial class MainWindow private readonly TimeSpan _thresholdBefore = TimeSpan.FromMilliseconds(100); private readonly TimeSpan _thresholdAfter = TimeSpan.FromMilliseconds(300); + private readonly Config _config; public Dictionary> AnnotationsDict { get; set; } = new(); private IntervalTree> Annotations { get; set; } = new(); @@ -37,7 +38,7 @@ public partial class MainWindow public MainWindow(LibVLC libVLC, MediaPlayer mediaPlayer, IMediator mediator, FormState formState, - Config config, + IConfigRepository configRepository, HelpWindow helpWindow) { InitializeComponent(); @@ -45,7 +46,8 @@ public partial class MainWindow _mediaPlayer = mediaPlayer; _mediator = mediator; _formState = formState; - _config = config; + _configRepository = configRepository; + _config = _configRepository.Get(); _helpWindow = helpWindow; VideoView.Loaded += VideoView_Loaded; @@ -232,7 +234,7 @@ public partial class MainWindow _mediaPlayer.Dispose(); _libVLC.Dispose(); _config.AnnotationClasses = AnnotationClasses.ToList(); - _config.Save(); + _configRepository.Save(_config); Application.Current.Shutdown(); }