fix keyboard catch, fix loading

refactoring
This commit is contained in:
Alex Bezdieniezhnykh
2024-11-25 13:03:46 +02:00
parent b61bed3b51
commit 30b9b9aa80
18 changed files with 88 additions and 117 deletions
+37 -27
View File
@@ -1,7 +1,7 @@
using System.IO;
using System.Net.Http;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;
using Azaion.Annotator;
@@ -13,7 +13,6 @@ using Azaion.Common.DTO.Config;
using Azaion.Common.Extensions;
using Azaion.Common.Services;
using Azaion.Dataset;
using Azaion.Suite.Services.DTO;
using CommandLine;
using LibVLCSharp.Shared;
using MediatR;
@@ -33,24 +32,28 @@ public partial class App
private readonly ILogger<App> _logger;
private readonly IMediator _mediator;
private static readonly List<string> EncryptedResources =
[
"Azaion.Annotator.dll",
"Azaion.Dataset.dll"
];
private static readonly IResourceLoader? ResourceLoader = new ResourceLoader("", "", null!, null!);
//Authorize to api and
private static readonly IResourceLoader ResourceLoader;
static App()
{
//Load encrypted dlls
var result = Parser.Default.ParseArguments<SuiteCommandLineOptions>(Environment.GetCommandLineArgs());
new ConfigUpdater().CheckConfig();
var result = Parser.Default.ParseArguments<ApiCredentials>(Environment.GetCommandLineArgs());
if (result.Errors.Any())
return;
return;
var apiCreds = result.Value;
ResourceLoader = new ResourceLoader(CreateApiClient(apiCreds), apiCreds);
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => ResourceLoader.LoadAssembly(args.Name);
}
private static AzaionApiClient CreateApiClient(ApiCredentials credentials)
{
ApiConfig apiConfig;
try
{
if (File.Exists(Constants.CONFIG_PATH))
if (!File.Exists(Constants.CONFIG_PATH))
throw new FileNotFoundException(Constants.CONFIG_PATH);
var configStr = File.ReadAllText(Constants.CONFIG_PATH);
apiConfig = JsonConvert.DeserializeObject<AppConfig>(configStr)!.ApiConfig;
@@ -70,19 +73,9 @@ public partial class App
BaseAddress = new Uri(apiConfig.Url),
Timeout = TimeSpan.FromSeconds(apiConfig.TimeoutSeconds)
});
var email = result.Value.Email;
var password = result.Value.Password;
api.Login(email, password);
ResourceLoader = new ResourceLoader(email, password, api, new HardwareService());
foreach (var resource in EncryptedResources)
{
var stream = ResourceLoader.Load(resource).GetAwaiter().GetResult();
Assembly.Load(stream.ToArray());
}
new ConfigUpdater().CheckConfig();
api.EnterCredentials(credentials);
return api;
}
public App()
@@ -104,7 +97,7 @@ public partial class App
{
services.AddSingleton<MainSuite>();
services.AddSingleton<IHardwareService, HardwareService>();
services.AddSingleton<IResourceLoader>(ResourceLoader!);
services.AddSingleton(ResourceLoader);
services.Configure<AppConfig>(context.Configuration);
services.ConfigureSection<ApiConfig>(context.Configuration);
@@ -170,10 +163,27 @@ public partial class App
private void GlobalClick(object sender, RoutedEventArgs e)
{
var args = (KeyEventArgs)e;
var keyEvent = new KeyEvent(sender, args, (sender as FrameworkElement).GetParentWindow());
var windowEnum = GetParentWindow(sender as FrameworkElement);
if (windowEnum == WindowEnum.None)
return;
var keyEvent = new KeyEvent(sender, args, windowEnum);
_ = ThrottleExt.Throttle(() => _mediator.Publish(keyEvent), TimeSpan.FromMilliseconds(50));
}
private WindowEnum GetParentWindow(FrameworkElement? element)
{
while (element != null && element is not TabItem)
{
element = element.Parent as FrameworkElement;
}
if (element is not TabItem || element.Tag == null)
return WindowEnum.None;
return (WindowEnum)element.Tag;
}
protected override async void OnExit(ExitEventArgs e)
{
base.OnExit(e);