Skip to content

Commit 6896a96

Browse files
committed
simplification
1 parent c927f83 commit 6896a96

File tree

10 files changed

+164
-146
lines changed

10 files changed

+164
-146
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using SimpleDataGrid.Pagination;
2+
3+
namespace SimpleDataGrid.Tests;
4+
5+
[TestClass]
6+
public class PagedCollectionTests
7+
{
8+
[TestMethod]
9+
public void SetSource_WithValidSource_InitializesCollection()
10+
{
11+
// Arrange
12+
var source = new List<int> { 1, 2, 3, 4, 5 };
13+
var pagedCollection = new PagedCollection<int>(2);
14+
15+
// Act
16+
pagedCollection.SetSource(source);
17+
18+
// Assert
19+
Assert.AreEqual(3, pagedCollection.TotalPages);
20+
Assert.AreEqual(1, pagedCollection.CurrentPage);
21+
Assert.HasCount(2, pagedCollection.CurrentPageItems);
22+
Assert.AreEqual(1, pagedCollection.CurrentPageItems[0]);
23+
Assert.AreEqual(2, pagedCollection.CurrentPageItems[1]);
24+
}
25+
26+
[TestMethod]
27+
public void AddFilter_WithFilter_FiltersTheCollection()
28+
{
29+
// Arrange
30+
var source = new List<int> { 1, 2, 3, 4, 5 };
31+
var pagedCollection = new PagedCollection<int>(2);
32+
pagedCollection.SetSource(source);
33+
34+
// Act
35+
pagedCollection.AddFilter(x => x > 3);
36+
37+
// Assert
38+
Assert.AreEqual(1, pagedCollection.TotalPages);
39+
Assert.AreEqual(1, pagedCollection.CurrentPage);
40+
Assert.HasCount(2, pagedCollection.CurrentPageItems);
41+
Assert.AreEqual(4, pagedCollection.CurrentPageItems[0]);
42+
Assert.AreEqual(5, pagedCollection.CurrentPageItems[1]);
43+
}
44+
45+
[TestMethod]
46+
public void SetSearch_WithSearchTerm_SearchesTheCollection()
47+
{
48+
// Arrange
49+
var source = new List<string> { "apple", "banana", "cherry", "date" };
50+
var pagedCollection = new PagedCollection<string>(2);
51+
pagedCollection.SetSource(source);
52+
53+
// Act
54+
pagedCollection.SetSearch(x => x, "an");
55+
56+
// Assert
57+
Assert.AreEqual(1, pagedCollection.TotalPages);
58+
Assert.AreEqual(1, pagedCollection.CurrentPage);
59+
Assert.HasCount(1, pagedCollection.CurrentPageItems);
60+
Assert.AreEqual("banana", pagedCollection.CurrentPageItems[0]);
61+
}
62+
63+
[TestMethod]
64+
public void Pagination_MovesCorrectlyBetweenPages()
65+
{
66+
// Arrange
67+
var source = new List<int> { 1, 2, 3, 4, 5, 6 };
68+
var pagedCollection = new PagedCollection<int>(2);
69+
pagedCollection.SetSource(source);
70+
71+
// Assert initial state
72+
Assert.AreEqual(3, pagedCollection.TotalPages);
73+
Assert.AreEqual(1, pagedCollection.CurrentPage);
74+
Assert.IsTrue(pagedCollection.HasNext);
75+
Assert.IsFalse(pagedCollection.HasPrevious);
76+
CollectionAssert.AreEqual(new List<int> { 1, 2 }, (List<int>)pagedCollection.CurrentPageItems);
77+
78+
// Act: Move to next page
79+
pagedCollection.NextPage();
80+
81+
// Assert after moving to next page
82+
Assert.AreEqual(2, pagedCollection.CurrentPage);
83+
Assert.IsTrue(pagedCollection.HasNext);
84+
Assert.IsTrue(pagedCollection.HasPrevious);
85+
CollectionAssert.AreEqual(new List<int> { 3, 4 }, (List<int>)pagedCollection.CurrentPageItems);
86+
87+
// Act: Move to next page again (last page)
88+
pagedCollection.NextPage();
89+
90+
// Assert after moving to last page
91+
Assert.AreEqual(3, pagedCollection.CurrentPage);
92+
Assert.IsFalse(pagedCollection.HasNext);
93+
Assert.IsTrue(pagedCollection.HasPrevious);
94+
CollectionAssert.AreEqual(new List<int> { 5, 6 }, (List<int>)pagedCollection.CurrentPageItems);
95+
96+
// Act: Try to move next beyond last page (should do nothing)
97+
pagedCollection.NextPage();
98+
Assert.AreEqual(3, pagedCollection.CurrentPage); // Should still be on page 3
99+
100+
// Act: Move to previous page
101+
pagedCollection.PreviousPage();
102+
103+
// Assert after moving to previous page
104+
Assert.AreEqual(2, pagedCollection.CurrentPage);
105+
Assert.IsTrue(pagedCollection.HasNext);
106+
Assert.IsTrue(pagedCollection.HasPrevious);
107+
CollectionAssert.AreEqual(new List<int> { 3, 4 }, (List<int>)pagedCollection.CurrentPageItems);
108+
109+
// Act: Move to previous page again (first page)
110+
pagedCollection.PreviousPage();
111+
112+
// Assert after moving to first page
113+
Assert.AreEqual(1, pagedCollection.CurrentPage);
114+
Assert.IsTrue(pagedCollection.HasNext);
115+
Assert.IsFalse(pagedCollection.HasPrevious);
116+
CollectionAssert.AreEqual(new List<int> { 1, 2 }, (List<int>)pagedCollection.CurrentPageItems);
117+
118+
// Act: Try to move previous beyond first page (should do nothing)
119+
pagedCollection.PreviousPage();
120+
Assert.AreEqual(1, pagedCollection.CurrentPage); // Should still be on page 1
121+
}
122+
}

