Files
WinWARP/WinWARP.Ui/App.xaml.cs
T

147 lines
5.5 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.ApplicationSettings;
using WinUI3Localizer;
using WinWARP.Core.ViewModels;
using WinWARP.Core.ViewModels.ConfigSettings;
using WinWARP.Core.ViewModels.Settings;
using WinWARP.Core.Data;
using WinWARP.Ui.Services;
using WinWARP.Ui.Views;
using WinWARP.Ui.Views.Settings;
using WinWARP.Core.Services.Implementations;
using WinWARP.Core.Services.Interfaces;
using System.Diagnostics;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace WinWARP.Ui;
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
public partial class App : Application
{
public static IServiceProvider Services
{
get;
private set;
}
private Window? _window;
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
InitializeComponent();
Services = ConfigureServices();
using var context = Services.GetRequiredService<IDbContextFactory<AppDbContext>>().CreateDbContext();
context.Database.EnsureCreated();
}
/// <summary>
/// Configures the services for dependency injection.
/// </summary>
/// <returns></returns>
private static ServiceProvider ConfigureServices()
{
var services = new ServiceCollection();
// DB
Debug.WriteLine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "WinWARP", "app.db"));
services.AddDbContextFactory<AppDbContext>(options =>
{
var folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "WinWARP");
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
var dbPath = Path.Combine(folder, "app.db");
options.UseSqlite($"Data Source={dbPath}");
});
services.AddTransient<ISubscriptionDbService, SubscriptionDbService>();
// ViewModels
services.AddTransient<HomeViewModel>();
services.AddTransient<SettingsViewModel>();
services.AddTransient<SubsSettingsViewModel>();
services.AddTransient<ConfigSettingsViewModel>();
services.AddSingleton<MainViewModel>();
services.AddTransient<DnsSettingsViewModel>();
services.AddTransient<GeneralSettingsViewModel>();
services.AddTransient<InboundSettingsViewModel>();
services.AddTransient<RoutingSettingsViewModel>();
services.AddTransient<TlsSettingsViewModel>();
services.AddTransient<WarpSettingsViewModel>();
// Views
services.AddTransient<MainWindow>();
// UI services
var navigationService = new NavigationService();
navigationService.Configure(typeof(HomeViewModel), typeof(HomePage));
navigationService.Configure(typeof(SettingsViewModel), typeof(SettingsPage));
navigationService.Configure(typeof(SubsSettingsViewModel), typeof(SubsSettingsPage));
navigationService.Configure(typeof(ConfigSettingsViewModel), typeof(ConfigSettingsPage));
navigationService.Configure(typeof(GeneralSettingsViewModel), typeof(GeneralSettingsPage));
navigationService.Configure(typeof(UpdateSettingsViewModel), typeof(UpdateSettingsPage));
navigationService.Configure(typeof(InboundSettingsViewModel), typeof(InboundSettingsPage));
navigationService.Configure(typeof(RoutingSettingsViewModel), typeof(RoutingSettingsPage));
navigationService.Configure(typeof(DnsSettingsViewModel), typeof(DnsSettingsPage));
navigationService.Configure(typeof(TlsSettingsViewModel), typeof(TlsSettingsPage));
navigationService.Configure(typeof(WarpSettingsViewModel), typeof(WarpSettingsPage));
services.AddSingleton<INavigationService>(navigationService);
services.AddSingleton<ILocalizationService, LocalizationService>();
return services.BuildServiceProvider();
}
/// <summary>
/// Invoked when the application is launched.
/// </summary>
/// <param name="args">Details about the launch request and process.</param>
protected async override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
var stringsFolderPath = System.IO.Path.Combine(AppContext.BaseDirectory, "Strings");
var localizer = await new LocalizerBuilder()
.AddStringResourcesFolderForLanguageDictionaries(stringsFolderPath)
.SetOptions(options =>
{
options.DefaultLanguage = "en-US";
})
.Build();
_window = Services.GetRequiredService<MainWindow>();
_window.Activate();
}
}