Skip to content

Instantly share code, notes, and snippets.

@julesx
Created April 29, 2015 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julesx/298e0236f8b065495484 to your computer and use it in GitHub Desktop.
Save julesx/298e0236f8b065495484 to your computer and use it in GitHub Desktop.
updated PageParameterData
public class PageParameterData<TVm> : AbstractNotifyPropertyChanged
{
private int _currentPage;
private int _pageCount;
private int _pageSize;
private int _totalSize;
private int _startingIndex;
private int _endingIndex;
private readonly Command _cmdPageForward;
private readonly Command _cmdPageToEnd;
private readonly Command _cmdPageBackward;
private readonly Command _cmdPageToStart;
private readonly IObservableCollection<TVm> _masterList;
private readonly IObservableCollection<TVm> _filteredList;
public PageParameterData(int currentPage, int pageSize, IObservableCollection<TVm> filteredList, IObservableCollection<TVm> masterList)
{
_masterList = masterList;
_filteredList = filteredList;
_currentPage = currentPage;
_pageSize = pageSize;
_totalSize = masterList.Count;
_masterList.CollectionChanged += _masterList_CollectionChanged;
_filteredList.CollectionChanged += _filteredList_CollectionChanged;
_cmdPageForward = new Command(() => CurrentPage = CurrentPage + 1, () => CurrentPage < PageCount);
_cmdPageBackward = new Command(() => CurrentPage = CurrentPage - 1, () => CurrentPage > 1);
_cmdPageToEnd = new Command(() => CurrentPage = PageCount, () => CurrentPage < PageCount);
_cmdPageToStart = new Command(() => CurrentPage = 1, () => CurrentPage > 1);
}
void _filteredList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
StartingIndex = ((CurrentPage - 1) * PageSize) + 1;
if (_filteredList.Count == 0)
StartingIndex = EndingIndex = 0;
else if (_currentPage == _pageCount && StartingIndex > 0)
EndingIndex = StartingIndex + (_filteredList.Count - 1);
else
EndingIndex = _currentPage*_pageSize;
}
void _masterList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (CurrentPage == PageCount)
EndingIndex = _masterList.Count;
}
public void Update(IPageResponse response)
{
CurrentPage = response.Page;
PageSize = response.PageSize;
PageCount = response.Pages;
if (response.TotalSize != 0)
TotalSize = response.TotalSize;
_cmdPageForward.Refresh();
_cmdPageBackward.Refresh();
_cmdPageToEnd.Refresh();
_cmdPageToStart.Refresh();
}
public ICommand CmdPageForward
{
get { return _cmdPageForward; }
}
public ICommand CmdPageBackward
{
get { return _cmdPageBackward; }
}
public ICommand CmdPageToEnd
{
get { return _cmdPageToEnd; }
}
public ICommand CmdPageToStart
{
get { return _cmdPageToStart; }
}
public int TotalSize
{
get { return _totalSize; }
private set { SetAndRaise(ref _totalSize, value); }
}
public int PageCount
{
get { return _pageCount; }
private set { SetAndRaise(ref _pageCount, value); }
}
public int CurrentPage
{
get { return _currentPage; }
private set { SetAndRaise(ref _currentPage, value); }
}
public int PageSize
{
get { return _pageSize; }
private set { SetAndRaise(ref _pageSize, value); }
}
public int StartingIndex
{
get { return _startingIndex; }
private set { SetAndRaise(ref _startingIndex, value); }
}
public int EndingIndex
{
get { return _endingIndex; }
private set { SetAndRaise(ref _endingIndex, value); }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment