1+
12using System . ComponentModel ;
23
34namespace SimpleDataGrid . Pagination ;
45
6+ /// <summary>
7+ /// Represents a collection of items that can be paged, filtered, and searched.
8+ /// </summary>
9+ /// <typeparam name="T">The type of items in the collection.</typeparam>
510public class PagedCollection < T > : INotifyPropertyChanged
611{
712 private readonly int _pageSize ;
@@ -13,30 +18,50 @@ public class PagedCollection<T> : INotifyPropertyChanged
1318 private Func < T , string > ? _searchSelector ;
1419 private string ? _searchTerm ;
1520
21+ /// <summary>
22+ /// Initializes a new instance of the <see cref="PagedCollection{T}"/> class.
23+ /// </summary>
24+ /// <param name="pageSize">The number of items to display per page.</param>
1625 public PagedCollection ( int pageSize = 50 )
1726 {
1827 ArgumentOutOfRangeException . ThrowIfNegativeOrZero ( pageSize ) ;
1928 this . _pageSize = pageSize ;
2029 }
2130
31+ /// <summary>
32+ /// Sets the source collection.
33+ /// </summary>
34+ /// <param name="items">The source collection.</param>
2235 public void SetSource ( IReadOnlyList < T > items )
2336 {
2437 _source = items ?? throw new ArgumentNullException ( nameof ( items ) ) ;
2538 ApplyFiltering ( ) ;
2639 }
2740
41+ /// <summary>
42+ /// Adds a filter to the collection.
43+ /// </summary>
44+ /// <param name="filter">The filter to add.</param>
2845 public void AddFilter ( Func < T , bool > filter )
2946 {
3047 _filters . Add ( filter ) ;
3148 ApplyFiltering ( ) ;
3249 }
3350
51+ /// <summary>
52+ /// Clears all filters from the collection.
53+ /// </summary>
3454 public void ClearFilters ( )
3555 {
3656 _filters . Clear ( ) ;
3757 ApplyFiltering ( ) ;
3858 }
3959
60+ /// <summary>
61+ /// Sets the search criteria for the collection.
62+ /// </summary>
63+ /// <param name="selector">A function that returns the string representation of the object to search.</param>
64+ /// <param name="term">The search term.</param>
4065 public void SetSearch ( Func < T , string > selector , string ? term )
4166 {
4267 _searchSelector = selector ;
@@ -65,16 +90,35 @@ private void ApplyFiltering()
6590 RaiseAllChanged ( ) ;
6691 }
6792
93+ /// <summary>
94+ /// Gets the items on the current page.
95+ /// </summary>
6896 public IReadOnlyList < T > CurrentPageItems =>
6997 [ .. _filtered . Skip ( _currentPage * _pageSize ) . Take ( _pageSize ) ] ;
7098
99+ /// <summary>
100+ /// Gets the current page number.
101+ /// </summary>
71102 public int CurrentPage => _currentPage + 1 ;
72103
104+ /// <summary>
105+ /// Gets the total number of pages.
106+ /// </summary>
73107 public int TotalPages => ( int ) Math . Ceiling ( ( double ) _filtered . Count / _pageSize ) ;
74108
109+ /// <summary>
110+ /// Gets a value indicating whether there is a next page.
111+ /// </summary>
75112 public bool HasNext => _currentPage < TotalPages - 1 ;
113+
114+ /// <summary>
115+ /// Gets a value indicating whether there is a previous page.
116+ /// </summary>
76117 public bool HasPrevious => _currentPage > 0 ;
77118
119+ /// <summary>
120+ /// Moves to the next page.
121+ /// </summary>
78122 public void NextPage ( )
79123 {
80124 if ( HasNext )
@@ -87,6 +131,9 @@ public void NextPage()
87131 }
88132 }
89133
134+ /// <summary>
135+ /// Moves to the previous page.
136+ /// </summary>
90137 public void PreviousPage ( )
91138 {
92139 if ( HasPrevious )
@@ -108,7 +155,10 @@ private void RaiseAllChanged()
108155 OnPropertyChanged ( nameof ( HasPrevious ) ) ;
109156 }
110157
158+ /// <summary>
159+ /// Occurs when a property value changes.
160+ /// </summary>
111161 public event PropertyChangedEventHandler ? PropertyChanged ;
112162 private void OnPropertyChanged ( string name ) =>
113163 PropertyChanged ? . Invoke ( this , new PropertyChangedEventArgs ( name ) ) ;
114- }
164+ }
0 commit comments