Skip to content

Commit

Permalink
sort action catalog items; add comparer interface to actionCatalogItem
Browse files Browse the repository at this point in the history
  • Loading branch information
joekolodz committed Nov 15, 2022
1 parent 6af9d1f commit 03daf5f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
70 changes: 66 additions & 4 deletions src/Models/ActionCatalog.cs
Original file line number Diff line number Diff line change
@@ -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<ActionCatalogItem> Catalog { get; }
public event NotifyCollectionChangedEventHandler CollectionChanged;

public ObservableCollection<ActionCatalogItem> Catalog { get; private set; }

public ActionCatalog()
{
Catalog = new ObservableCollection<ActionCatalogItem>();
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<ActionCatalogItem> comparer = Comparer<ActionCatalogItem>.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();
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion src/Models/ActionCatalogItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace SierraHOTAS.Models
{
public class ActionCatalogItem : INotifyPropertyChanged
public class ActionCatalogItem : INotifyPropertyChanged, IComparable<ActionCatalogItem>
{
private string _actionName;
public const string NO_ACTION_TEXT = "<No Action>";
Expand All @@ -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}";
Expand Down

0 comments on commit 03daf5f

Please sign in to comment.