22// Roland Pheasant licenses this file to you under the MIT license.
33// See the LICENSE file in the project root for full license information.
44
5- using System ;
65using System . Collections ;
7- using System . Collections . Generic ;
8- using System . Linq ;
96using System . Reactive . Linq ;
107
118using DynamicData . Kernel ;
@@ -95,7 +92,7 @@ private static PageChangeSet<T> Page(List<T> all, ChangeAwareList<T> paged, IPag
9592 int page = request . Page > pages ? pages : request . Page ;
9693 int skip = request . Size * ( page - 1 ) ;
9794
98- var current = all . Skip ( skip )
95+ var current = all . Distinct ( ) . Skip ( skip )
9996 . Take ( request . Size )
10097 . ToList ( ) ;
10198
@@ -104,39 +101,27 @@ private static PageChangeSet<T> Page(List<T> all, ChangeAwareList<T> paged, IPag
104101
105102 paged . RemoveMany ( removes ) ;
106103
107- adds . ForEach ( t =>
104+ foreach ( var add in adds )
108105 {
109- var index = current . IndexOf ( t ) ;
110- paged . Insert ( index , t ) ;
111- } ) ;
106+ var index = current . IndexOf ( add ) ;
107+ paged . Insert ( index , add ) ;
108+ }
112109
113110 var startIndex = skip ;
114111
115- var moves = changeSet . EmptyIfNull ( )
116- . Where ( change => change . Reason == ListChangeReason . Moved
117- && change . MovedWithinRange ( startIndex , startIndex + request . Size ) ) ;
118-
119- foreach ( var change in moves )
120- {
121- // check whether an item has moved within the same page
122- var currentIndex = change . Item . CurrentIndex - startIndex ;
123- var previousIndex = change . Item . PreviousIndex - startIndex ;
124- paged . Move ( previousIndex , currentIndex ) ;
125- }
126-
127- // find replaces [Is this ever the case that it can be reached]
128- for ( int i = 0 ; i < current . Count ; i ++ )
112+ if ( changeSet is not null && changeSet . Count != 0 )
129113 {
130- var currentItem = current [ i ] ;
131- var previousItem = previous [ i ] ;
114+ var moves = changeSet
115+ . Where ( change => change . Reason == ListChangeReason . Moved
116+ && change . MovedWithinRange ( startIndex , startIndex + request . Size ) ) ;
132117
133- if ( ReferenceEquals ( currentItem , previousItem ) )
118+ foreach ( var change in moves )
134119 {
135- continue ;
120+ // check whether an item has moved within the same page
121+ var currentIndex = change . Item . CurrentIndex - startIndex ;
122+ var previousIndex = change . Item . PreviousIndex - startIndex ;
123+ paged . Move ( previousIndex , currentIndex ) ;
136124 }
137-
138- var index = paged . IndexOf ( currentItem ) ;
139- paged . Move ( i , index ) ;
140125 }
141126
142127 var changed = paged . CaptureChanges ( ) ;
0 commit comments