Skip to content

Commit

Permalink
Merge pull request #40 from macadmins/dev
Browse files Browse the repository at this point in the history
v1.0.5
  • Loading branch information
almenscorner authored Jun 10, 2024
2 parents c6b14d2 + 43302ec commit 0f1b49e
Show file tree
Hide file tree
Showing 11 changed files with 510 additions and 180 deletions.
275 changes: 136 additions & 139 deletions App.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
Expand All @@ -13,164 +10,164 @@
using SupportCompanion.Services;
using SupportCompanion.ViewModels;

namespace SupportCompanion;

public class App : Application
namespace SupportCompanion
{
public App()
{
RegisterAppServices();
}

public static AppConfiguration Config { get; private set; }
public IServiceProvider ServiceProvider { get; private set; }

public override void Initialize()
public partial class App : Application
{
AvaloniaXamlLoader.Load(this);
var prefs = new AppConfigHelper();
try
{
prefs.SetPrefs();
}
catch (Exception e)
public App()
{
var logger = ServiceProvider.GetRequiredService<LoggerService>();
logger.Log("App initialization", "Error loading preferences: " + e.Message, 2);
RegisterAppServices();
}

Config = AppConfigHelper.Config;
}

private async Task InitializeCultureAsync()
{
var actionService = ServiceProvider.GetRequiredService<ActionsService>();
var mainViewModel = ServiceProvider.GetRequiredService<MainWindowViewModel>();
public static AppConfiguration Config { get; private set; }
public IServiceProvider ServiceProvider { get; private set; }

try
public override void Initialize()
{
// Run the command asynchronously and await the result
var locale = await actionService.RunCommandWithOutput("defaults read NSGlobalDomain AppleLocale");

// Verify and trim the locale string
locale = locale?.Trim().Replace("_", "-");
// Remove unsupported parts from the locale string
if (locale.Contains("@"))
AvaloniaXamlLoader.Load(this);
var prefs = new AppConfigHelper();
try
{
locale = locale.Split('@')[0];
prefs.SetPrefs();
}

if (!string.IsNullOrEmpty(locale))
catch (Exception e)
{
// Dynamically set the culture based on the macOS locale
var cultureInfo = new CultureInfo(locale);
Assets.Resources.Culture = cultureInfo;
var logger = ServiceProvider.GetRequiredService<LoggerService>();
logger.Log("App initialization", "Error loading preferences: " + e.Message, 2);
}
else
// Fallback to a default culture if locale is empty
Assets.Resources.Culture = CultureInfo.CurrentCulture;

Config = AppConfigHelper.Config;
}
catch (Exception ex)

private async Task InitializeCultureAsync()
{
// Handle exceptions (log them, set a default culture, etc.)
Console.WriteLine($"Failed to set culture: {ex.Message}");
Assets.Resources.Culture = CultureInfo.CurrentCulture; // or set a default culture
}
var actionService = ServiceProvider.GetRequiredService<ActionsService>();
var mainViewModel = ServiceProvider.GetRequiredService<MainWindowViewModel>();

// Update localized strings for menu items
mainViewModel.NativeMenuOpenText = Assets.Resources.Open + " Support Companion";
mainViewModel.NativeMenuSystemUpdatesText = Assets.Resources.NativeMenuSystemUpdates;
mainViewModel.NativeMenuActionsHeader = Assets.Resources.Actions;
mainViewModel.NativeMenuQuitAppText = Assets.Resources.Exit;
}
try
{
var locale = await actionService.RunCommandWithOutput("defaults read NSGlobalDomain AppleLocale");
locale = locale?.Trim().Replace("_", "-");
if (locale.Contains("@"))
{
locale = locale.Split('@')[0];
}

public override async void OnFrameworkInitializationCompleted()
{
RegisterAppServices();
await InitializeCultureAsync(); // Ensure the culture is set before proceeding
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;
}

if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
DataContext = ServiceProvider.GetRequiredService<MainWindowViewModel>();
desktop.ShutdownMode = ShutdownMode.OnExplicitShutdown;
if (Config.IntuneMode)
Config.MunkiMode = false;
var updateNotifications = ServiceProvider.GetRequiredService<UpdateNotifications>();
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();

DataContext = ServiceProvider.GetRequiredService<MainWindowViewModel>();
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.ShutdownMode = ShutdownMode.OnExplicitShutdown;

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
DataContext = ServiceProvider.GetRequiredService<MainWindowViewModel>();
if (Config.IntuneMode)
Config.MunkiMode = false;
var updateNotifications = ServiceProvider.GetRequiredService<UpdateNotifications>();

// Register the URL handler
var urlHandler = new UrlHandler(desktop);
NSAppleEventManager.SharedAppleEventManager.SetEventHandler(urlHandler,
new ObjCRuntime.Selector("handleGetURLEvent:withReplyEvent:"),
AEEventClass.Internet, AEEventID.GetUrl);

//desktop.MainWindow = new MainWindow { DataContext = DataContext };
}

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))
{
Header = name,
Command = new RelayCommand(() =>
var subItem = new NativeMenuItem
{
var actionsService = ServiceProvider.GetRequiredService<ActionsService>();
actionsService.RunCommandWithoutOutput(command);
})
};
actionsMenuItem.Menu.Items.Add(subItem);
}
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);
}

// 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();
}

base.OnFrameworkInitializationCompleted();
}

private void RegisterAppServices()
{
var serviceCollection = new ServiceCollection();

// Register your services
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>();

// Register view models
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>();

ServiceProvider = serviceCollection.BuildServiceProvider();
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<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>();

ServiceProvider = serviceCollection.BuildServiceProvider();
}
}
}
}
Loading

0 comments on commit 0f1b49e

Please sign in to comment.