FilterItemViewModel.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 Microsoft.CmdPal.Core.ViewModels.Models; 6 using Microsoft.CommandPalette.Extensions; 7 8 namespace Microsoft.CmdPal.Core.ViewModels; 9 10 public partial class FilterItemViewModel : ExtensionObjectViewModel, IFilterItemViewModel 11 { 12 private readonly ExtensionObject<IFilter> _model; 13 14 public string Id { get; set; } = string.Empty; 15 16 public string Name { get; set; } = string.Empty; 17 18 public IconInfoViewModel Icon { get; set; } = new(null); 19 20 internal InitializedState Initialized { get; private set; } = InitializedState.Uninitialized; 21 22 protected bool IsInitialized => IsInErrorState || Initialized.HasFlag(InitializedState.Initialized); 23 24 public bool IsInErrorState => Initialized.HasFlag(InitializedState.Error); 25 26 public FilterItemViewModel(IFilter filter, WeakReference<IPageContext> context) 27 : base(context) 28 { 29 _model = new(filter); 30 } 31 32 public override void InitializeProperties() 33 { 34 if (IsInitialized) 35 { 36 return; 37 } 38 39 var filter = _model.Unsafe; 40 if (filter == null) 41 { 42 return; // throw? 43 } 44 45 Id = filter.Id; 46 Name = filter.Name; 47 Icon = new(filter.Icon); 48 if (Icon is not null) 49 { 50 Icon.InitializeProperties(); 51 } 52 53 UpdateProperty(nameof(Id)); 54 UpdateProperty(nameof(Name)); 55 UpdateProperty(nameof(Icon)); 56 } 57 }