Files
VDownload/VDownload/App.xaml.cs

215 lines
7.9 KiB
C#
Raw Normal View History

2024-02-13 02:59:40 +01:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.UI.Xaml;
2021-12-19 14:58:47 +01:00
using System;
2024-02-13 02:59:40 +01:00
using System.Net.Http;
2024-02-28 01:22:13 +01:00
using System.Threading.Tasks;
using VDownload.Core.Tasks;
using VDownload.Core.ViewModels;
2024-03-06 01:26:43 +01:00
using VDownload.Core.ViewModels.About;
using VDownload.Core.ViewModels.Authentication;
using VDownload.Core.ViewModels.Home;
using VDownload.Core.ViewModels.Settings;
2024-03-08 20:48:31 +01:00
using VDownload.Core.ViewModels.Subscriptions;
using VDownload.Core.Views;
2024-03-06 01:26:43 +01:00
using VDownload.Core.Views.About;
using VDownload.Core.Views.Authentication;
using VDownload.Core.Views.Home;
using VDownload.Core.Views.Settings;
2024-03-08 20:48:31 +01:00
using VDownload.Core.Views.Subscriptions;
2024-03-07 20:46:30 +01:00
using VDownload.Services.Data.Application;
using VDownload.Services.Data.Authentication;
using VDownload.Services.Data.Configuration;
2024-03-03 23:05:32 +01:00
using VDownload.Services.Data.Configuration.Models;
using VDownload.Services.Data.Settings;
2024-03-09 02:56:56 +01:00
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;
using VDownload.Services.UI.StringResources;
using VDownload.Services.UI.WebView;
using VDownload.Services.Utility.Encryption;
using VDownload.Services.Utility.FFmpeg;
2024-03-03 23:05:32 +01:00
using VDownload.Services.Utility.Filename;
using VDownload.Services.Utility.HttpClient;
using VDownload.Sources;
2024-02-13 02:59:40 +01:00
using VDownload.Sources.Twitch;
using VDownload.Sources.Twitch.Api;
using VDownload.Sources.Twitch.Authentication;
using Windows.Graphics.Printing;
2021-12-19 14:58:47 +01:00
namespace VDownload
{
2024-02-13 02:59:40 +01:00
public partial class App : Application
2021-12-19 14:58:47 +01:00
{
2024-02-13 02:59:40 +01:00
#region FIELDS
2022-03-02 22:13:28 +01:00
protected IServiceProvider _serviceProvider;
protected BaseWindow _window;
2021-12-19 14:58:47 +01:00
2022-03-02 22:13:28 +01:00
#endregion
2024-02-13 02:59:40 +01:00
#region CONSTRUCTORS
2022-03-02 22:13:28 +01:00
2024-02-13 02:59:40 +01:00
public App()
2021-12-19 14:58:47 +01:00
{
2024-02-13 02:59:40 +01:00
this.InitializeComponent();
2021-12-19 14:58:47 +01:00
IServiceCollection services = new ServiceCollection();
BuildCore(services);
BuildConfiguration(services);
BuildDataServices(services);
BuildUIServices(services);
BuildUtilityServices(services);
BuildSourcesServices(services);
BuildTasksManager(services);
BuildPresentation(services);
_serviceProvider = services.BuildServiceProvider();
}
#endregion
#region PRIVATE METHODS
protected void BuildCore(IServiceCollection services)
{
services.AddSingleton<HttpClient>();
}
protected void BuildConfiguration(IServiceCollection services)
{
2024-02-13 02:59:40 +01:00
IConfigurationBuilder configBuilder = new ConfigurationBuilder
2021-12-19 14:58:47 +01:00
{
2024-02-13 02:59:40 +01:00
Sources =
2021-12-19 14:58:47 +01:00
{
2024-02-13 02:59:40 +01:00
new JsonConfigurationSource
{
Path = "configuration.json"
2024-02-13 02:59:40 +01:00
}
2021-12-19 14:58:47 +01:00
}
2024-02-13 02:59:40 +01:00
};
IConfiguration config = configBuilder.Build();
services.AddSingleton(config);
}
2024-02-13 02:59:40 +01:00
protected void BuildDataServices(IServiceCollection services)
{
services.AddSingleton<IConfigurationService, ConfigurationService>();
services.AddSingleton<IAuthenticationDataService, AuthenticationDataService>();
services.AddSingleton<ISettingsService, SettingsService>();
2024-03-07 20:46:30 +01:00
services.AddSingleton<IApplicationDataService, ApplicationDataService>();
2024-03-09 02:56:56 +01:00
services.AddSingleton<ISubscriptionsDataService, SubscriptionsDataService>();
}
2024-02-13 02:59:40 +01:00
protected void BuildUIServices(IServiceCollection services)
{
services.AddSingleton<IStringResourcesService, StringResourcesService>();
services.AddSingleton<IDictionaryResourcesService, DictionaryResourcesService>();
services.AddSingleton<IWebViewService, WebViewService>();
services.AddSingleton<IStoragePickerService, StoragePickerService>();
services.AddSingleton<INotificationsService, NotificationsService>();
services.AddSingleton<IDialogsService, DialogsService>();
}
2024-02-13 02:59:40 +01:00
protected void BuildUtilityServices(IServiceCollection services)
{
2024-02-13 02:59:40 +01:00
services.AddSingleton<IEncryptionService, EncryptionService>();
services.AddSingleton<IHttpClientService, HttpClientService>();
services.AddSingleton<IFFmpegService, FFmpegService>();
2024-03-03 23:05:32 +01:00
services.AddSingleton<IFilenameService, FilenameService>();
}
protected void BuildSourcesServices(IServiceCollection services)
{
// Twitch
2024-02-13 02:59:40 +01:00
services.AddSingleton<ITwitchApiService, TwitchApiService>();
services.AddSingleton<ITwitchAuthenticationService, TwitchAuthenticationService>();
services.AddSingleton<ITwitchVideoStreamFactoryService, TwitchVideoStreamFactoryService>();
2024-02-13 02:59:40 +01:00
services.AddSingleton<ITwitchSearchService, TwitchSearchService>();
// Base
services.AddSingleton<ISearchService, SearchService>();
}
2024-02-13 02:59:40 +01:00
protected void BuildTasksManager(IServiceCollection services)
{
services.AddSingleton<IDownloadTaskFactoryService, DownloadTaskFactoryService>();
services.AddSingleton<IDownloadTaskManager, DownloadTaskManager>();
}
2024-02-13 02:59:40 +01:00
protected void BuildPresentation(IServiceCollection services)
{
2024-02-13 02:59:40 +01:00
// ViewModels
2024-03-06 01:26:43 +01:00
services.AddSingleton<AboutViewModel>();
2024-02-13 02:59:40 +01:00
services.AddSingleton<AuthenticationViewModel>();
services.AddSingleton<SettingsViewModel>();
services.AddSingleton<HomeDownloadsViewModel>();
services.AddSingleton<HomeVideoViewModel>();
services.AddSingleton<HomeVideoCollectionViewModel>();
services.AddSingleton<HomeViewModel>();
2024-03-08 20:48:31 +01:00
services.AddSingleton<SubscriptionsViewModel>();
services.AddSingleton<BaseViewModel>();
2024-02-13 02:59:40 +01:00
// Views
2024-03-06 01:26:43 +01:00
services.AddTransient<AboutView>();
2024-02-13 02:59:40 +01:00
services.AddTransient<AuthenticationView>();
services.AddTransient<SettingsView>();
services.AddTransient<HomeDownloadsView>();
services.AddTransient<HomeVideoView>();
services.AddTransient<HomeVideoCollectionView>();
services.AddTransient<HomeView>();
2024-03-08 20:48:31 +01:00
services.AddTransient<SubscriptionsView>();
services.AddTransient<BaseWindow>();
2021-12-19 14:58:47 +01:00
}
2024-02-28 01:22:13 +01:00
protected override async void OnLaunched(LaunchActivatedEventArgs args)
{
2024-02-28 01:22:13 +01:00
await InitData();
2024-02-13 02:59:40 +01:00
_window = _serviceProvider.GetService<BaseWindow>();
_window.RootLoaded += Window_RootLoaded;
2024-02-13 02:59:40 +01:00
_window.Activate();
AssignStaticProperties();
}
2024-02-13 02:59:40 +01:00
2024-02-28 01:22:13 +01:00
protected async Task InitData()
{
2024-03-07 20:46:30 +01:00
IApplicationDataService applicationDataService = _serviceProvider.GetService<IApplicationDataService>();
2024-02-28 01:22:13 +01:00
ISettingsService settingsService = _serviceProvider.GetService<ISettingsService>();
IAuthenticationDataService authenticationDataService = _serviceProvider.GetService<IAuthenticationDataService>();
2024-03-09 02:56:56 +01:00
ISubscriptionsDataService subscriptionsDataService = _serviceProvider.GetService<ISubscriptionsDataService>();
await Task.WhenAll(applicationDataService.Load(), settingsService.Load(), authenticationDataService.Load(), subscriptionsDataService.Load());
2024-02-28 01:22:13 +01:00
}
protected void AssignStaticProperties()
{
IStoragePickerService storagePickerService = _serviceProvider.GetService<IStoragePickerService>();
storagePickerService.DefaultRoot = _window;
ViewModelToViewConverter.ServiceProvider = _serviceProvider;
}
protected void Window_RootLoaded(object sender, EventArgs e)
{
IDialogsService dialogsService = _serviceProvider.GetService<IDialogsService>();
dialogsService.DefaultRoot = _window.XamlRoot;
2021-12-19 14:58:47 +01:00
}
2022-03-02 22:13:28 +01:00
#endregion
2021-12-19 14:58:47 +01:00
}
}