-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathApp.xaml.cs
144 lines (123 loc) · 5.44 KB
/
App.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* RyTuneX - A Windows 10 and 11 optimizer designed to enhance system performance.
* Copyright (C) 2023 Rayen Ghanmi
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* Contact: ghanmirayen12@gmail.com
*/
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml;
using RyTuneX.Activation;
using RyTuneX.Contracts.Services;
using RyTuneX.Core.Contracts.Services;
using RyTuneX.Core.Services;
using RyTuneX.Helpers;
using RyTuneX.Models;
using RyTuneX.Notifications;
using RyTuneX.Services;
using RyTuneX.ViewModels;
using RyTuneX.Views;
using Windows.Storage;
namespace RyTuneX;
public partial class App : Application
{
public IHost Host
{
get;
}
public static void ShowNotification(string title, string message, Microsoft.UI.Xaml.Controls.InfoBarSeverity severity, int duration) =>
ShellPage.ShowNotification(title, message, severity, duration);
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();
LogHelper.Log("___________ New Session ___________");
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<INavigationViewService, NavigationViewService>();
services.AddSingleton<IActivationService, ActivationService>();
services.AddSingleton<IPageService, PageService>();
services.AddSingleton<INavigationService, NavigationService>();
// Core Services
services.AddSingleton<IFileService, FileService>();
// Views and ViewModels
services.AddTransient<SettingsPage>();
services.AddTransient<OptimizeSystemPage>();
services.AddTransient<SystemInfoPage>();
services.AddTransient<DebloatSystemPage>();
services.AddTransient<HomePage>();
services.AddTransient<NetworkPage>();
services.AddTransient<SecurityPage>();
services.AddTransient<RepairPage>();
services.AddTransient<ShellPage>();
services.AddTransient<ShellViewModel>();
// Configuration
services.Configure<LocalSettingsOptions>(context.Configuration.GetSection(nameof(LocalSettingsOptions)));
}).
Build();
//App.GetService<IAppNotificationService>().Initialize();
}
protected async override void OnLaunched(LaunchActivatedEventArgs args)
{
base.OnLaunched(args);
// setting custom title bar when the app starts to prevent it from briefly show the standard titlebar
MainWindow.AppWindow.TitleBar.ExtendsContentIntoTitleBar = true;
MainWindow.AppWindow.TitleBar.ButtonBackgroundColor = Microsoft.UI.Colors.Transparent;
if (ApplicationData.Current.LocalSettings.Values.TryGetValue("JustUpdated", out var value))
{
ApplicationData.Current.LocalSettings.Values["JustUpdated"] = false;
if ((bool)value == true)
{
await OptimizationOptions.StartInCmd("rd /S /Q \"%TEMP%\"");
if (ApplicationData.Current.LocalSettings.Values.TryGetValue("DoneUpdating", out var isDoneUpdating)
&& ApplicationData.Current.LocalSettings.Values.TryGetValue("latestChanges", out var latestChanges))
{
App.MainWindow.DispatcherQueue.TryEnqueue(DispatcherQueuePriority.Low, () =>
{
App.MainWindow.ShowMessageDialogAsync((string)latestChanges, "UpdateTitle".GetLocalized());
});
ApplicationData.Current.LocalSettings.Values["DoneUpdating"] = false;
}
}
}
await App.GetService<IActivationService>().ActivateAsync(args);
}
}