/ src / modules / cmdpal / Microsoft.CmdPal.UI.ViewModels / FallbackSettingsViewModel.cs
FallbackSettingsViewModel.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using CommunityToolkit.Mvvm.ComponentModel;
 6  using CommunityToolkit.Mvvm.Messaging;
 7  using Microsoft.CmdPal.Core.ViewModels;
 8  using Microsoft.CmdPal.UI.ViewModels.Messages;
 9  
10  namespace Microsoft.CmdPal.UI.ViewModels;
11  
12  public partial class FallbackSettingsViewModel : ObservableObject
13  {
14      private readonly SettingsModel _settings;
15      private readonly FallbackSettings _fallbackSettings;
16  
17      public string DisplayName { get; private set; } = string.Empty;
18  
19      public IconInfoViewModel Icon { get; private set; } = new(null);
20  
21      public string Id { get; private set; } = string.Empty;
22  
23      public bool IsEnabled
24      {
25          get => _fallbackSettings.IsEnabled;
26          set
27          {
28              if (value != _fallbackSettings.IsEnabled)
29              {
30                  _fallbackSettings.IsEnabled = value;
31  
32                  if (!_fallbackSettings.IsEnabled)
33                  {
34                      _fallbackSettings.IncludeInGlobalResults = false;
35                  }
36  
37                  Save();
38                  OnPropertyChanged(nameof(IsEnabled));
39              }
40          }
41      }
42  
43      public bool IncludeInGlobalResults
44      {
45          get => _fallbackSettings.IncludeInGlobalResults;
46          set
47          {
48              if (value != _fallbackSettings.IncludeInGlobalResults)
49              {
50                  _fallbackSettings.IncludeInGlobalResults = value;
51  
52                  if (!_fallbackSettings.IsEnabled)
53                  {
54                      _fallbackSettings.IsEnabled = true;
55                  }
56  
57                  Save();
58                  OnPropertyChanged(nameof(IncludeInGlobalResults));
59              }
60          }
61      }
62  
63      public FallbackSettingsViewModel(
64      TopLevelViewModel fallback,
65      FallbackSettings fallbackSettings,
66      SettingsModel settingsModel,
67      ProviderSettingsViewModel providerSettings)
68      {
69          _settings = settingsModel;
70          _fallbackSettings = fallbackSettings;
71  
72          Id = fallback.Id;
73          DisplayName = string.IsNullOrWhiteSpace(fallback.DisplayTitle)
74              ? (string.IsNullOrWhiteSpace(fallback.Title) ? providerSettings.DisplayName : fallback.Title)
75              : fallback.DisplayTitle;
76  
77          Icon = new(fallback.InitialIcon);
78          Icon.InitializeProperties();
79      }
80  
81      private void Save()
82      {
83          SettingsModel.SaveSettings(_settings);
84          WeakReferenceMessenger.Default.Send<ReloadCommandsMessage>(new());
85      }
86  }