diff --git a/.github/workflows/push_master.yml b/.github/workflows/push_master.yml index 4a4dfd5..accf086 100644 --- a/.github/workflows/push_master.yml +++ b/.github/workflows/push_master.yml @@ -57,14 +57,10 @@ jobs: uses: actions/setup-dotnet@v3 with: dotnet-version: 8.0.x - - name: Setup msbuild - uses: microsoft/setup-msbuild@v2 - - name: Deploy application - shell: cmd - run: | - "%MSBUILD_PATH%" VDownload.sln /Deploy Release + - name: Build application + run: dotnet build -o build - name: Upload artifact uses: actions/upload-artifact@v2 with: name: build - path: bin + path: build diff --git a/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.DictionaryResources/DictionaryResourcesService.cs b/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.DictionaryResources/DictionaryResourcesService.cs index 5c97eef..fd8818e 100644 --- a/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.DictionaryResources/DictionaryResourcesService.cs +++ b/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.DictionaryResources/DictionaryResourcesService.cs @@ -9,6 +9,7 @@ namespace VDownload.Services.UI.DictionaryResources { public interface IDictionaryResourcesService { + ResourceDictionary Resources { get; set; } T Get(string key); } @@ -16,6 +17,14 @@ public interface IDictionaryResourcesService public class DictionaryResourcesService : IDictionaryResourcesService { + #region PROPERTIES + + public ResourceDictionary Resources { get; set; } + + #endregion + + + #region CONSTRUCTORS public DictionaryResourcesService() { } @@ -28,7 +37,7 @@ public DictionaryResourcesService() { } public T Get(string key) { - Application.Current.Resources.TryGetValue(key, out object value); + Resources.TryGetValue(key, out object value); if (value is not null && value is T cast) { return cast; diff --git a/VDownload/App.xaml.cs b/VDownload/App.xaml.cs index fc2d7c1..c390645 100644 --- a/VDownload/App.xaml.cs +++ b/VDownload/App.xaml.cs @@ -1,10 +1,13 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration.Json; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using Microsoft.Toolkit.Uwp.Notifications; +using Microsoft.UI; using Microsoft.UI.Xaml; using Microsoft.Windows.AppNotifications; using System; +using System.Net; using System.Net.Http; using System.Threading.Tasks; using VDownload.Core.Tasks; @@ -47,11 +50,21 @@ namespace VDownload { public partial class App : Application { - #region FIELDS + #region PROPERTIES - protected IServiceProvider _serviceProvider; + public static BaseWindow Window { get; protected set; } - protected BaseWindow _window; + public static T GetService() where T : class + { + if ((App.Current as App)!.Host.Services.GetService(typeof(T)) is not T service) + { + throw new ArgumentException($"{typeof(T)} needs to be registered in ConfigureServices within App.xaml.cs."); + } + + return service; + } + + public IHost Host { get; set; } #endregion @@ -63,22 +76,70 @@ public App() { this.InitializeComponent(); + Host = Microsoft.Extensions.Hosting.Host + .CreateDefaultBuilder() + .UseContentRoot(AppContext.BaseDirectory) + .ConfigureAppConfiguration((builder) => + { + builder.Sources.Add(new JsonConfigurationSource + { + Path = "configuration.json" + }); + }) + .ConfigureServices((context, services) => + { + BuildCore(services); + + BuildDataServices(services); + BuildUIServices(services); + BuildUtilityServices(services); + BuildSourcesServices(services); + + BuildTasksManager(services); + BuildPresentation(services); + }) + .Build(); + } + + #endregion - IServiceCollection services = new ServiceCollection(); - BuildCore(services); - BuildConfiguration(services); + #region EVENT HANDLERS - BuildDataServices(services); - BuildUIServices(services); - BuildUtilityServices(services); - BuildSourcesServices(services); + private void WindowRootLoaded(object sender, EventArgs e) + { + GetService().DefaultRoot = Window.XamlRoot; + } - BuildTasksManager(services); - BuildPresentation(services); + protected override async void OnLaunched(LaunchActivatedEventArgs args) + { + base.OnLaunched(args); + + GetService().Resources = (App.Current as App).Resources; + + Window = GetService(); + Window.RootLoaded += WindowRootLoaded; + + IApplicationDataService applicationDataService = GetService(); + ISettingsService settingsService = GetService(); + IAuthenticationDataService authenticationDataService = GetService(); + ISubscriptionsDataService subscriptionsDataService = GetService(); + Task initViewModelToViewConverterTask = Task.Run(() => ViewModelToViewConverter.ServiceProvider = (App.Current as App)!.Host.Services); + Task initStoragePickerServiceTask = Task.Run(() => GetService().DefaultRoot = Window); + Task initNotificationsServiceTask = Task.Run(() => GetService().Initialize(() => WindowHelper.ShowWindow(Window))); + + await Task.WhenAll( + applicationDataService.Load(), + settingsService.Load(), + authenticationDataService.Load(), + subscriptionsDataService.Load(), + initStoragePickerServiceTask, + initViewModelToViewConverterTask, + initNotificationsServiceTask + ); - _serviceProvider = services.BuildServiceProvider(); + Window.Activate(); } #endregion @@ -92,22 +153,6 @@ protected void BuildCore(IServiceCollection services) services.AddSingleton(); } - protected void BuildConfiguration(IServiceCollection services) - { - IConfigurationBuilder configBuilder = new ConfigurationBuilder - { - Sources = - { - new JsonConfigurationSource - { - Path = "configuration.json" - } - } - }; - IConfiguration config = configBuilder.Build(); - services.AddSingleton(config); - } - protected void BuildDataServices(IServiceCollection services) { services.AddSingleton(); @@ -178,42 +223,6 @@ protected void BuildPresentation(IServiceCollection services) services.AddTransient(); } - protected async Task InitializeServices() - { - IApplicationDataService applicationDataService = _serviceProvider.GetService(); - ISettingsService settingsService = _serviceProvider.GetService(); - IAuthenticationDataService authenticationDataService = _serviceProvider.GetService(); - ISubscriptionsDataService subscriptionsDataService = _serviceProvider.GetService(); - Task initViewModelToViewConverterTask = Task.Run(() => ViewModelToViewConverter.ServiceProvider = _serviceProvider); - Task initStoragePickerServiceTask = Task.Run(() => _serviceProvider.GetService().DefaultRoot = _window); - Task initNotificationsServiceTask = Task.Run(() => _serviceProvider.GetService().Initialize(() => WindowHelper.ShowWindow(_window))); - - await Task.WhenAll( - applicationDataService.Load(), - settingsService.Load(), - authenticationDataService.Load(), - subscriptionsDataService.Load(), - initStoragePickerServiceTask, - initViewModelToViewConverterTask, - initNotificationsServiceTask - ); - } - - protected override async void OnLaunched(LaunchActivatedEventArgs args) - { - _window = _serviceProvider.GetService(); - _window.RootLoaded += Window_RootLoaded; - _window.Activate(); - - await InitializeServices(); - } - - protected void Window_RootLoaded(object sender, EventArgs e) - { - IDialogsService dialogsService = _serviceProvider.GetService(); - dialogsService.DefaultRoot = _window.XamlRoot; - } - #endregion } } diff --git a/VDownload/VDownload.csproj b/VDownload/VDownload.csproj index 58e0110..63d4fc2 100644 --- a/VDownload/VDownload.csproj +++ b/VDownload/VDownload.csproj @@ -168,6 +168,7 @@ +