|
| 1 | +using System.ComponentModel; |
| 2 | + |
| 3 | +namespace SimpleDataGrid.Pagination; |
| 4 | + |
| 5 | +public class PagedCollection<T> : INotifyPropertyChanged |
| 6 | +{ |
| 7 | + private readonly int _pageSize; |
| 8 | + private int _currentPage; |
| 9 | + private IReadOnlyList<T> _source = Array.Empty<T>(); |
| 10 | + private IReadOnlyList<T> _filtered = Array.Empty<T>(); |
| 11 | + |
| 12 | + private Func<T, bool> _filter = _ => true; |
| 13 | + private Func<T, string>? _searchSelector; |
| 14 | + private string? _searchTerm; |
| 15 | + |
| 16 | + public PagedCollection(int pageSize = 50) |
| 17 | + { |
| 18 | + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(pageSize); |
| 19 | + this._pageSize = pageSize; |
| 20 | + } |
| 21 | + |
| 22 | + public void SetSource(IReadOnlyList<T> items) |
| 23 | + { |
| 24 | + _source = items ?? throw new ArgumentNullException(nameof(items)); |
| 25 | + ApplyFiltering(); |
| 26 | + } |
| 27 | + |
| 28 | + public void SetFilter(Func<T, bool> filter) |
| 29 | + { |
| 30 | + this._filter = filter ?? (_ => true); |
| 31 | + ApplyFiltering(); |
| 32 | + } |
| 33 | + |
| 34 | + public void SetSearch(Func<T, string> selector, string? term) |
| 35 | + { |
| 36 | + _searchSelector = selector; |
| 37 | + _searchTerm = term; |
| 38 | + ApplyFiltering(); |
| 39 | + } |
| 40 | + |
| 41 | + private void ApplyFiltering() |
| 42 | + { |
| 43 | + var query = _source.Where(_filter); |
| 44 | + |
| 45 | + if (!string.IsNullOrWhiteSpace(_searchTerm) && _searchSelector != null) |
| 46 | + { |
| 47 | + query = query.Where(x => |
| 48 | + _searchSelector(x) |
| 49 | + ?.Contains(_searchTerm, StringComparison.OrdinalIgnoreCase) == true); |
| 50 | + } |
| 51 | + |
| 52 | + _filtered = query.ToList(); |
| 53 | + _currentPage = 0; |
| 54 | + RaiseAllChanged(); |
| 55 | + } |
| 56 | + |
| 57 | + public IReadOnlyList<T> CurrentPageItems => |
| 58 | + _filtered.Skip(_currentPage * _pageSize).Take(_pageSize).ToList(); |
| 59 | + |
| 60 | + public int CurrentPage => _currentPage + 1; |
| 61 | + |
| 62 | + public int TotalPages => (int)Math.Ceiling((double)_filtered.Count / _pageSize); |
| 63 | + |
| 64 | + public bool HasNext => _currentPage < TotalPages - 1; |
| 65 | + public bool HasPrevious => _currentPage > 0; |
| 66 | + |
| 67 | + public void NextPage() |
| 68 | + { |
| 69 | + if (HasNext) |
| 70 | + { |
| 71 | + _currentPage++; |
| 72 | + OnPropertyChanged(nameof(CurrentPageItems)); |
| 73 | + OnPropertyChanged(nameof(CurrentPage)); |
| 74 | + OnPropertyChanged(nameof(HasNext)); |
| 75 | + OnPropertyChanged(nameof(HasPrevious)); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public void PreviousPage() |
| 80 | + { |
| 81 | + if (HasPrevious) |
| 82 | + { |
| 83 | + _currentPage--; |
| 84 | + OnPropertyChanged(nameof(CurrentPageItems)); |
| 85 | + OnPropertyChanged(nameof(CurrentPage)); |
| 86 | + OnPropertyChanged(nameof(HasNext)); |
| 87 | + OnPropertyChanged(nameof(HasPrevious)); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private void RaiseAllChanged() |
| 92 | + { |
| 93 | + OnPropertyChanged(nameof(CurrentPageItems)); |
| 94 | + OnPropertyChanged(nameof(CurrentPage)); |
| 95 | + OnPropertyChanged(nameof(TotalPages)); |
| 96 | + OnPropertyChanged(nameof(HasNext)); |
| 97 | + OnPropertyChanged(nameof(HasPrevious)); |
| 98 | + } |
| 99 | + |
| 100 | + public event PropertyChangedEventHandler? PropertyChanged; |
| 101 | + private void OnPropertyChanged(string name) => |
| 102 | + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); |
| 103 | +} |
0 commit comments