SimpleDataGrid.Tests/SimpleDataGrid.Tests.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net9.0</TargetFramework>
4+
<TargetFramework>net9.0-windows</TargetFramework>
55
<LangVersion>latest</LangVersion>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
@@ -15,6 +15,10 @@
1515
<TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure>
1616
</PropertyGroup>
1717

18+
<ItemGroup>
19+
<ProjectReference Include="..\SimpleDataGrid\SimpleDataGrid.csproj" />
20+
</ItemGroup>
21+
1822
<ItemGroup>
1923
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
2024
<PackageReference Include="Microsoft.Testing.Extensions.CodeCoverage" Version="18.0.4" />
@@ -26,4 +30,4 @@
2630
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
2731
</ItemGroup>
2832

29-
</Project>
33+
</Project>

SimpleDataGrid.Tests/Test1.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

SimpleDataGrid/Controls/PageDataGrid.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
1-
using System.Windows;
2-
using System.Windows.Controls;
1+
32
using SimpleDataGrid.Pagination;
3+
using System.Windows;
4+
using System.Windows.Controls;
45

56
namespace SimpleDataGrid.Controls;
67

7-
public class PagedDataGrid : DataGrid
8+
public class PagedDataGrid<T> : DataGrid
89
{
910
public static readonly DependencyProperty PagedSourceProperty =
10-
DependencyProperty.Register(nameof(PagedSource), typeof(object), typeof(PagedDataGrid),
11+
DependencyProperty.Register(nameof(PagedSource), typeof(PagedCollection<T>), typeof(PagedDataGrid<T>),
1112
new PropertyMetadata(null, OnPagedSourceChanged));
1213

13-
public object? PagedSource
14+
public PagedCollection<T>? PagedSource
1415
{
15-
get => GetValue(PagedSourceProperty);
16+
get => (PagedCollection<T>)GetValue(PagedSourceProperty);
1617
set => SetValue(PagedSourceProperty, value);
1718
}
1819

1920
private static void OnPagedSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
2021
{
21-
if (d is PagedDataGrid grid && e.NewValue is IPagedSource paged)
22+
if (d is PagedDataGrid<T> grid && e.NewValue is PagedCollection<T> paged)
2223
{
2324
grid.ItemsSource = paged.CurrentPageItems;
25+
paged.PropertyChanged += (s, a) =>
26+
{
27+
if (a.PropertyName == nameof(paged.CurrentPageItems))
28+
{
29+
grid.ItemsSource = paged.CurrentPageItems;
30+
}
31+
};
2432
}
2533
}
2634
}
27-
28-
public interface IPagedSource
29-
{
30-
object CurrentPageItems { get; }
31-
}

SimpleDataGrid/Extensions/DataGridExtensions.cs

Lines changed: 0 additions & 18 deletions
This file was deleted.

SimpleDataGrid/Filtering/FilterCollection.cs

Lines changed: 0 additions & 29 deletions
This file was deleted.

SimpleDataGrid/Filtering/FilterDescriptor.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.

SimpleDataGrid/Pagination/PagedCollection.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
using System.ComponentModel;
1+
using System.ComponentModel;
22

33
namespace SimpleDataGrid.Pagination;
44

55
public class PagedCollection<T> : INotifyPropertyChanged
66
{
77
private readonly int _pageSize;
88
private int _currentPage;
9-
private IReadOnlyList<T> _source = Array.Empty<T>();
10-
private IReadOnlyList<T> _filtered = Array.Empty<T>();
9+
private IReadOnlyList<T> _source = [];
10+
private IReadOnlyList<T> _filtered = [];
1111

12-
private Func<T, bool> _filter = _ => true;
12+
private readonly List<Func<T, bool>> _filters = [];
1313
private Func<T, string>? _searchSelector;
1414
private string? _searchTerm;
1515

@@ -25,9 +25,15 @@ public void SetSource(IReadOnlyList<T> items)
2525
ApplyFiltering();
2626
}
2727

28-
public void SetFilter(Func<T, bool> filter)
28+
public void AddFilter(Func<T, bool> filter)
2929
{
30-
this._filter = filter ?? (_ => true);
30+
_filters.Add(filter);
31+
ApplyFiltering();
32+
}
33+
34+
public void ClearFilters()
35+
{
36+
_filters.Clear();
3137
ApplyFiltering();
3238
}
3339

@@ -40,7 +46,12 @@ public void SetSearch(Func<T, string> selector, string? term)
4046

4147
private void ApplyFiltering()
4248
{
43-
var query = _source.Where(_filter);
49+
IEnumerable<T> query = _source;
50+
51+
foreach (var filter in _filters)
52+
{
53+
query = query.Where(filter);
54+
}
4455

4556
if (!string.IsNullOrWhiteSpace(_searchTerm) && _searchSelector != null)
4657
{
@@ -49,13 +60,13 @@ private void ApplyFiltering()
4960
?.Contains(_searchTerm, StringComparison.OrdinalIgnoreCase) == true);
5061
}
5162

52-
_filtered = query.ToList();
63+
_filtered = [.. query];
5364
_currentPage = 0;
5465
RaiseAllChanged();
5566
}
5667

5768
public IReadOnlyList<T> CurrentPageItems =>
58-
_filtered.Skip(_currentPage * _pageSize).Take(_pageSize).ToList();
69+
[.. _filtered.Skip(_currentPage * _pageSize).Take(_pageSize)];
5970

6071
public int CurrentPage => _currentPage + 1;
6172

@@ -100,4 +111,4 @@ private void RaiseAllChanged()
100111
public event PropertyChangedEventHandler? PropertyChanged;
101112
private void OnPropertyChanged(string name) =>
102113
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
103-
}
114+
}

SimpleDataGrid/Pagination/PagedCollectionView.cs

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)