Skip to content

Commit

Permalink
Refactored TgDownloaderDesktop
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianMorozov committed Nov 7, 2024
1 parent 6e3b000 commit bab1a71
Show file tree
Hide file tree
Showing 63 changed files with 1,776 additions and 1,813 deletions.
5 changes: 4 additions & 1 deletion Clients/TgDownloaderDesktop/Activation/ActivationHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace TgDownloaderDesktop.Activation;
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com

namespace TgDownloaderDesktop.Activation;

// Extend this class to implement new ActivationHandlers. See DefaultActivationHandler for an example.
// https://github.com/microsoft/TemplateStudio/blob/main/docs/WinUI/activation.md
Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,49 @@
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml;
using Microsoft.Windows.AppLifecycle;
using Microsoft.Windows.AppNotifications;
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com

using TgDownloaderDesktop.Contracts.Services;
using TgDownloaderDesktop.ViewModels;
using AppInstance = Microsoft.Windows.AppLifecycle.AppInstance;
using DispatcherQueuePriority = Microsoft.UI.Dispatching.DispatcherQueuePriority;

namespace TgDownloaderDesktop.Activation;

public class AppNotificationActivationHandler : ActivationHandler<LaunchActivatedEventArgs>
{
private readonly INavigationService _navigationService;
private readonly IAppNotificationService _notificationService;

public AppNotificationActivationHandler(INavigationService navigationService, IAppNotificationService notificationService)
{
_navigationService = navigationService;
_notificationService = notificationService;
}

protected override bool CanHandleInternal(LaunchActivatedEventArgs args)
{
return AppInstance.GetCurrent().GetActivatedEventArgs()?.Kind == ExtendedActivationKind.AppNotification;
}

protected async override Task HandleInternalAsync(LaunchActivatedEventArgs args)
{
// TODO: Handle notification activations.

//// // Access the AppNotificationActivatedEventArgs.
//// var activatedEventArgs = (AppNotificationActivatedEventArgs)AppInstance.GetCurrent().GetActivatedEventArgs().Data;

//// // Navigate to a specific page based on the notification arguments.
//// if (_notificationService.ParseArguments(activatedEventArgs.Argument)["action"] == "Settings")
//// {
//// // Queue navigation with low priority to allow the UI to initialize.
//// App.MainWindow.DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, () =>
//// {
//// _navigationService.NavigateTo(typeof(SettingsViewModel).FullName!);
//// });
//// }

App.MainWindow.DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, () =>
{
App.MainWindow.ShowMessageDialogAsync("TODO: Handle notification activations.", "Notification Activation");
});

await Task.CompletedTask;
}
private readonly INavigationService _navigationService;
private readonly IAppNotificationService _notificationService;

public AppNotificationActivationHandler(INavigationService navigationService, IAppNotificationService notificationService)
{
_navigationService = navigationService;
_notificationService = notificationService;
}

protected override bool CanHandleInternal(LaunchActivatedEventArgs args)
{
return AppInstance.GetCurrent().GetActivatedEventArgs()?.Kind == ExtendedActivationKind.AppNotification;
}

protected override async Task HandleInternalAsync(LaunchActivatedEventArgs args)
{
// TODO: Handle notification activations.

//// // Access the AppNotificationActivatedEventArgs.
//// var activatedEventArgs = (AppNotificationActivatedEventArgs)AppInstance.GetCurrent().GetActivatedEventArgs().Data;

//// // Navigate to a specific page based on the notification arguments.
//// if (_notificationService.ParseArguments(activatedEventArgs.Argument)["action"] == "Settings")
//// {
//// // Queue navigation with low priority to allow the UI to initialize.
//// App.MainWindow.DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, () =>
//// {
//// _navigationService.NavigateTo(typeof(SettingsViewModel).FullName!);
//// });
//// }

App.MainWindow.DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, () =>
{
App.MainWindow.ShowMessageDialogAsync("TODO: Handle notification activations.", "Notification Activation");
});

await Task.CompletedTask;
}
}
36 changes: 17 additions & 19 deletions Clients/TgDownloaderDesktop/Activation/DefaultActivationHandler.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
using Microsoft.UI.Xaml;

using TgDownloaderDesktop.Contracts.Services;
using TgDownloaderDesktop.ViewModels;
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com

namespace TgDownloaderDesktop.Activation;

