Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do some refactoring #15

Merged
merged 13 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,4 @@ MigrationBackup/
# Secure Area. Always ignore key files, etc.
.secureArea/*
.secureArea.7z
/src/electrifier/Strings/de-de/Resources.resw
2 changes: 1 addition & 1 deletion src/electrifier/Activation/ActivationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ public abstract class ActivationHandler<T> : IActivationHandler
public bool CanHandle(object args) => args is T && CanHandleInternal((args as T)!);

public async Task HandleAsync(object args) => await HandleInternalAsync((args as T)!);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using electrifier.Contracts.Services;
using electrifier.ViewModels;

using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml;
using Microsoft.Windows.AppLifecycle;
using Microsoft.Windows.AppNotifications;

namespace electrifier.Activation;

Expand Down Expand Up @@ -48,4 +45,4 @@ protected async override Task HandleInternalAsync(LaunchActivatedEventArgs args)

await Task.CompletedTask;
}
}
}
2 changes: 1 addition & 1 deletion src/electrifier/Activation/DefaultActivationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ protected async override Task HandleInternalAsync(LaunchActivatedEventArgs args)

await Task.CompletedTask;
}
}
}
2 changes: 1 addition & 1 deletion src/electrifier/Activation/IActivationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ public interface IActivationHandler
bool CanHandle(object args);

Task HandleAsync(object args);
}
}
201 changes: 97 additions & 104 deletions src/electrifier/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
// Disable XAML Generated break on unhalted exception
// <seealso href="" />
#define DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
//#define DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION


using System.Text;
using CommunityToolkit.WinUI;
using electrifier.Activation;
using electrifier.Contracts.Services;
using electrifier.Models.Configuration.Global;
using electrifier.Models;
using electrifier.Notifications;
using electrifier.Services;
using electrifier.ViewModels;
using electrifier.Views;

using Microsoft.AppCenter;
using Microsoft.AppCenter.Analytics;
using Microsoft.AppCenter.Crashes;
using Microsoft.AppCenter;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.UI.Xaml;
using System.Diagnostics;
using System.Text;

namespace electrifier;

Expand All @@ -35,15 +35,26 @@ public IHost Host
get;
}

public static T GetService<T>()
public static T? GetService<T>()
where T : class
{
if ((App.Current as App)!.Host.Services.GetService(typeof(T)) is not T service)
try
{
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;
}
catch (Exception ex)
{
throw new ArgumentException($"{typeof(T)} needs to be registered in ConfigureServices within App.xaml.cs.");
Debug.WriteLine(ex.Message);
Debug.WriteLine(ex.StackTrace);
}

return service;
return null;
}

public static WindowEx MainWindow { get; } = new MainWindow();
Expand All @@ -58,72 +69,69 @@ public App()
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<INavigationViewService, NavigationViewService>();
services.AddTransient<IWebViewService, WebViewService>();

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

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

// Views and ViewModels
services.AddTransient<ClipboardPage>();
services.AddTransient<ClipboardViewModel>();
services.AddTransient<DevicesPage>();
services.AddTransient<DevicesViewModel>();
services.AddTransient<FileManagerPage>();
services.AddTransient<FileManagerViewModel>();
services.AddTransient<NetworkDevicesPage>();
services.AddTransient<NetworkDevicesViewModel>();
services.AddTransient<SettingsPage>();
services.AddTransient<SettingsViewModel>();
services.AddTransient<ShellPage>();
services.AddTransient<ShellViewModel>();
services.AddTransient<WebFavoritesPage>();
services.AddTransient<WebFavoritesViewModel>();
services.AddTransient<WebHostsPage>();
services.AddTransient<WebHostsViewModel>();
services.AddTransient<WebViewPage>();
services.AddTransient<WebViewViewModel>();
services.AddTransient<WorkbenchPage>();
services.AddTransient<WorkbenchViewModel>();

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

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

// AppCenter.Start("{Your app secret here}", typeof(Analytics), typeof(Crashes));

// App_UnhandledException(this, new Microsoft.UI.Xaml.UnhandledExceptionEventArgs(this, false));
//System.UnhandledExceptionEventArgs args = new System.UnhandledExceptionEventArgs();

//var v1 = new Microsoft.UI.Xaml.UnhandledExceptionEventArgs();

// UnhandledException.UnhandledException += App_UnhandledException;

{
// 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<INavigationViewService, NavigationViewService>();
services.AddTransient<IWebViewService, WebViewService>();

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

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

// Views and ViewModels
services.AddTransient<ClipboardPage>();
services.AddTransient<ClipboardViewModel>();
services.AddTransient<DevicesPage>();
services.AddTransient<DevicesViewModel>();
services.AddTransient<FileManagerPage>();
services.AddTransient<FileManagerViewModel>();
services.AddTransient<NetworkDevicesPage>();
services.AddTransient<NetworkDevicesViewModel>();
services.AddTransient<SettingsPage>();
services.AddTransient<SettingsViewModel>();
services.AddTransient<ShellPage>();
services.AddTransient<ShellViewModel>();
services.AddTransient<WebFavoritesPage>();
services.AddTransient<WebFavoritesViewModel>();
services.AddTransient<WebHostsPage>();
services.AddTransient<WebHostsViewModel>();
services.AddTransient<WebViewPage>();
services.AddTransient<WebViewViewModel>();
services.AddTransient<WorkbenchPage>();
services.AddTransient<WorkbenchViewModel>();

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

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

UnhandledException += App_UnhandledException;
}

/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs args) => App_UnhandledException(sender, args, false);
//private void App_StartAppCenter()
//{
// AppCenter.Start("{ TODO:Your_app_secret_here }", typeof(Analytics), typeof(Crashes));
//}


private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs args)
{
App_UnhandledException(sender, args, false);
}

/// <summary>
/// Log and handle exceptions as appropriate.
Expand All @@ -139,41 +147,24 @@ public App()
/// <param name="itIsComplicated">
/// Set <b>true</b> to force shutdown in case of critical error.<br/>
/// <br/>
/// Triggers <see cref="UnhandledExceptionEventArgs.Handled"/>.
/// Triggers <see cref="Microsoft.UI.Xaml.UnhandledExceptionEventArgs.Handled"/>.
/// </param>
private void App_UnhandledException(
object sender,
Microsoft.UI.Xaml.UnhandledExceptionEventArgs args,
bool itIsComplicated = false)
bool itIsComplicated)
{
try
{
StringBuilder sb = new();

sb.AppendJoin("\n", "Exception happened!", "line 1", "line 2");

// TODO: Try to make an backup of current configuration and mark as "dirty".
// TODO: Log and handle exceptions as appropriate.

//var guru = App.GetService<GuruMeditationDialoge>();

//guru?.ThrowGuruMeditation(sender, args);



/*
var stringBuilder = new StringBuilder()
.AppendLine("GURU MEDITATION")
.AppendLine("Sender: { }")
.AppendLine(args?.ToString());
*/



