using System.Collections.Specialized; using ClassicAssist.Data.Hotkeys; using ClassicAssist.Data.Hotkeys.Commands; using ClassicAssist.UI.Misc; using Newtonsoft.Json.Linq; namespace ClassicAssist.UI.ViewModels { public abstract class HotkeyEntryViewModel : BaseViewModel where T : HotkeyEntry { private readonly HotkeyCommand _category; private ObservableCollectionEx _items = new ObservableCollectionEx(); protected HotkeyEntryViewModel( string name ) { _category = new HotkeyCommand { Name = name, IsCategory = true }; HotkeyManager hotkey = HotkeyManager.GetInstance(); hotkey.AddCategory( _category ); Items.CollectionChanged += OnCollectionChanged; _category.Children = new ObservableCollectionEx(); } public ObservableCollectionEx Items { get => _items; set => SetProperty( ref _items, value ); } ~HotkeyEntryViewModel() { Items.CollectionChanged -= OnCollectionChanged; } protected virtual void OnCollectionChanged( object sender, NotifyCollectionChangedEventArgs e ) { _category.Children = new ObservableCollectionEx(); foreach ( T item in Items ) { _category.Children.Add( item ); } } protected void SetJsonValue( JToken json, string name, JToken value ) { json[name] = value; } protected T2 GetJsonValue( JToken json, string name, T2 defaultValue ) { if ( json == null ) { return defaultValue; } return json[name] == null ? defaultValue : json[name].ToObject(); } } }