Utopia-Canteen-System/UtopiaCanteen.Client/App.xaml.cs

66 lines
2.8 KiB
C#

using System.Net.Http;
using System.Windows;
using Microsoft.EntityFrameworkCore;
using UtopiaCanteenSystem;
using UtopiaCanteenSystem.Data;
using UtopiaCanteenSystem.Services;
using UtopiaCanteen.Client.ViewModels;
using UtopiaCanteenSystem.ViewModels;
namespace UtopiaCanteen.Client;
public partial class App : Application
{
private static Mutex _mutex = null!;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
bool isNewInstance;
_mutex = new Mutex(true, "UtopiaCanteenClientMutex", out isNewInstance);
if (!isNewInstance)
{
MessageBox.Show("Another instance of the scanner client is already running.", "Warning",
MessageBoxButton.OK, MessageBoxImage.Warning);
Current.Shutdown();
return;
}
var configService = new ClientConfigService();
var httpClient = new HttpClient { Timeout = TimeSpan.FromMinutes(5) };
var backendApiClient = new CanteenBackendApiClient(httpClient, configService);
var dbFactory = new DbContextFactory();
using (var db = dbFactory.CreateDbContext())
db.EnsureDatabaseCreated();
var employeeLookupService = new EmployeeLookupService(dbFactory, configService);
var menuLookupService = new EmptyMenuLookupService();
var employeePhotoService = new NoOpEmployeePhotoService();
var adminAuditService = new AdminAuditService(dbFactory);
var session = new AppSession();
var authService = new AuthService("https://portal.utopiaindustries.pk/uind/rest/auth/user/");
NavigationService navigationService = null!;
navigationService = new NavigationService(
session,
() => new AdminLoginViewModel(authService, session, navigationService, configService, adminAuditService, employeeLookupService, backendApiClient),
() => new ScannerDashboardViewModel(backendApiClient, navigationService, session, configService, menuLookupService, employeePhotoService),
() => new MainDashboardViewModel(navigationService, backendApiClient, configService, session),
() => new AdminSettingsAuthViewModel(authService, session, navigationService, configService, employeeLookupService, backendApiClient),
() => new ClientSettingsViewModel(configService, navigationService, backendApiClient, session),
() => new MealSchedulesViewModel(new BackendApiMealScheduleService(backendApiClient), navigationService, configService));
var mainWindow = new MainWindow { DataContext = new MainViewModel(navigationService) };
mainWindow.WindowState = WindowState.Maximized;
mainWindow.Show();
}
protected override void OnExit(ExitEventArgs e)
{
_mutex?.ReleaseMutex();
base.OnExit(e);
}
}