Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into MeasureFirstItem
  • Loading branch information
Edward Miller committed Oct 12, 2024
2 parents b94ff66 + bda06bc commit 8c1f4d2
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 70 deletions.
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<WarningLevel>9999</WarningLevel>
<AnalysisLevel>latest-all</AnalysisLevel>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
Expand Down
2 changes: 2 additions & 0 deletions Maui.DataGrid.Sample/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace Maui.DataGrid.Sample;

#pragma warning disable CA1724

/// <summary>
/// Codebehind for the App.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions Maui.DataGrid.Sample/Converters/StreakToColorConverter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
namespace Maui.DataGrid.Sample.Converters;

using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using Maui.DataGrid.Sample.Models;

[SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses", Justification = "Instantiated via XAML")]
internal sealed class StreakToColorConverter : IValueConverter
{
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
Expand Down
2 changes: 2 additions & 0 deletions Maui.DataGrid/Collections/ObservableRangeCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ namespace Maui.DataGrid.Collections;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;

/// <summary>
/// Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.
/// </summary>
/// <typeparam name="T">The object type of elements in the collection.</typeparam>
[SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses", Justification = "Instantiated via collection expression")]
internal sealed class ObservableRangeCollection<T> : ObservableCollection<T>
{
/// <summary>
Expand Down
100 changes: 33 additions & 67 deletions Maui.DataGrid/DataGrid.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ namespace Maui.DataGrid;
using Microsoft.Maui.Controls.Shapes;
using Font = Microsoft.Maui.Font;

#pragma warning disable CA1724

/// <summary>
/// DataGrid component for .NET MAUI.
/// </summary>
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class DataGrid
{
#region Bindable properties

/// <summary>
/// Gets or sets the color of the active row.
/// </summary>
Expand Down Expand Up @@ -637,10 +637,6 @@ public partial class DataGrid
}
});

#endregion Bindable properties

#region Fields

private static readonly SortedSet<int> DefaultPageSizeSet = [5, 10, 50, 100, 200, 1000];
private static readonly IList<int> DefaultPageSizeList = [.. DefaultPageSizeSet];

Expand All @@ -659,10 +655,6 @@ public partial class DataGrid
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]
private Type? _cachedType;

#endregion Fields

#region ctor

/// <summary>
/// Initializes a new instance of the <see cref="DataGrid"/> class.
/// </summary>
Expand All @@ -680,10 +672,6 @@ public DataGrid()
}
}

#endregion ctor

#region Events

/// <summary>
/// Occurs when an item is selected in the DataGrid.
/// </summary>
Expand Down Expand Up @@ -720,10 +708,6 @@ internal event EventHandler RowsTextColorPaletteChanged
remove => _rowsTextColorPaletteChangedEventManager.RemoveEventHandler(value);
}

#endregion Events

#region Properties

#pragma warning disable CA2227 // Collection properties should be read only

/// <summary>
Expand Down Expand Up @@ -1145,8 +1129,6 @@ private set
}
}

#pragma warning restore CA2227 // Collection properties should be read only

internal Style DefaultHeaderLabelStyle { get; }

internal Style DefaultHeaderFilterStyle { get; }
Expand All @@ -1155,10 +1137,6 @@ private set

internal ObservableRangeCollection<object> InternalItems { get; } = [];

#endregion Properties

#region Methods

/// <summary>
/// Scrolls to the row.
/// </summary>
Expand All @@ -1182,9 +1160,36 @@ internal void Initialize()
}
}

#endregion Methods
internal void SortFilterAndPaginate(SortData? sortData = null)
{
if (ItemsSource is null)
{
return;
}

lock (_sortAndPaginateLock)
{
sortData ??= SortedColumnIndex;

var originalItems = ItemsSource as IList<object> ?? ItemsSource.Cast<object>().ToList();

PageCount = (int)Math.Ceiling(originalItems.Count / (double)PageSize);

if (originalItems.Count == 0)
{
InternalItems.Clear();
return;
}

var filteredItems = CanFilter() ? GetFilteredItems(originalItems) : originalItems;

var sortedItems = CanSort(sortData) ? GetSortedItems(filteredItems, sortData!) : filteredItems;

var paginatedItems = PaginationEnabled ? GetPaginatedItems(sortedItems) : sortedItems;

#region UI Methods
InternalItems.ReplaceRange(paginatedItems);
}
}

