From 03daf5ffdb472b2397aa10e3f39fd1982e5ad4bd Mon Sep 17 00:00:00 2001 From: joekolodz Date: Mon, 14 Nov 2022 21:34:53 -0600 Subject: [PATCH] sort action catalog items; add comparer interface to actionCatalogItem --- src/Models/ActionCatalog.cs | 70 +++++++++++++++++++++++++++++++-- src/Models/ActionCatalogItem.cs | 10 ++++- 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/src/Models/ActionCatalog.cs b/src/Models/ActionCatalog.cs index 06e6372..2f640f6 100644 --- a/src/Models/ActionCatalog.cs +++ b/src/Models/ActionCatalog.cs @@ -1,19 +1,76 @@ using System; +using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Collections.Specialized; +using System.ComponentModel; using System.Linq; namespace SierraHOTAS.Models { - public class ActionCatalog + public class ActionCatalog : INotifyCollectionChanged { - public ObservableCollection Catalog { get; } + public event NotifyCollectionChangedEventHandler CollectionChanged; + + public ObservableCollection Catalog { get; private set; } public ActionCatalog() { Catalog = new ObservableCollection(); AddEmptyItem(); + + Catalog.CollectionChanged += Catalog_CollectionChanged; + } + + private void Catalog_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) + { + if (e.OldItems != null) + { + foreach (INotifyPropertyChanged item in e.OldItems) + { + item.PropertyChanged -= Item_PropertyChanged; + } + } + + if (e.NewItems != null) + { + foreach (INotifyPropertyChanged item in e.NewItems) + { + item.PropertyChanged += Item_PropertyChanged; + } + } } + private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e) + { + if (sender is ActionCatalogItem item) + { + ReSortItem(item); + } + } + + private void ReSortItem(ActionCatalogItem item) + { + var index = Catalog.IndexOf(item); + Catalog.RemoveAt(index); + Insert(item); + } + + private int GetSortPosition(ActionCatalogItem item) + { + IComparer comparer = Comparer.Default; + var i = 0; + while (i < Catalog.Count && comparer.Compare(Catalog[i], item) <= 0) + { + i++; + } + return i; + } + + //public void Sort() + //{ + // Catalog = Catalog.OrderBy(x => x.ActionName).ToObservableCollection(); + //} + public void Clear() { Catalog.Clear(); @@ -51,9 +108,14 @@ public void Add(ActionCatalogItem item, string buttonName) { item.ActionName = $"Action for {buttonName}"; } + Insert(item); + } - Catalog.Add(item); - Logging.Log.Debug($"{item.ActionName} - {buttonName} added to actions catalog"); + private void Insert(ActionCatalogItem item) + { + var index = GetSortPosition(item); + Catalog.Insert(index, item); + Logging.Log.Debug($"[{item.ActionName}] added to actions catalog at position: {index}"); } public ActionCatalogItem Get(string actionName) diff --git a/src/Models/ActionCatalogItem.cs b/src/Models/ActionCatalogItem.cs index 0cb4153..c8dbf99 100644 --- a/src/Models/ActionCatalogItem.cs +++ b/src/Models/ActionCatalogItem.cs @@ -6,7 +6,7 @@ namespace SierraHOTAS.Models { - public class ActionCatalogItem : INotifyPropertyChanged + public class ActionCatalogItem : INotifyPropertyChanged, IComparable { private string _actionName; public const string NO_ACTION_TEXT = ""; @@ -31,6 +31,14 @@ public string ActionName public event PropertyChangedEventHandler PropertyChanged; + public int CompareTo(ActionCatalogItem other) + { + if (other == null) return 1; + var value = string.CompareOrdinal(other.ActionName, ActionName) * -1; + Logging.Log.Info($"Comparing:'{other.ActionName}' to '{ActionName}': {value}"); + return value; + } + public override string ToString() { return $"{ActionName} - {Actions.Count}";