-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
App.axaml.cs
191 lines (168 loc) · 7.52 KB
/
App.axaml.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System.Globalization;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.DependencyInjection;
using ObjCRuntime;
using SukiUI.Dialogs;
using SukiUI.Toasts;
using SupportCompanion.Helpers;
using SupportCompanion.Models;
using SupportCompanion.Services;
using SupportCompanion.ViewModels;
using SupportCompanion.Views;
namespace SupportCompanion;
public class App : Application
{
public App()
{
RegisterAppServices();
}
public static AppConfiguration Config { get; private set; }
public IServiceProvider ServiceProvider { get; private set; }
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
RegisterAppServices();
var prefs = new AppConfigHelper();
try
{
prefs.SetPrefs();
}
catch (Exception e)
{
var logger = ServiceProvider.GetRequiredService<LoggerService>();
logger.Log("App initialization", "Error loading preferences: " + e.Message, 2);
}
Config = AppConfigHelper.Config;
}
private async Task InitializeCultureAsync()
{
var actionService = ServiceProvider.GetRequiredService<ActionsService>();
var mainViewModel = ServiceProvider.GetRequiredService<MainWindowViewModel>();
try
{
var locale = await actionService.RunCommandWithOutput("defaults read NSGlobalDomain AppleLocale");
locale = locale?.Trim().Replace("_", "-");
if (locale.Contains("@")) locale = locale.Split('@')[0];
if (!string.IsNullOrEmpty(locale))
{
var cultureInfo = new CultureInfo(locale);
Assets.Resources.Culture = cultureInfo;
}
else
{
Assets.Resources.Culture = CultureInfo.CurrentCulture;
}
}
catch (Exception ex)
{
Console.WriteLine($"Failed to set culture: {ex.Message}");
Assets.Resources.Culture = CultureInfo.CurrentCulture;
}
mainViewModel.NativeMenuOpenText = Assets.Resources.Open + " Support Companion";
mainViewModel.NativeMenuSystemUpdatesText = Assets.Resources.NativeMenuSystemUpdates;
mainViewModel.NativeMenuActionsHeader = Assets.Resources.Actions;
mainViewModel.NativeMenuQuitAppText = Assets.Resources.Exit;
}
public override async void OnFrameworkInitializationCompleted()
{
//RegisterAppServices();
await InitializeCultureAsync();
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.ShutdownMode = ShutdownMode.OnExplicitShutdown;
DataContext = ServiceProvider.GetRequiredService<MainWindowViewModel>();
if (Config.IntuneMode)
Config.MunkiMode = false;
if (Config.AppProfilerMode)
{
Config.MunkiMode = false;
Config.IntuneMode = false;
}
var updateNotifications = ServiceProvider.GetRequiredService<UpdateNotifications>();
// Register the URL handler
var urlHandler = new UrlHandler(desktop);
NSAppleEventManager.SharedAppleEventManager.SetEventHandler(urlHandler,
new Selector("handleGetURLEvent:withReplyEvent:"),
AEEventClass.Internet, AEEventID.GetUrl);
if (Config.ShowDesktopInfo)
{
var transparentWindow = new TransparentWindow();
transparentWindow.Show();
}
}
if (Config.Actions != null && Config.Actions.Count > 0)
{
var mainViewModel = ServiceProvider.GetRequiredService<MainWindowViewModel>();
// Create the main "Actions" menu item
var actionsMenuItem = new NativeMenuItem { Header = mainViewModel.NativeMenuActionsHeader };
actionsMenuItem.Menu = new NativeMenu();
// Iterate over the Config.Actions and add them as sub-items
foreach (var action in Config.Actions)
if (action.Value.TryGetValue("Name", out var name) &&
action.Value.TryGetValue("Command", out var command))
{
var subItem = new NativeMenuItem
{
Header = name,
Command = new RelayCommand(() =>
{
var actionsService = ServiceProvider.GetRequiredService<ActionsService>();
actionsService.RunCommandWithoutOutput(command);
})
};
actionsMenuItem.Menu.Items.Add(subItem);
}
// Insert the main "Actions" menu item at the third position (index 2)
var trayIcon = TrayIcon.GetIcons(this).First();
if (trayIcon.Menu.Items.Count > 2)
trayIcon.Menu.Items.Insert(2, actionsMenuItem);
else
trayIcon.Menu.Items.Add(actionsMenuItem);
}
base.OnFrameworkInitializationCompleted();
}
private void RegisterAppServices()
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton<IOKitService>();
serviceCollection.AddSingleton<SystemInfoService>();
serviceCollection.AddSingleton<MunkiAppsService>();
serviceCollection.AddSingleton<StorageService>();
serviceCollection.AddSingleton<MdmStatusService>();
serviceCollection.AddSingleton<CatalogsService>();
serviceCollection.AddSingleton<ActionsService>();
serviceCollection.AddSingleton<ClipboardService>();
serviceCollection.AddSingleton<NotificationService>();
serviceCollection.AddSingleton<IntuneAppsService>();
serviceCollection.AddSingleton<MacPasswordService>();
serviceCollection.AddSingleton<UpdateNotifications>();
serviceCollection.AddSingleton<LoggerService>();
serviceCollection.AddSingleton<NotificationService>();
serviceCollection.AddSingleton<ProfilerApplications>();
serviceCollection.AddSingleton<ISukiToastManager, SukiToastManager>();
serviceCollection.AddSingleton<ISukiDialogManager, SukiDialogManager>();
serviceCollection.AddSingleton<DeviceWidgetViewModel>();
serviceCollection.AddSingleton<MunkiPendingAppsViewModel>();
serviceCollection.AddSingleton<MunkiUpdatesViewModel>();
serviceCollection.AddSingleton<StorageViewModel>();
serviceCollection.AddSingleton<MdmStatusViewModel>();
serviceCollection.AddSingleton<EvergreenWidgetViewModel>();
serviceCollection.AddSingleton<ActionsViewModel>();
serviceCollection.AddSingleton<BatteryWidgetViewModel>();
serviceCollection.AddSingleton<ApplicationsViewModel>();
serviceCollection.AddSingleton<MainWindowViewModel>();
serviceCollection.AddSingleton<SupportDialogViewModel>();
serviceCollection.AddSingleton<IntuneUpdatesViewModel>();
serviceCollection.AddSingleton<IntunePendingAppsViewModel>();
serviceCollection.AddSingleton<UserViewModel>();
serviceCollection.AddSingleton<MacPasswordViewModel>();
serviceCollection.AddSingleton<TransparentWindowViewModel>();
serviceCollection.AddSingleton<CustomWidgetsBaseViewModel>();
serviceCollection.AddSingleton<SelfServiceViewModel>();
ServiceProvider = serviceCollection.BuildServiceProvider();
}
}