/// <inheritdoc/>
protected override void OnParentSet()
Expand Down Expand Up @@ -1313,10 +1318,6 @@ private ICollection<object> GetInternalItems(int lookupCount = 1)
return new(newSortedColumnIndex, SortedColumnIndex.Order);
}

#endregion UI Methods

#region Sorting methods

private bool CanFilter() => FilteringEnabled && Columns.Any(c => c.FilteringEnabled);

private bool CanSort(SortData? sortData)
Expand Down Expand Up @@ -1413,41 +1414,6 @@ private IEnumerable<object> GetSortedItems(IList<object> unsortedItems, SortData
return items;
}

#endregion Sorting methods

#region Pagination methods

internal void SortFilterAndPaginate(SortData? sortData = null)
{
if (ItemsSource is null)
{
return;
}

lock (_sortAndPaginateLock)
{
sortData ??= SortedColumnIndex;

var originalItems = ItemsSource as IList<object> ?? ItemsSource.Cast<object>().ToList();

PageCount = (int)Math.Ceiling(originalItems.Count / (double)PageSize);

if (originalItems.Count == 0)
{
InternalItems.Clear();
return;
}

var filteredItems = CanFilter() ? GetFilteredItems(originalItems) : originalItems;

var sortedItems = CanSort(sortData) ? GetSortedItems(filteredItems, sortData!) : filteredItems;

var paginatedItems = PaginationEnabled ? GetPaginatedItems(sortedItems) : sortedItems;

InternalItems.ReplaceRange(paginatedItems);
}
}

private List<object> GetFilteredItems(IList<object> originalItems)
{
var filteredItems = originalItems.AsEnumerable();
Expand Down Expand Up @@ -1495,11 +1461,13 @@ private bool FilterItem(object item, DataGridColumn column)

return result == true;
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception ex)
{
Debug.WriteLine(ex);
return false;
}
#pragma warning restore CA1031 // Do not catch general exception types
}

private IEnumerable<object> GetPaginatedItems(IEnumerable<object> unpaginatedItems)
Expand All @@ -1526,6 +1494,4 @@ private void UpdatePageSizeList()
OnPropertyChanged(nameof(PageSize));
}
}

#endregion Pagination methods
}
5 changes: 2 additions & 3 deletions Maui.DataGrid/DataGridHeaderRow.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
namespace Maui.DataGrid;

using System.Diagnostics.CodeAnalysis;
using Maui.DataGrid.Extensions;
using Microsoft.Maui.Controls;

[SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses", Justification = "Instantiated via XAML")]
internal sealed class DataGridHeaderRow : Grid
{
#region Bindable Properties
Expand All @@ -26,8 +28,6 @@ internal sealed class DataGridHeaderRow : Grid
new() { Width = new(1, GridUnitType.Auto) },
];

private readonly Thickness _headerCellPadding = new(0, 0, 4, 0);

private readonly Command<DataGridColumn> _sortCommand = new(OnSort, CanSort);

#endregion Fields
Expand Down Expand Up @@ -195,7 +195,6 @@ private DataGridCell CreateHeaderCell(DataGridColumn column)

var cellContent = new Grid
{
Padding = _headerCellPadding,
ColumnDefinitions = _headerColumnDefinitions,
RowDefinitions = _headerRowDefinitions,
};
Expand Down
2 changes: 2 additions & 0 deletions Maui.DataGrid/DataGridRow.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
namespace Maui.DataGrid;

using System.Diagnostics.CodeAnalysis;
using Maui.DataGrid.Extensions;
using Microsoft.Maui.Controls;

[SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses", Justification = "Instantiated via XAML")]
internal sealed class DataGridRow : Grid
{
#region Bindable Properties
Expand Down
1 change: 1 addition & 0 deletions Maui.DataGrid/Maui.DataGrid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)">
<PrivateAssets>all</PrivateAssets>
Expand Down

0 comments on commit 8c1f4d2

Please sign in to comment.