SortableBindingList2 => Dinah.Core SortableBindingList

This commit is contained in:
Robert McRackan 2021-08-18 15:06:52 -04:00
parent 2e60d2accf
commit 012a92ea30
7 changed files with 22 additions and 126 deletions

View File

@ -13,7 +13,7 @@
<!-- <PublishSingleFile>true</PublishSingleFile> --> <!-- <PublishSingleFile>true</PublishSingleFile> -->
<RuntimeIdentifier>win-x64</RuntimeIdentifier> <RuntimeIdentifier>win-x64</RuntimeIdentifier>
<Version>5.5.0.1</Version> <Version>5.5.0.9</Version>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

View File

@ -1,14 +1,15 @@
using ApplicationServices; using System;
using DataLayer;
using InternalUtilities;
using LibationWinForms.Login;
using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Data; using System.Data;
using System.Linq; using System.Linq;
using System.Windows.Forms; using System.Windows.Forms;
using ApplicationServices;
using DataLayer;
using Dinah.Core.DataBinding;
using InternalUtilities;
using LibationWinForms.Login;
namespace LibationWinForms.Dialogs namespace LibationWinForms.Dialogs
{ {
@ -18,7 +19,7 @@ namespace LibationWinForms.Dialogs
private Account[] _accounts { get; } private Account[] _accounts { get; }
private readonly List<LibraryBook> _libraryBooks; private readonly List<LibraryBook> _libraryBooks;
private readonly SortableBindingList2<RemovableGridEntry> _removableGridEntries; private readonly SortableBindingList<RemovableGridEntry> _removableGridEntries;
private readonly string _labelFormat; private readonly string _labelFormat;
private int SelectedCount => SelectedEntries?.Count() ?? 0; private int SelectedCount => SelectedEntries?.Count() ?? 0;
private IEnumerable<RemovableGridEntry> SelectedEntries => _removableGridEntries?.Where(b => b.Remove); private IEnumerable<RemovableGridEntry> SelectedEntries => _removableGridEntries?.Where(b => b.Remove);
@ -40,7 +41,7 @@ namespace LibationWinForms.Dialogs
.OrderByDescending(ge => (DateTime)ge.GetMemberValue(nameof(ge.PurchaseDate))) .OrderByDescending(ge => (DateTime)ge.GetMemberValue(nameof(ge.PurchaseDate)))
.ToList(); .ToList();
_removableGridEntries = new SortableBindingList2<RemovableGridEntry>(orderedGridEntries); _removableGridEntries = new SortableBindingList<RemovableGridEntry>(orderedGridEntries);
gridEntryBindingSource.DataSource = _removableGridEntries; gridEntryBindingSource.DataSource = _removableGridEntries;
_dataGridView.Enabled = false; _dataGridView.Enabled = false;

View File

@ -1,12 +1,13 @@
using ApplicationServices; using System;
using DataLayer;
using Dinah.Core.Drawing;
using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using ApplicationServices;
using DataLayer;
using Dinah.Core.DataBinding;
using Dinah.Core.Drawing;
namespace LibationWinForms namespace LibationWinForms
{ {

View File

@ -1,11 +0,0 @@
using System;
using System.Collections;
namespace LibationWinForms
{
internal interface IMemberComparable
{
IComparer GetMemberComparer(Type memberType);
object GetMemberValue(string memberName);
}
}

View File

@ -1,21 +0,0 @@
using System.Collections.Generic;
using System.ComponentModel;
namespace LibationWinForms
{
internal class MemberComparer<T> : IComparer<T> where T : IMemberComparable
{
public ListSortDirection Direction { get; set; } = ListSortDirection.Ascending;
public string PropertyName { get; set; }
public int Compare(T x, T y)
{
var val1 = x.GetMemberValue(PropertyName);
var val2 = y.GetMemberValue(PropertyName);
return DirMult * x.GetMemberComparer(val1.GetType()).Compare(val1, val2);
}
private int DirMult => Direction == ListSortDirection.Descending ? -1 : 1;
}
}

View File

@ -1,12 +1,13 @@
using ApplicationServices; using System;
using DataLayer;
using Dinah.Core;
using Dinah.Core.Windows.Forms;
using LibationWinForms.Dialogs;
using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using ApplicationServices;
using DataLayer;
using Dinah.Core;
using Dinah.Core.DataBinding;
using Dinah.Core.Windows.Forms;
using LibationWinForms.Dialogs;
namespace LibationWinForms namespace LibationWinForms
{ {
@ -138,7 +139,7 @@ namespace LibationWinForms
.ToList(); .ToList();
// BIND // BIND
gridEntryBindingSource.DataSource = new SortableBindingList2<GridEntry>(orderedGridEntries); gridEntryBindingSource.DataSource = new SortableBindingList<GridEntry>(orderedGridEntries);
// FILTER // FILTER
Filter(); Filter();

View File

@ -1,75 +0,0 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
namespace LibationWinForms
{
internal class SortableBindingList2<T> : BindingList<T> where T : IMemberComparable
{
private bool isSorted;
private ListSortDirection listSortDirection;
private PropertyDescriptor propertyDescriptor;
public SortableBindingList2() : base(new List<T>()) { }
public SortableBindingList2(IEnumerable<T> enumeration) : base(new List<T>(enumeration)) { }
private MemberComparer<T> Comparer { get; } = new();
protected override bool SupportsSortingCore => true;
protected override bool SupportsSearchingCore => true;
protected override bool IsSortedCore => isSorted;
protected override PropertyDescriptor SortPropertyCore => propertyDescriptor;
protected override ListSortDirection SortDirectionCore => listSortDirection;
protected override void ApplySortCore(PropertyDescriptor property, ListSortDirection direction)
{
List<T> itemsList = (List<T>)Items;
Comparer.PropertyName = property.Name;
Comparer.Direction = direction;
//Array.Sort() and List<T>.Sort() are unstable sorts. OrderBy is stable.
var sortedItems = itemsList.OrderBy((ge) => ge, Comparer).ToList();
itemsList.Clear();
itemsList.AddRange(sortedItems);
propertyDescriptor = property;
listSortDirection = direction;
isSorted = true;
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}
protected override void RemoveSortCore()
{
isSorted = false;
propertyDescriptor = base.SortPropertyCore;
listSortDirection = base.SortDirectionCore;
OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}
//NOTE: Libation does not currently use BindingSource.Find anywhere,
//so this override may be removed (along with SupportsSearchingCore)
protected override int FindCore(PropertyDescriptor property, object key)
{
int count = Count;
System.Collections.IComparer valueComparer = null;
for (int i = 0; i < count; ++i)
{
T element = this[i];
var elemValue = element.GetMemberValue(property.Name);
valueComparer ??= element.GetMemberComparer(elemValue.GetType());
if (valueComparer.Compare(elemValue, key) == 0)
{
return i;
}
}
return -1;
}
}
}