Skip to content

Commit

Permalink
use HashSet where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Miller committed Jan 24, 2024
1 parent ac13c2c commit 6a2f841
Showing 1 changed file with 43 additions and 10 deletions.
53 changes: 43 additions & 10 deletions Maui.DataGrid/DataGrid.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public partial class DataGrid
private readonly object _reloadLock = new();
private readonly object _sortAndPaginateLock = new();
private DataGridColumn? _sortedColumn;
private static HashSet<object>? _internalItemsHashSet;

#endregion Fields

Expand Down Expand Up @@ -405,6 +406,8 @@ private void SortAndPaginate(SortData? sortData = null)
return;
}

_internalItemsHashSet = null;

// Unsubscribe from old collection's change event
if (o is INotifyCollectionChanged oldCollection)
{
Expand All @@ -420,7 +423,7 @@ private void SortAndPaginate(SortData? sortData = null)
self.SortAndPaginate();

// Reset SelectedItem if it's not in the new collection
if (self.SelectedItem != null && self.InternalItems?.Contains(self.SelectedItem) != true)
if (self.SelectedItem != null && self.GetInternalItems().Contains(self.SelectedItem) != true)
{
self.SelectedItem = null;
}
Expand Down Expand Up @@ -542,7 +545,7 @@ private void SortAndPaginate(SortData? sortData = null)
return null;
}

if (self.InternalItems.Contains(v))
if (self.GetInternalItems().Contains(v))
{
return v;
}
Expand Down Expand Up @@ -571,9 +574,11 @@ private void SortAndPaginate(SortData? sortData = null)
return null;
}

var internalItems = self.GetInternalItems(v.Count);

foreach (var selectedItem in v)
{
if (!self.InternalItems.Contains(selectedItem))
if (!internalItems.Contains(selectedItem))
{
return null;
}
Expand Down Expand Up @@ -1288,31 +1293,59 @@ private void OnItemsSourceCollectionChanged(object? sender, NotifyCollectionChan
{
SortAndPaginate();

ICollection<object> internalItems;

switch (SelectionMode)
{
case SelectionMode.Single:
if (SelectedItem != null && InternalItems?.Contains(SelectedItem) != true)
if (SelectedItem == null)
{
break;
}

internalItems = GetInternalItems(SelectedItems.Count);

if (internalItems.Contains(SelectedItem) != true)
{
SelectedItem = null;
}

break;
case SelectionMode.Multiple:
if (SelectedItems != null)
if (SelectedItems == null)
{
break;
}

internalItems = GetInternalItems(SelectedItems.Count);

foreach (var selectedItem in SelectedItems)
{
foreach (var selectedItem in SelectedItems)
if (!internalItems.Contains(selectedItem))
{
if (!InternalItems.Contains(selectedItem))
{
SelectedItems.Clear();
}
SelectedItems.Clear();
}
}

break;
}
}

private ICollection<object> GetInternalItems(int lookupCount = 1)
{
if (_internalItemsHashSet != null)
{
return _internalItemsHashSet;
}

if (lookupCount <= 1)
{
return InternalItems;
}

return _internalItemsHashSet = new HashSet<object>(InternalItems);
}

private SortData? RegenerateSortedColumnIndex()
{
if (_sortedColumn != null && SortedColumnIndex != null)
Expand Down

0 comments on commit 6a2f841

Please sign in to comment.