diff --git a/VDownload.Core/VDownload.Core.Views/ViewModelToViewConverter.cs b/VDownload.Core/VDownload.Core.Views/ViewModelToViewConverter.cs index bcf00dd..79b497a 100644 --- a/VDownload.Core/VDownload.Core.Views/ViewModelToViewConverter.cs +++ b/VDownload.Core/VDownload.Core.Views/ViewModelToViewConverter.cs @@ -31,13 +31,7 @@ namespace VDownload.Core.Views { typeof(AuthenticationViewModel), typeof(AuthenticationView) } }; - #endregion - - - - #region PROPERTIES - - public static IServiceProvider ServiceProvider { protected get; set; } + protected static IServiceProvider _serviceProvider; #endregion @@ -45,6 +39,8 @@ namespace VDownload.Core.Views #region PUBLIC METHODS + public static void Initialize(IServiceProvider serviceProvider) => _serviceProvider = serviceProvider; + public object Convert(object value, Type targetType, object parameter, string language) { if (value is null) @@ -53,11 +49,11 @@ namespace VDownload.Core.Views } if (value is Type type && _viewModelViewBinding.ContainsKey(type)) { - return ServiceProvider.GetService(_viewModelViewBinding[type]); + return _serviceProvider.GetService(_viewModelViewBinding[type]); } if (_viewModelViewBinding.ContainsKey(value.GetType())) { - return ServiceProvider.GetService(_viewModelViewBinding[value.GetType()]); + return _serviceProvider.GetService(_viewModelViewBinding[value.GetType()]); } return null; } diff --git a/VDownload.Services/VDownload.Services.Common/IInitializableService.cs b/VDownload.Services/VDownload.Services.Common/IInitializableService.cs new file mode 100644 index 0000000..726f8e3 --- /dev/null +++ b/VDownload.Services/VDownload.Services.Common/IInitializableService.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VDownload.Services.Common +{ + public interface IInitializableService + { + #region METHODS + + Task Initialize(); + + #endregion + } + + public interface IInitializableService + { + #region METHODS + + Task Initialize(T arg); + + #endregion + } +} diff --git a/VDownload.Services/VDownload.Services.Common/VDownload.Services.Common.csproj b/VDownload.Services/VDownload.Services.Common/VDownload.Services.Common.csproj new file mode 100644 index 0000000..fa71b7a --- /dev/null +++ b/VDownload.Services/VDownload.Services.Common/VDownload.Services.Common.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + diff --git a/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Application/ApplicationDataService.cs b/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Application/ApplicationDataService.cs index 9684b3a..90f84c8 100644 --- a/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Application/ApplicationDataService.cs +++ b/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Application/ApplicationDataService.cs @@ -4,11 +4,12 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using VDownload.Services.Common; using VDownload.Services.Data.Configuration; namespace VDownload.Services.Data.Application { - public interface IApplicationDataService + public interface IApplicationDataService : IInitializableService { #region PROPERTIES @@ -73,6 +74,8 @@ namespace VDownload.Services.Data.Application #region PUBLIC METHODS + public async Task Initialize() => await Load(); + public async Task Load() { if (File.Exists(_filePath)) diff --git a/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Application/VDownload.Services.Data.Application.csproj b/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Application/VDownload.Services.Data.Application.csproj index 8b38c4b..62d33a0 100644 --- a/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Application/VDownload.Services.Data.Application.csproj +++ b/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Application/VDownload.Services.Data.Application.csproj @@ -11,6 +11,7 @@ + diff --git a/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Authentication/AuthenticationDataService.cs b/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Authentication/AuthenticationDataService.cs index 80a0840..f97de09 100644 --- a/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Authentication/AuthenticationDataService.cs +++ b/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Authentication/AuthenticationDataService.cs @@ -4,11 +4,12 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using VDownload.Services.Common; using VDownload.Services.Data.Configuration; namespace VDownload.Services.Data.Authentication { - public interface IAuthenticationDataService + public interface IAuthenticationDataService : IInitializableService { #region PROPERTIES @@ -70,6 +71,8 @@ namespace VDownload.Services.Data.Authentication #region PUBLIC METHODS + public async Task Initialize() => await Load(); + public async Task Load() { Data = null; diff --git a/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Authentication/VDownload.Services.Data.Authentication.csproj b/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Authentication/VDownload.Services.Data.Authentication.csproj index 8b38c4b..62d33a0 100644 --- a/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Authentication/VDownload.Services.Data.Authentication.csproj +++ b/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Authentication/VDownload.Services.Data.Authentication.csproj @@ -11,6 +11,7 @@ + diff --git a/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Settings/SettingsService.cs b/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Settings/SettingsService.cs index 2d83230..898eb44 100644 --- a/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Settings/SettingsService.cs +++ b/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Settings/SettingsService.cs @@ -4,11 +4,12 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using VDownload.Services.Common; using VDownload.Services.Data.Configuration; namespace VDownload.Services.Data.Settings { - public interface ISettingsService + public interface ISettingsService : IInitializableService { #region PROPERTIES @@ -73,6 +74,8 @@ namespace VDownload.Services.Data.Settings #region PUBLIC METHODS + public async Task Initialize() => await Load(); + public async Task Load() { if (File.Exists(_filePath)) diff --git a/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Settings/VDownload.Services.Data.Settings.csproj b/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Settings/VDownload.Services.Data.Settings.csproj index 12f9356..e689809 100644 --- a/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Settings/VDownload.Services.Data.Settings.csproj +++ b/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Settings/VDownload.Services.Data.Settings.csproj @@ -13,6 +13,7 @@ + diff --git a/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Subscriptions/SubscriptionsDataService.cs b/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Subscriptions/SubscriptionsDataService.cs index 4caee21..db08b2d 100644 --- a/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Subscriptions/SubscriptionsDataService.cs +++ b/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Subscriptions/SubscriptionsDataService.cs @@ -2,13 +2,15 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; +using VDownload.Services.Common; using VDownload.Services.Data.Configuration; namespace VDownload.Services.Data.Subscriptions { - public interface ISubscriptionsDataService + public interface ISubscriptionsDataService : IInitializableService { #region PROPERTIES @@ -72,6 +74,8 @@ namespace VDownload.Services.Data.Subscriptions #region PUBLIC METHODS + public async Task Initialize() => await Load(); + public async Task Load() { if (File.Exists(_filePath)) diff --git a/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Subscriptions/VDownload.Services.Data.Subscriptions.csproj b/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Subscriptions/VDownload.Services.Data.Subscriptions.csproj index d81009e..4c44878 100644 --- a/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Subscriptions/VDownload.Services.Data.Subscriptions.csproj +++ b/VDownload.Services/VDownload.Services.Data/VDownload.Services.Data.Subscriptions/VDownload.Services.Data.Subscriptions.csproj @@ -12,6 +12,7 @@ + diff --git a/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.Dialogs/DialogsService.cs b/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.Dialogs/DialogsService.cs index 845663d..6a80b3b 100644 --- a/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.Dialogs/DialogsService.cs +++ b/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.Dialogs/DialogsService.cs @@ -6,22 +6,13 @@ using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; +using VDownload.Services.Common; using VDownload.Services.UI.StringResources; namespace VDownload.Services.UI.Dialogs { - public interface IDialogsService + public interface IDialogsService : IInitializableService { - #region PROPERTIES - - XamlRoot DefaultRoot { get; set; } - - #endregion - - - - #region METHODS - Task ShowClose(string title, string message); Task ShowDouble(string title, string message, string primaryButtonText, string secondaryButtonText); Task ShowOk(string title, string message); @@ -30,8 +21,6 @@ namespace VDownload.Services.UI.Dialogs Task ShowTriple(string title, string message, string primaryButtonText, string secondaryButtonText, string cancelButtonText); Task ShowYesNo(string title, string message); Task ShowYesNoCancel(string title, string message); - - #endregion } @@ -54,13 +43,7 @@ namespace VDownload.Services.UI.Dialogs protected string _yesString; protected string _noString; - #endregion - - - - #region PROPERTIES - - public XamlRoot DefaultRoot { get; set; } + protected XamlRoot _root; #endregion @@ -84,6 +67,8 @@ namespace VDownload.Services.UI.Dialogs #region PUBLIC METHODS + public async Task Initialize(XamlRoot arg) => await Task.Run(() => _root = arg); + public async Task ShowOk(string title, string message) => await ShowSingle(title, message, _okString); public async Task ShowClose(string title, string message) => await ShowSingle(title, message, _closeString); public async Task ShowSingle(string title, string message, string buttonText) @@ -138,7 +123,7 @@ namespace VDownload.Services.UI.Dialogs { Title = title, Content = message, - XamlRoot = DefaultRoot + XamlRoot = _root }; } diff --git a/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.Dialogs/VDownload.Services.UI.Dialogs.csproj b/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.Dialogs/VDownload.Services.UI.Dialogs.csproj index 8744244..fd60d13 100644 --- a/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.Dialogs/VDownload.Services.UI.Dialogs.csproj +++ b/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.Dialogs/VDownload.Services.UI.Dialogs.csproj @@ -15,6 +15,7 @@ + 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 fd8818e..4c4ae40 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 @@ -4,12 +4,13 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using VDownload.Services.Common; +using Windows.Media.Core; namespace VDownload.Services.UI.DictionaryResources { - public interface IDictionaryResourcesService + public interface IDictionaryResourcesService : IInitializableService { - ResourceDictionary Resources { get; set; } T Get(string key); } @@ -17,17 +18,9 @@ namespace VDownload.Services.UI.DictionaryResources public class DictionaryResourcesService : IDictionaryResourcesService { - #region PROPERTIES + #region FIELDS - public ResourceDictionary Resources { get; set; } - - #endregion - - - - #region CONSTRUCTORS - - public DictionaryResourcesService() { } + protected ResourceDictionary _resources; #endregion @@ -35,9 +28,11 @@ namespace VDownload.Services.UI.DictionaryResources #region PUBLIC METHODS + public async Task Initialize(ResourceDictionary arg) => await Task.Run(() => _resources = arg); + public T Get(string key) { - 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.Services/VDownload.Services.UI/VDownload.Services.UI.DictionaryResources/VDownload.Services.UI.DictionaryResources.csproj b/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.DictionaryResources/VDownload.Services.UI.DictionaryResources.csproj index 0404798..54b8411 100644 --- a/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.DictionaryResources/VDownload.Services.UI.DictionaryResources.csproj +++ b/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.DictionaryResources/VDownload.Services.UI.DictionaryResources.csproj @@ -15,6 +15,7 @@ + diff --git a/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.Notifications/NotificationsService.cs b/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.Notifications/NotificationsService.cs index 653760b..527dbd2 100644 --- a/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.Notifications/NotificationsService.cs +++ b/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.Notifications/NotificationsService.cs @@ -1,17 +1,19 @@ using Microsoft.Toolkit.Uwp.Notifications; +using Microsoft.UI.Xaml; using Microsoft.Windows.AppNotifications; using Microsoft.Windows.AppNotifications.Builder; +using SimpleToolkit.UI.WinUI.Helpers; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using VDownload.Services.Common; namespace VDownload.Services.UI.Notifications { - public interface INotificationsService + public interface INotificationsService : IInitializableService { - void Initialize(Action notificationInvoked); void SendNotification(string title, IEnumerable message); } @@ -19,7 +21,15 @@ namespace VDownload.Services.UI.Notifications public class NotificationsService : INotificationsService { - #region CONSTRCUTORS + #region FIELDS + + protected Window _window; + + #endregion + + + + #region CONSTRUCTORS ~NotificationsService() { @@ -32,12 +42,14 @@ namespace VDownload.Services.UI.Notifications #region PUBLIC METHODS - public void Initialize(Action notificationInvoked) + public async Task Initialize(Window window) => await Task.Run(() => { - AppNotificationManager.Default.NotificationInvoked += (obj, args) => notificationInvoked.Invoke(); + _window = window; + + AppNotificationManager.Default.NotificationInvoked += NotificationInvoked; AppNotificationManager.Default.Register(); - } + }); public void SendNotification(string title, IEnumerable message) { @@ -54,5 +66,13 @@ namespace VDownload.Services.UI.Notifications } #endregion + + + + #region PRIVATE METHODS + + private void NotificationInvoked(AppNotificationManager sender, AppNotificationActivatedEventArgs args) => WindowHelper.ShowWindow(_window); + + #endregion } } diff --git a/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.Notifications/VDownload.Services.UI.Notifications.csproj b/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.Notifications/VDownload.Services.UI.Notifications.csproj index 8d89a91..e6fa01d 100644 --- a/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.Notifications/VDownload.Services.UI.Notifications.csproj +++ b/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.Notifications/VDownload.Services.UI.Notifications.csproj @@ -13,5 +13,10 @@ + + + + + diff --git a/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.StoragePicker/StoragePickerService.cs b/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.StoragePicker/StoragePickerService.cs index 5981f8c..effbd19 100644 --- a/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.StoragePicker/StoragePickerService.cs +++ b/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.StoragePicker/StoragePickerService.cs @@ -3,24 +3,15 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using VDownload.Services.Common; using Windows.Storage; using Windows.Storage.Pickers; using WinRT.Interop; namespace VDownload.Services.UI.StoragePicker { - public interface IStoragePickerService + public interface IStoragePickerService : IInitializableService { - #region PROPERTIES - - Window DefaultRoot { get; set; } - - #endregion - - - - #region METHODS - Task OpenDirectory(); Task OpenDirectory(StoragePickerStartLocation startLocation); Task> OpenMultipleFiles(); @@ -33,17 +24,15 @@ namespace VDownload.Services.UI.StoragePicker Task OpenSingleFile(string[] fileTypes, StoragePickerStartLocation startLocation); Task SaveFile(FileSavePickerFileTypeChoice[] fileTypes, string defaultFileType); Task SaveFile(FileSavePickerFileTypeChoice[] fileTypes, string defaultFileType, StoragePickerStartLocation startLocation); - - #endregion } public class StoragePickerService : IStoragePickerService { - #region PROPERTIES + #region FIELDS - public Window DefaultRoot { get; set; } + protected Window _root; #endregion @@ -51,6 +40,8 @@ namespace VDownload.Services.UI.StoragePicker #region PUBLIC METHODS + public async Task Initialize(Window arg) => await Task.Run(() => _root = arg); + public async Task OpenDirectory() => await OpenDirectory(StoragePickerStartLocation.Unspecified); public async Task OpenDirectory(StoragePickerStartLocation startLocation) { @@ -111,7 +102,7 @@ namespace VDownload.Services.UI.StoragePicker protected void InitializePicker(object picker) { - var hwnd = WindowNative.GetWindowHandle(DefaultRoot); + var hwnd = WindowNative.GetWindowHandle(_root); InitializeWithWindow.Initialize(picker, hwnd); } diff --git a/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.StoragePicker/VDownload.Services.UI.StoragePicker.csproj b/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.StoragePicker/VDownload.Services.UI.StoragePicker.csproj index 6ea7798..8407c20 100644 --- a/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.StoragePicker/VDownload.Services.UI.StoragePicker.csproj +++ b/VDownload.Services/VDownload.Services.UI/VDownload.Services.UI.StoragePicker/VDownload.Services.UI.StoragePicker.csproj @@ -13,4 +13,8 @@ + + + + diff --git a/VDownload.sln b/VDownload.sln index 117e850..9e75b74 100644 --- a/VDownload.sln +++ b/VDownload.sln @@ -73,7 +73,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VDownload.Services.Utility. EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VDownload.Services.Data.Application", "VDownload.Services\VDownload.Services.Data\VDownload.Services.Data.Application\VDownload.Services.Data.Application.csproj", "{4983E15B-3730-4646-A2BD-16B9ECC9E4FF}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VDownload.Services.Data.Subscriptions", "VDownload.Services\VDownload.Services.Data\VDownload.Services.Data.Subscriptions\VDownload.Services.Data.Subscriptions.csproj", "{3193DABC-87F8-4256-9449-3CF42FEF7098}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VDownload.Services.Data.Subscriptions", "VDownload.Services\VDownload.Services.Data\VDownload.Services.Data.Subscriptions\VDownload.Services.Data.Subscriptions.csproj", "{3193DABC-87F8-4256-9449-3CF42FEF7098}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VDownload.Services.Common", "VDownload.Services\VDownload.Services.Common\VDownload.Services.Common.csproj", "{267F5A31-1257-4820-9FE5-C11D26CC3C55}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -557,6 +559,22 @@ Global {3193DABC-87F8-4256-9449-3CF42FEF7098}.Release|x64.Build.0 = Release|Any CPU {3193DABC-87F8-4256-9449-3CF42FEF7098}.Release|x86.ActiveCfg = Release|Any CPU {3193DABC-87F8-4256-9449-3CF42FEF7098}.Release|x86.Build.0 = Release|Any CPU + {267F5A31-1257-4820-9FE5-C11D26CC3C55}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {267F5A31-1257-4820-9FE5-C11D26CC3C55}.Debug|Any CPU.Build.0 = Debug|Any CPU + {267F5A31-1257-4820-9FE5-C11D26CC3C55}.Debug|ARM64.ActiveCfg = Debug|Any CPU + {267F5A31-1257-4820-9FE5-C11D26CC3C55}.Debug|ARM64.Build.0 = Debug|Any CPU + {267F5A31-1257-4820-9FE5-C11D26CC3C55}.Debug|x64.ActiveCfg = Debug|Any CPU + {267F5A31-1257-4820-9FE5-C11D26CC3C55}.Debug|x64.Build.0 = Debug|Any CPU + {267F5A31-1257-4820-9FE5-C11D26CC3C55}.Debug|x86.ActiveCfg = Debug|Any CPU + {267F5A31-1257-4820-9FE5-C11D26CC3C55}.Debug|x86.Build.0 = Debug|Any CPU + {267F5A31-1257-4820-9FE5-C11D26CC3C55}.Release|Any CPU.ActiveCfg = Release|Any CPU + {267F5A31-1257-4820-9FE5-C11D26CC3C55}.Release|Any CPU.Build.0 = Release|Any CPU + {267F5A31-1257-4820-9FE5-C11D26CC3C55}.Release|ARM64.ActiveCfg = Release|Any CPU + {267F5A31-1257-4820-9FE5-C11D26CC3C55}.Release|ARM64.Build.0 = Release|Any CPU + {267F5A31-1257-4820-9FE5-C11D26CC3C55}.Release|x64.ActiveCfg = Release|Any CPU + {267F5A31-1257-4820-9FE5-C11D26CC3C55}.Release|x64.Build.0 = Release|Any CPU + {267F5A31-1257-4820-9FE5-C11D26CC3C55}.Release|x86.ActiveCfg = Release|Any CPU + {267F5A31-1257-4820-9FE5-C11D26CC3C55}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -593,6 +611,7 @@ Global {4647EFB5-A206-4F47-976D-BAED11B52579} = {1020167A-4101-496E-82CF-41B65769DD28} {4983E15B-3730-4646-A2BD-16B9ECC9E4FF} = {05A45688-7EEF-4656-818A-2477625C3707} {3193DABC-87F8-4256-9449-3CF42FEF7098} = {05A45688-7EEF-4656-818A-2477625C3707} + {267F5A31-1257-4820-9FE5-C11D26CC3C55} = {8D351DB0-74E6-4C1E-A123-34D6BBBD5585} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {9FD7B842-C3E2-4FD0-AD8A-C8E619280AB7} diff --git a/VDownload/Activation/ActivationHandler.cs b/VDownload/Activation/ActivationHandler.cs new file mode 100644 index 0000000..1d6ef5f --- /dev/null +++ b/VDownload/Activation/ActivationHandler.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VDownload.Activation +{ + public interface IActivationHandler + { + bool CanHandle(object args); + Task HandleAsync(object args); + } + + + + public abstract class ActivationHandler : IActivationHandler + where T : class + { + #region PUBLIC METHODS + + public bool CanHandle(object args) => args is T && CanHandleInternal((args as T)!); + + public async Task HandleAsync(object args) => await HandleInternalAsync((args as T)!); + + #endregion + + + + #region PRIVATE METHODS + + protected virtual bool CanHandleInternal(T args) => true; + + protected abstract Task HandleInternalAsync(T args); + + #endregion + } +} diff --git a/VDownload/Activation/ActivationService.cs b/VDownload/Activation/ActivationService.cs new file mode 100644 index 0000000..6cbd3af --- /dev/null +++ b/VDownload/Activation/ActivationService.cs @@ -0,0 +1,131 @@ +using Microsoft.UI.Xaml; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VDownload.Core.Views; +using VDownload.Services.Common; +using VDownload.Services.Data.Application; +using VDownload.Services.Data.Authentication; +using VDownload.Services.Data.Settings; +using VDownload.Services.Data.Subscriptions; +using VDownload.Services.UI.Dialogs; +using VDownload.Services.UI.DictionaryResources; +using VDownload.Services.UI.Notifications; +using VDownload.Services.UI.StoragePicker; + +namespace VDownload.Activation +{ + public interface IActivationService + { + Task ActivateAsync(object activationArgs); + } + + + + public class ActivationService : IActivationService + { + #region SERVICES + protected readonly ActivationHandler _defaultHandler; + protected readonly IEnumerable _activationHandlers; + + #endregion + + + + #region FIELDS + + protected readonly ICollection> _beforeActivationInitializations = new List>(); + protected readonly ICollection> _afterActivationInitializations = new List>(); + protected readonly ICollection> _afterWindowRootLoadedInitializations = new List>(); + + protected BaseWindow _window; + + #endregion + + + + #region CONSTRUCTORS + + public ActivationService(ActivationHandler defaultHandler, IEnumerable activationHandlers, ISettingsService settingsService, IApplicationDataService applicationDataService, IAuthenticationDataService authenticationDataService, ISubscriptionsDataService subscriptionsDataService, IStoragePickerService storagePickerService, INotificationsService notificationsService, IDictionaryResourcesService dictionaryResourcesService, IDialogsService dialogsService) + { + _defaultHandler = defaultHandler; + _activationHandlers = activationHandlers; + + _beforeActivationInitializations.Add(() => dictionaryResourcesService.Initialize((App.Current as App).Resources)); + + _afterActivationInitializations.Add(settingsService.Initialize); + _afterActivationInitializations.Add(applicationDataService.Initialize); + _afterActivationInitializations.Add(authenticationDataService.Initialize); + _afterActivationInitializations.Add(subscriptionsDataService.Initialize); + _afterActivationInitializations.Add(() => storagePickerService.Initialize(_window)); + _afterActivationInitializations.Add(() => notificationsService.Initialize(_window)); + + _afterWindowRootLoadedInitializations.Add(() => dialogsService.Initialize(_window.XamlRoot)); + } + + #endregion + + + + #region PUBLIC METHODS + + public async Task ActivateAsync(object activationArgs) + { + await InitializeAsync(); + ViewModelToViewConverter.Initialize((App.Current as App)!.Host.Services); + + await HandleActivationAsync(activationArgs); + + _window = App.GetService(); + _window.RootLoaded += Window_RootLoaded; + _window.Activate(); + + await StartupAsync(); + } + + #endregion + + + + #region PRIVATE METHODS + + #region EVENT HANDLERS + + protected async void Window_RootLoaded(object sender, EventArgs e) => await AfterWindowRootLoaded(); + + #endregion + + protected async Task InitializeAsync() + { + List tasks = new List(); + foreach (Func init in _beforeActivationInitializations) + { + tasks.Add(init.Invoke()); + } + await Task.WhenAll(tasks); + } + + protected async Task StartupAsync() => await Task.WhenAll(_afterActivationInitializations.Select(x => x.Invoke())); + + protected async Task AfterWindowRootLoaded() => await Task.WhenAll(_afterWindowRootLoadedInitializations.Select(x => x.Invoke())); + + protected async Task HandleActivationAsync(object activationArgs) + { + var activationHandler = _activationHandlers.FirstOrDefault(h => h.CanHandle(activationArgs)); + + if (activationHandler != null) + { + await activationHandler.HandleAsync(activationArgs); + } + + if (_defaultHandler.CanHandle(activationArgs)) + { + await _defaultHandler.HandleAsync(activationArgs); + } + } + + #endregion + } +} diff --git a/VDownload/Activation/DefaultActivationHandler.cs b/VDownload/Activation/DefaultActivationHandler.cs new file mode 100644 index 0000000..402ec4c --- /dev/null +++ b/VDownload/Activation/DefaultActivationHandler.cs @@ -0,0 +1,21 @@ +using Microsoft.UI.Xaml; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VDownload.Activation +{ + public class DefaultActivationHandler : ActivationHandler + { + #region PRIVATE METHODS + + protected override async Task HandleInternalAsync(LaunchActivatedEventArgs args) + { + await Task.CompletedTask; + } + + #endregion + } +} diff --git a/VDownload/App.xaml.cs b/VDownload/App.xaml.cs index c390645..5bed22b 100644 --- a/VDownload/App.xaml.cs +++ b/VDownload/App.xaml.cs @@ -10,6 +10,7 @@ using System; using System.Net; using System.Net.Http; using System.Threading.Tasks; +using VDownload.Activation; using VDownload.Core.Tasks; using VDownload.Core.ViewModels; using VDownload.Core.ViewModels.About; @@ -97,49 +98,11 @@ namespace VDownload BuildTasksManager(services); BuildPresentation(services); + BuildActivation(services); }) .Build(); - } - #endregion - - - - #region EVENT HANDLERS - - private void WindowRootLoaded(object sender, EventArgs e) - { - GetService().DefaultRoot = Window.XamlRoot; - } - - 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 - ); - - Window.Activate(); + UnhandledException += UnhandledExceptionCatched; } #endregion @@ -148,6 +111,22 @@ namespace VDownload #region PRIVATE METHODS + #region EVENT HANDLERS + + protected override async void OnLaunched(LaunchActivatedEventArgs args) + { + base.OnLaunched(args); + + await GetService().ActivateAsync(args); + } + + protected void UnhandledExceptionCatched(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e) + { + throw new NotImplementedException(); + } + + #endregion + protected void BuildCore(IServiceCollection services) { services.AddSingleton(); @@ -223,6 +202,13 @@ namespace VDownload services.AddTransient(); } + protected void BuildActivation(IServiceCollection services) + { + services.AddTransient, DefaultActivationHandler>(); + + services.AddSingleton(); + } + #endregion } } diff --git a/VDownload/VDownload.csproj b/VDownload/VDownload.csproj index 63d4fc2..df49035 100644 --- a/VDownload/VDownload.csproj +++ b/VDownload/VDownload.csproj @@ -16,8 +16,8 @@ LICENSE True 0.0.0 + false None - True true diff --git a/VDownload/WindowHelper.cs b/VDownload/WindowHelper.cs deleted file mode 100644 index e63b411..0000000 --- a/VDownload/WindowHelper.cs +++ /dev/null @@ -1,43 +0,0 @@ -using Microsoft.UI.Xaml; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; - -namespace VDownload -{ - public static partial class WindowHelper - { - #region PUBLIC METHODS - - public static void ShowWindow(Window window) - { - // Bring the window to the foreground... first get the window handle... - var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(window); - - // Restore window if minimized... requires DLL import above - ShowWindow(hwnd, 0x00000009); - - // And call SetForegroundWindow... requires DLL import above - SetForegroundWindow(hwnd); - } - - #endregion - - - - #region PRIVATE METHODS - - [LibraryImport("user32.dll")] - [return: MarshalAs(UnmanagedType.Bool)] - private static partial bool ShowWindow(IntPtr hWnd, int nCmdShow); - - [LibraryImport("user32.dll")] - [return: MarshalAs(UnmanagedType.Bool)] - private static partial bool SetForegroundWindow(IntPtr hWnd); - - #endregion - } -}