using System.IO; using System.Reflection; using System.Windows; using System.Windows.Input; using System.Windows.Threading; using Azaion.Annotator.DTO; using Azaion.Annotator.Extensions; using LibVLCSharp.Shared; using MediatR; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Serilog; using ILogger = Microsoft.Extensions.Logging.ILogger; using LogLevel = Microsoft.Extensions.Logging.LogLevel; namespace Azaion.Annotator; public partial class App : Application { private readonly ServiceProvider _serviceProvider; private readonly IMediator _mediator; public App() { Log.Logger = new LoggerConfiguration() .Enrich.FromLogContext() .MinimumLevel.Warning() .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()!; DispatcherUnhandledException += OnDispatcherUnhandledException; } private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) { Log.Logger.Error(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(); } private void GlobalClick(object sender, RoutedEventArgs e) { var args = (KeyEventArgs)e; _mediator.Publish(new KeyEvent(sender, args)); } }