| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | |
| 3 | using CommunityToolkit.Mvvm.ComponentModel; |
| 4 | using Microsoft.UI.Xaml.Controls; |
| 5 | using WslSettings.Contracts.Services; |
| 6 | using WslSettings.ViewModels.OOBE; |
| 7 | using WslSettings.ViewModels.Settings; |
| 8 | using WslSettings.Views.OOBE; |
| 9 | using WslSettings.Views.Settings; |
| 10 | |
| 11 | namespace WslSettings.Services; |
| 12 | |
| 13 | public class PageService : IPageService |
| 14 | { |
| 15 | private readonly Dictionary<string, Type> _pages = new(); |
| 16 | |
| 17 | public PageService() |
| 18 | { |
| 19 | Configure<MemAndProcViewModel, MemAndProcPage>(); |
| 20 | Configure<FileSystemViewModel, FileSystemPage>(); |
| 21 | Configure<NetworkingViewModel, NetworkingPage>(); |
| 22 | Configure<OptionalFeaturesViewModel, OptionalFeaturesPage>(); |
| 23 | Configure<DeveloperViewModel, DeveloperPage>(); |
| 24 | Configure<AboutViewModel, AboutPage>(); |
| 25 | |
| 26 | Configure<GeneralViewModel, GeneralPage>(); |
| 27 | Configure<WorkingAcrossFileSystemsViewModel, WorkingAcrossFileSystemsPage>(); |
| 28 | Configure<VSCodeIntegrationViewModel, VSCodeIntegrationPage>(); |
| 29 | Configure<VSIntegrationViewModel, VSIntegrationPage>(); |
| 30 | Configure<GUIAppsViewModel, GUIAppsPage>(); |
| 31 | Configure<GPUAccelerationViewModel, GPUAccelerationPage>(); |
| 32 | Configure<DockerDesktopIntegrationViewModel, DockerDesktopIntegrationPage>(); |
| 33 | Configure<NetworkingIntegrationViewModel, NetworkingIntegrationPage>(); |
| 34 | Configure<DistroManagementViewModel, DistroManagementPage>(); |
| 35 | } |
| 36 | |
| 37 | public Type GetPageType(string key) |
| 38 | { |
| 39 | Type? pageType; |
| 40 | lock (_pages) |
| 41 | { |
| 42 | if (!_pages.TryGetValue(key, out pageType)) |
| 43 | { |
| 44 | throw new ArgumentException($"Page not found: {key}. Did you forget to call PageService.Configure?"); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | return pageType; |
| 49 | } |
| 50 | |
| 51 | private void Configure<VM, V>() |
| 52 | where VM : ObservableObject |
| 53 | where V : Page |
| 54 | { |
| 55 | lock (_pages) |
| 56 | { |
| 57 | var key = typeof(VM).FullName!; |
| 58 | if (_pages.ContainsKey(key)) |
| 59 | { |
| 60 | throw new ArgumentException($"The key {key} is already configured in PageService"); |
| 61 | } |
| 62 | |
| 63 | var type = typeof(V); |
| 64 | if (_pages.ContainsValue(type)) |
| 65 | { |
| 66 | throw new ArgumentException($"This type is already configured with key {_pages.First(p => p.Value == type).Key}"); |
| 67 | } |
| 68 | |
| 69 | _pages.Add(key, type); |
| 70 | } |
| 71 | } |
| 72 | } |