11using System ;
2- using System . Collections . Generic ;
2+ using System . Collections ;
33using System . Linq ;
44
55namespace SmartStore . Core
66{
7- /// <summary>
8- /// Paged list
9- /// </summary>
10- /// <typeparam name="T">T</typeparam>
11- public class PagedList < T > : List < T > , IPagedList < T >
7+
8+ // codehint: sm-add (whole file)
9+ public abstract class PagedListBase : IPageable
1210 {
13- /// <summary>
14- /// Ctor
15- /// </summary>
16- /// <param name="source">source</param>
17- /// <param name="pageIndex">Page index</param>
18- /// <param name="pageSize">Page size</param>
19- public PagedList ( IQueryable < T > source , int pageIndex , int pageSize )
20- {
21- // codehint: sm-edit
22- Guard . ArgumentNotNull ( source , "source" ) ;
23- Init ( source . Skip ( pageIndex * pageSize ) . Take ( pageSize ) , pageIndex , pageSize , source . Count ( ) ) ;
11+
12+ protected PagedListBase ( )
13+ {
14+ this . PageIndex = 0 ;
15+ this . PageSize = 0 ;
16+ this . TotalCount = 1 ;
2417 }
2518
26- /// <summary>
27- /// Ctor
28- /// </summary>
29- /// <param name="source">source</param>
30- /// <param name="pageIndex">Page index</param>
31- /// <param name="pageSize">Page size</param>
32- public PagedList ( IList < T > source , int pageIndex , int pageSize )
19+ protected PagedListBase ( IPageable pageable )
3320 {
34- // codehint: sm-edit
35- Guard . ArgumentNotNull ( source , "source" ) ;
36- Init ( source . Skip ( pageIndex * pageSize ) . Take ( pageSize ) , pageIndex , pageSize , source . Count ) ;
21+ this . Init ( pageable ) ;
3722 }
3823
39- /// <summary>
40- /// Ctor
41- /// </summary>
42- /// <param name="source">source</param>
43- /// <param name="pageIndex">Page index</param>
44- /// <param name="pageSize">Page size</param>
45- /// <param name="totalCount">Total count</param>
46- public PagedList ( IEnumerable < T > source , int pageIndex , int pageSize , int totalCount )
24+ protected PagedListBase ( IEnumerable source , int pageIndex , int pageSize )
4725 {
48- // codehint: sm-edit
4926 Guard . ArgumentNotNull ( source , "source" ) ;
50- Init ( source , pageIndex , pageSize , totalCount ) ;
27+ Guard . PagingArgsValid ( pageIndex , pageSize , "pageIndex" , "pageSize" ) ;
28+
29+ this . PageIndex = pageIndex ;
30+ this . PageSize = pageSize ;
31+ this . TotalCount = source . GetCount ( ) ;
5132 }
5233
53- // codehint: sm-add
54- private void Init ( IEnumerable < T > source , int pageIndex , int pageSize , int totalCount )
34+ protected PagedListBase ( int pageIndex , int pageSize , int totalItemsCount )
5535 {
56- Guard . PagingArgsValid ( pageIndex , pageSize , "pageIndex" , "pageSize" ) ;
36+ Guard . PagingArgsValid ( pageIndex , pageSize , "pageIndex" , "pageSize" ) ;
5737
5838 this . PageIndex = pageIndex ;
5939 this . PageSize = pageSize ;
60- this . TotalCount = totalCount ;
40+ this . TotalCount = totalItemsCount ;
41+ }
6142
62- this . AddRange ( source ) ;
43+ // only here for compat reasons with nc
44+ public void LoadPagedList < T > ( IPagedList < T > pagedList )
45+ {
46+ this . Init ( pagedList as IPageable ) ;
6347 }
6448
65- #region IPageable Members
49+ public virtual void Init ( IPageable pageable )
50+ {
51+ Guard . ArgumentNotNull ( pageable , "pageable" ) ;
6652
67- // codehint: sm-add/edit
53+ this . PageIndex = pageable . PageIndex ;
54+ this . PageSize = pageable . PageSize ;
55+ this . TotalCount = pageable . TotalCount ;
56+ }
6857
6958 public int PageIndex
7059 {
@@ -86,7 +75,7 @@ public int TotalCount
8675
8776 public int PageNumber
8877 {
89- get
78+ get
9079 {
9180 return this . PageIndex + 1 ;
9281 }
@@ -98,7 +87,7 @@ public int PageNumber
9887
9988 public int TotalPages
10089 {
101- get
90+ get
10291 {
10392 var total = this . TotalCount / this . PageSize ;
10493
@@ -111,53 +100,64 @@ public int TotalPages
111100
112101 public bool HasPreviousPage
113102 {
114- get
103+ get
115104 {
116- return this . PageIndex > 0 ;
105+ return this . PageIndex > 0 ;
117106 }
118107 }
119108
120109 public bool HasNextPage
121110 {
122- get
111+ get
123112 {
124113 return ( this . PageIndex < ( this . TotalPages - 1 ) ) ;
125114 }
126115 }
127116
128117 public int FirstItemIndex
129118 {
130- get
119+ get
131120 {
132- return ( this . PageIndex * this . PageSize ) + 1 ;
121+ return ( this . PageIndex * this . PageSize ) + 1 ;
133122 }
134123 }
135124
136125 public int LastItemIndex
137126 {
138- get
127+ get
139128 {
140129 return Math . Min ( this . TotalCount , ( ( this . PageIndex * this . PageSize ) + this . PageSize ) ) ;
141130 }
142131 }
143132
144133 public bool IsFirstPage
145134 {
146- get
147- {
135+ get
136+ {
148137 return ( this . PageIndex <= 0 ) ;
149138 }
150139 }
151140
152141 public bool IsLastPage
153142 {
154- get
143+ get
155144 {
156- return ( this . PageIndex >= ( this . TotalPages - 1 ) ) ;
145+ return ( this . PageIndex >= ( this . TotalPages - 1 ) ) ;
157146 }
158147 }
159148
160- #endregion
149+ public virtual IEnumerator GetEnumerator ( )
150+ {
151+ return Enumerable . Empty < int > ( ) . GetEnumerator ( ) ;
152+ }
153+
154+ }
161155
156+ public class PagedList : PagedListBase
157+ {
158+ public PagedList ( int pageIndex , int pageSize , int totalItemsCount ) : base ( pageIndex , pageSize , totalItemsCount )
159+ {
160+ }
162161 }
162+
163163}
0 commit comments