public class DefaultActivationHandler : ActivationHandler<LaunchActivatedEventArgs>
{
private readonly INavigationService _navigationService;
private readonly INavigationService _navigationService;

public DefaultActivationHandler(INavigationService navigationService)
{
_navigationService = navigationService;
}
public DefaultActivationHandler(INavigationService navigationService)
{
_navigationService = navigationService;
}

protected override bool CanHandleInternal(LaunchActivatedEventArgs args)
{
// None of the ActivationHandlers has handled the activation.
return _navigationService.Frame?.Content == null;
}
protected override bool CanHandleInternal(LaunchActivatedEventArgs args)
{
// None of the ActivationHandlers has handled the activation.
return _navigationService.Frame?.Content == null;
}

protected async override Task HandleInternalAsync(LaunchActivatedEventArgs args)
{
_navigationService.NavigateTo(typeof(MainViewModel).FullName!, args.Arguments);
protected override async Task HandleInternalAsync(LaunchActivatedEventArgs args)
{
_navigationService.NavigateTo(typeof(MainViewModel).FullName!, args.Arguments);

await Task.CompletedTask;
}
await Task.CompletedTask;
}
}
5 changes: 4 additions & 1 deletion Clients/TgDownloaderDesktop/Activation/IActivationHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace TgDownloaderDesktop.Activation;
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com

namespace TgDownloaderDesktop.Activation;

