-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #757 from EdiWang/pagelist
Merge X.PagedList lib into the code
- Loading branch information
Showing
18 changed files
with
412 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
using System.Collections; | ||
|
||
namespace Moonglade.Web.PagedList; | ||
|
||
/// <summary> | ||
/// Represents a subset of a collection of objects that can be individually accessed by index and containing | ||
/// metadata about the superset collection of objects this subset was created from. | ||
/// </summary> | ||
/// <remarks> | ||
/// Represents a subset of a collection of objects that can be individually accessed by index and containing | ||
/// metadata about the superset collection of objects this subset was created from. | ||
/// </remarks> | ||
/// <typeparam name = "T">The type of object the collection should contain.</typeparam> | ||
/// <seealso cref = "IPagedList{T}" /> | ||
/// <seealso cref = "List{T}" /> | ||
public abstract class BasePagedList<T> : PagedListMetaData, IPagedList<T> | ||
{ | ||
protected readonly List<T> Subset = new(); | ||
|
||
/// <summary> | ||
/// Parameterless constructor. | ||
/// </summary> | ||
protected internal BasePagedList() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of a type deriving from <see cref = "BasePagedList{T}" /> and sets properties | ||
/// needed to calculate position and size data on the subset and superset. | ||
/// </summary> | ||
/// <param name = "pageNumber">The one-based index of the subset of objects contained by this instance.</param> | ||
/// <param name = "pageSize">The maximum size of any individual subset.</param> | ||
/// <param name = "totalItemCount">The size of the superset.</param> | ||
protected internal BasePagedList(int pageNumber, int pageSize, int totalItemCount) | ||
{ | ||
if (pageNumber < 1) | ||
{ | ||
throw new ArgumentOutOfRangeException($"pageNumber = {pageNumber}. PageNumber cannot be below 1."); | ||
} | ||
|
||
if (pageSize < 1) | ||
{ | ||
throw new ArgumentOutOfRangeException($"pageSize = {pageSize}. PageSize cannot be less than 1."); | ||
} | ||
|
||
if (totalItemCount < 0) | ||
{ | ||
throw new ArgumentOutOfRangeException($"totalItemCount = {totalItemCount}. TotalItemCount cannot be less than 0."); | ||
} | ||
|
||
// set source to blank list if superset is null to prevent exceptions | ||
TotalItemCount = totalItemCount; | ||
|
||
PageSize = pageSize; | ||
PageNumber = pageNumber; | ||
PageCount = TotalItemCount > 0 | ||
? (int)Math.Ceiling(TotalItemCount / (double)PageSize) | ||
: 0; | ||
|
||
var pageNumberIsGood = PageCount > 0 && PageNumber <= PageCount; | ||
|
||
HasPreviousPage = pageNumberIsGood && PageNumber > 1; | ||
HasNextPage = pageNumberIsGood && PageNumber < PageCount; | ||
IsFirstPage = pageNumberIsGood && PageNumber == 1; | ||
IsLastPage = pageNumberIsGood && PageNumber == PageCount; | ||
|
||
var numberOfFirstItemOnPage = (PageNumber - 1) * PageSize + 1; | ||
|
||
FirstItemOnPage = pageNumberIsGood ? numberOfFirstItemOnPage : 0; | ||
|
||
var numberOfLastItemOnPage = numberOfFirstItemOnPage + PageSize - 1; | ||
|
||
LastItemOnPage = pageNumberIsGood | ||
? numberOfLastItemOnPage > TotalItemCount | ||
? TotalItemCount | ||
: numberOfLastItemOnPage | ||
: 0; | ||
} | ||
|
||
#region IPagedList<T> Members | ||
|
||
/// <summary> | ||
/// Returns an enumerator that iterates through the BasePagedList<T>. | ||
/// </summary> | ||
/// <returns>A BasePagedList<T>.Enumerator for the BasePagedList<T>.</returns> | ||
public IEnumerator<T> GetEnumerator() | ||
{ | ||
return Subset.GetEnumerator(); | ||
} | ||
|
||
/// <summary> | ||
/// Returns an enumerator that iterates through the BasePagedList<T>. | ||
/// </summary> | ||
/// <returns>A BasePagedList<T>.Enumerator for the BasePagedList<T>.</returns> | ||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
|
||
///<summary> | ||
/// Gets the element at the specified index. | ||
///</summary> | ||
///<param name = "index">The zero-based index of the element to get.</param> | ||
public T this[int index] => Subset[index]; | ||
|
||
/// <summary> | ||
/// Gets the number of elements contained on this page. | ||
/// </summary> | ||
public virtual int Count => Subset.Count; | ||
|
||
#endregion | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
namespace Moonglade.Web.PagedList; | ||
|
||
/// <summary> | ||
/// Represents a subset of a collection of objects that can be individually accessed by index and containing | ||
/// metadata about the superset collection of objects this subset was created from. | ||
/// </summary> | ||
/// <remarks> | ||
/// Represents a subset of a collection of objects that can be individually accessed by index and containing | ||
/// metadata about the superset collection of objects this subset was created from. | ||
/// </remarks> | ||
/// <typeparam name="T">The type of object the collection should contain.</typeparam> | ||
/// <seealso cref="IEnumerable{T}"/> | ||
public interface IPagedList<out T> : IPagedList, IReadOnlyList<T> | ||
{ | ||
|
||
} | ||
|
||
/// <summary> | ||
/// Represents a subset of a collection of objects that can be individually accessed by index and containing | ||
/// metadata about the superset collection of objects this subset was created from. | ||
/// </summary> | ||
/// <remarks> | ||
/// Represents a subset of a collection of objects that can be individually accessed by index and containing | ||
/// metadata about the superset collection of objects this subset was created from. | ||
/// </remarks> | ||
public interface IPagedList | ||
{ | ||
/// <summary> | ||
/// Total number of subsets within the superset. | ||
/// </summary> | ||
/// <value> | ||
/// Total number of subsets within the superset. | ||
/// </value> | ||
int PageCount { get; } | ||
|
||
/// <summary> | ||
/// Total number of objects contained within the superset. | ||
/// </summary> | ||
/// <value> | ||
/// Total number of objects contained within the superset. | ||
/// </value> | ||
int TotalItemCount { get; } | ||
|
||
/// <summary> | ||
/// One-based index of this subset within the superset, zero if the superset is empty. | ||
/// </summary> | ||
/// <value> | ||
/// One-based index of this subset within the superset, zero if the superset is empty. | ||
/// </value> | ||
int PageNumber { get; } | ||
|
||
/// <summary> | ||
/// Maximum size any individual subset. | ||
/// </summary> | ||
/// <value> | ||
/// Maximum size any individual subset. | ||
/// </value> | ||
int PageSize { get; } | ||
|
||
/// <summary> | ||
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this | ||
/// is NOT the first subset within the superset. | ||
/// </summary> | ||
/// <value> | ||
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this | ||
/// is NOT the first subset within the superset. | ||
/// </value> | ||
bool HasPreviousPage { get; } | ||
|
||
/// <summary> | ||
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this | ||
/// is NOT the last subset within the superset. | ||
/// </summary> | ||
/// <value> | ||
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this | ||
/// is NOT the last subset within the superset. | ||
/// </value> | ||
bool HasNextPage { get; } | ||
|
||
/// <summary> | ||
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this | ||
/// is the first subset within the superset. | ||
/// </summary> | ||
/// <value> | ||
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this | ||
/// is the first subset within the superset. | ||
/// </value> | ||
bool IsFirstPage { get; } | ||
|
||
/// <summary> | ||
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this | ||
/// is the last subset within the superset. | ||
/// </summary> | ||
/// <value> | ||
/// Returns true if the superset is not empty and PageNumber is less than or equal to PageCount and this | ||
/// is the last subset within the superset. | ||
/// </value> | ||
bool IsLastPage { get; } | ||
|
||
/// <summary> | ||
/// One-based index of the first item in the paged subset, zero if the superset is empty or PageNumber | ||
/// is greater than PageCount. | ||
/// </summary> | ||
/// <value> | ||
/// One-based index of the first item in the paged subset, zero if the superset is empty or PageNumber | ||
/// is greater than PageCount. | ||
/// </value> | ||
int FirstItemOnPage { get; } | ||
|
||
/// <summary> | ||
/// One-based index of the last item in the paged subset, zero if the superset is empty or PageNumber | ||
/// is greater than PageCount. | ||
/// </summary> | ||
/// <value> | ||
/// One-based index of the last item in the paged subset, zero if the superset is empty or PageNumber | ||
/// is greater than PageCount. | ||
/// </value> | ||
int LastItemOnPage { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.