//if (args is not null)
//{
// // TODO: Exception happened!

// args.Handled = true;
//}


/*

private async void ShowDialog_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -214,23 +205,25 @@ private async void ShowDialog_Click(object sender, RoutedEventArgs e)
}
catch (Exception ex)
{
ex.ToString();
var dummy = ex.ToString();


// TODO: Log inner exception
}
finally
{
if (args is not null)
{
//args.Handled = !itIsComplicated;
}
args.Handled = true; // TODO: For test purposes only
//args.Handled = !itIsComplicated; // TODO
}
}

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

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

await App.GetService<IActivationService>().ActivateAsync(args);
await GetService<IActivationService>()?.ActivateAsync(args);
}
}
}
2 changes: 1 addition & 1 deletion src/electrifier/Behaviors/NavigationViewHeaderBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,4 @@ private void UpdateHeaderTemplate()
AssociatedObject.HeaderTemplate = headerTemplate ?? DefaultHeaderTemplate;
}
}
}
}
2 changes: 1 addition & 1 deletion src/electrifier/Behaviors/NavigationViewHeaderMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ public enum NavigationViewHeaderMode
Always,
Never,
Minimal
}
}
2 changes: 1 addition & 1 deletion src/electrifier/Contracts/Services/IActivationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
public interface IActivationService
{
Task ActivateAsync(object activationArgs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ public interface IAppNotificationService
NameValueCollection ParseArguments(string arguments);

void Unregister();
}
}
2 changes: 1 addition & 1 deletion src/electrifier/Contracts/Services/IFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ public interface IFileService
void Save<T>(string folderPath, string fileName, T content);

void Delete(string folderPath, string fileName);
}
}
3 changes: 2 additions & 1 deletion src/electrifier/Contracts/Services/ILocalSettingsService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using electrifier.Models;
using electrifier.Models.Configuration.Global;

namespace electrifier.Contracts.Services;

Expand All @@ -13,4 +14,4 @@ public interface ILocalSettingsService
Task SaveSettingAsync<T>(string key, T value);

Task SetGuiLanguageAsync(LocalSettingsOptions.GuiLanguage language);
}
}
2 changes: 1 addition & 1 deletion src/electrifier/Contracts/Services/INavigationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ bool CanGoBack
}

void SetListDataItemForNextConnectedAnimation(object item);
}
}
4 changes: 2 additions & 2 deletions src/electrifier/Contracts/Services/INavigationViewService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface INavigationViewService

void Initialize(NavigationView navigationView);

void UnregisterEvents();
void UnregisterMyEvents();

NavigationViewItem? GetSelectedItem(Type pageType);
}
}
2 changes: 1 addition & 1 deletion src/electrifier/Contracts/Services/IPageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
public interface IPageService
{
Type GetPageType(string key);
}
}
Loading