Skip to content

Commit

Permalink
prevent simultaneous sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Miller committed Dec 27, 2023
1 parent 81222bb commit 89db8bc
Showing 1 changed file with 40 additions and 21 deletions.
61 changes: 40 additions & 21 deletions Maui.DataGrid/DataGrid.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public partial class DataGrid
private readonly Style _defaultSortIconStyle;

private bool _isReloading;
private bool _isSortingAndPaginating;
private readonly object _reloadLock = new();
private readonly object _sortAndPaginateLock = new();
private IList<object>? _internalItems;
private DataGridColumn? _sortedColumn;

Expand Down Expand Up @@ -175,33 +177,50 @@ private IEnumerable<object> GetPaginatedItems(IEnumerable<object> unpaginatedIte

private void SortAndPaginate(SortData? sortData = null)
{
if (ItemsSource is null)
lock (_sortAndPaginateLock)
{
return;
}
if (_isSortingAndPaginating)
{
return;
}

sortData ??= SortedColumnIndex;
_isSortingAndPaginating = true;

var originalItems = ItemsSource.Cast<object>().ToList();
try
{
if (ItemsSource is null)
{
return;
}

IList<object> sortedItems;
sortData ??= SortedColumnIndex;

if (sortData != null && CanSort(sortData))
{
sortedItems = GetSortedItems(originalItems, sortData);
}
else
{
sortedItems = originalItems;
}
var originalItems = ItemsSource.Cast<object>().ToList();

if (PaginationEnabled)
{
InternalItems = GetPaginatedItems(sortedItems).ToList();
}
else
{
InternalItems = sortedItems;
IList<object> sortedItems;

if (sortData != null && CanSort(sortData))
{
sortedItems = GetSortedItems(originalItems, sortData);
}
else
{
sortedItems = originalItems;
}

if (PaginationEnabled)
{
InternalItems = GetPaginatedItems(sortedItems).ToList();
}
else
{
InternalItems = sortedItems;
}
}
finally
{
_isSortingAndPaginating = false;
}
}
}

Expand Down

0 comments on commit 89db8bc

Please sign in to comment.