public interface IActivationHandler
{
Expand Down
205 changes: 94 additions & 111 deletions Clients/TgDownloaderDesktop/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,118 +1,101 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.UI.Xaml;

using TgDownloaderDesktop.Activation;
using TgDownloaderDesktop.Contracts.Services;
using TgDownloaderDesktop.Core.Contracts.Services;
using TgDownloaderDesktop.Core.Services;
using TgDownloaderDesktop.Helpers;
using TgDownloaderDesktop.Models;
using TgDownloaderDesktop.Notifications;
using TgDownloaderDesktop.Services;
using TgDownloaderDesktop.ViewModels;
using TgDownloaderDesktop.Views;
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com

namespace TgDownloaderDesktop;

// To learn more about WinUI 3, see https://docs.microsoft.com/windows/apps/winui/winui3/.
public partial class App : Application
{
// The .NET Generic Host provides dependency injection, configuration, logging, and other services.
// https://docs.microsoft.com/dotnet/core/extensions/generic-host
// https://docs.microsoft.com/dotnet/core/extensions/dependency-injection
// https://docs.microsoft.com/dotnet/core/extensions/configuration
// https://docs.microsoft.com/dotnet/core/extensions/logging
public IHost Host
{
get;
}

public static T GetService<T>()
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 static WindowEx MainWindow { get; } = new MainWindow();

public static UIElement? AppTitlebar { get; set; }

public App()
{
InitializeComponent();

Host = Microsoft.Extensions.Hosting.Host.
CreateDefaultBuilder().
UseContentRoot(AppContext.BaseDirectory).
ConfigureServices((context, services) =>
{
// Default Activation Handler
services.AddTransient<ActivationHandler<LaunchActivatedEventArgs>, DefaultActivationHandler>();

// Other Activation Handlers
services.AddTransient<IActivationHandler, AppNotificationActivationHandler>();

// Services
services.AddSingleton<IAppNotificationService, AppNotificationService>();
services.AddSingleton<ILocalSettingsService, LocalSettingsService>();
services.AddSingleton<IThemeSelectorService, ThemeSelectorService>();
services.AddTransient<IWebViewService, WebViewService>();
services.AddTransient<INavigationViewService, NavigationViewService>();

services.AddSingleton<IActivationService, ActivationService>();
services.AddSingleton<IPageService, PageService>();
services.AddSingleton<INavigationService, NavigationService>();

// Core Services
services.AddSingleton<ISampleDataService, SampleDataService>();
services.AddSingleton<IFileService, FileService>();

// Views and ViewModels
services.AddTransient<SettingsViewModel>();
services.AddTransient<SettingsPage>();
services.AddTransient<DataGridViewModel>();
services.AddTransient<DataGridPage>();
services.AddTransient<ContentGridDetailViewModel>();
services.AddTransient<ContentGridDetailPage>();
services.AddTransient<ContentGridViewModel>();
services.AddTransient<ContentGridPage>();
services.AddTransient<ListDetailsViewModel>();
services.AddTransient<ListDetailsPage>();
services.AddTransient<WebViewViewModel>();
services.AddTransient<WebViewPage>();
services.AddTransient<MainViewModel>();
services.AddTransient<MainPage>();
services.AddTransient<ShellPage>();
services.AddTransient<ShellViewModel>();

// Configuration
services.Configure<LocalSettingsOptions>(context.Configuration.GetSection(nameof(LocalSettingsOptions)));
}).
Build();

App.GetService<IAppNotificationService>().Initialize();

UnhandledException += App_UnhandledException;
}

private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
{
// TODO: Log and handle exceptions as appropriate.
// https://docs.microsoft.com/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.application.unhandledexception.
}

protected async override void OnLaunched(LaunchActivatedEventArgs args)
{
base.OnLaunched(args);

App.GetService<IAppNotificationService>().Show(string.Format("AppNotificationSamplePayload".GetLocalized(), AppContext.BaseDirectory));

await App.GetService<IActivationService>().ActivateAsync(args);
}
// The .NET Generic Host provides dependency injection, configuration, logging, and other services.
// https://docs.microsoft.com/dotnet/core/extensions/generic-host
// https://docs.microsoft.com/dotnet/core/extensions/dependency-injection
// https://docs.microsoft.com/dotnet/core/extensions/configuration
// https://docs.microsoft.com/dotnet/core/extensions/logging
public IHost Host { get; }

public static T GetService<T>() 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 static WindowEx MainWindow { get; } = new MainWindow();

public static UIElement? AppTitlebar { get; set; }

public App()
{
InitializeComponent();

Host = Microsoft.Extensions.Hosting.Host.
CreateDefaultBuilder().
UseContentRoot(AppContext.BaseDirectory).
ConfigureServices((context, services) =>
{
// Default Activation Handler
services.AddTransient<ActivationHandler<LaunchActivatedEventArgs>, DefaultActivationHandler>();

// Other Activation Handlers
services.AddTransient<IActivationHandler, AppNotificationActivationHandler>();

// Services
services.AddSingleton<IAppNotificationService, AppNotificationService>();
services.AddSingleton<ILocalSettingsService, LocalSettingsService>();
services.AddSingleton<IThemeSelectorService, ThemeSelectorService>();
services.AddTransient<IWebViewService, WebViewService>();
services.AddTransient<INavigationViewService, NavigationViewService>();

services.AddSingleton<IActivationService, ActivationService>();
services.AddSingleton<IPageService, PageService>();
services.AddSingleton<INavigationService, NavigationService>();

// Core Services
services.AddSingleton<ISampleDataService, SampleDataService>();
services.AddSingleton<IFileService, FileService>();

// Views and ViewModels
services.AddTransient<SettingsViewModel>();
services.AddTransient<SettingsPage>();
services.AddTransient<DataGridViewModel>();
services.AddTransient<DataGridPage>();
services.AddTransient<ContentGridDetailViewModel>();
services.AddTransient<ContentGridDetailPage>();
services.AddTransient<ContentGridViewModel>();
services.AddTransient<ContentGridPage>();
services.AddTransient<ListDetailsViewModel>();
services.AddTransient<ListDetailsPage>();
services.AddTransient<WebViewViewModel>();
services.AddTransient<WebViewPage>();
services.AddTransient<MainViewModel>();
services.AddTransient<MainPage>();
services.AddTransient<ShellPage>();
services.AddTransient<ShellViewModel>();

// Configuration
services.Configure<LocalSettingsOptions>(context.Configuration.GetSection(nameof(LocalSettingsOptions)));
}).
Build();

App.GetService<IAppNotificationService>().Initialize();

UnhandledException += App_UnhandledException;
}

private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
{
// TODO: Log and handle exceptions as appropriate.
// https://docs.microsoft.com/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.application.unhandledexception.
}

protected override async void OnLaunched(LaunchActivatedEventArgs args)
{
base.OnLaunched(args);

App.GetService<IAppNotificationService>().Show(string.Format("AppNotificationSamplePayload".GetLocalized(), AppContext.BaseDirectory));

await App.GetService<IActivationService>().ActivateAsync(args);
}
}
Loading

0 comments on commit bab1a71

Please sign in to comment.