1- using System . Collections . ObjectModel ;
1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Collections . ObjectModel ;
24using System . Collections . Specialized ;
35using System . ComponentModel ;
6+ using System . Linq ;
47using System . Runtime . CompilerServices ;
58using System . Runtime . InteropServices . WindowsRuntime ;
69using System . Threading ;
912using Windows . UI . Core ;
1013using Windows . UI . Xaml . Data ;
1114using WinGetStore . Common ;
15+ using WinGetStore . Helpers ;
1216
1317namespace WinGetStore . Models
1418{
@@ -21,18 +25,18 @@ namespace WinGetStore.Models
2125 /// so that you can load different data in your view model, refer this blog for detail
2226 /// <see href="http://blogs.msdn.com/b/devosaure/archive/2012/10/15/isupportincrementalloading-loading-a-subsets-of-data.aspx"/>
2327 /// </summary>
24- public abstract class IncrementalLoadingBase < T > ( CoreDispatcher dispatcher ) : ObservableCollection < T > , ISupportIncrementalLoading
28+ public abstract class IncrementalLoadingBase < T > ( CoreDispatcher dispatcher ) : ObservableCollection < T > , ISupportIncrementalLoading , IAsyncEnumerable < T >
2529 {
2630 #region ISupportIncrementalLoading
2731
28- public bool HasMoreItems => HasMoreItemsOverride ( ) ;
32+ public abstract bool HasMoreItems { get ; }
2933
3034 /// <summary>
3135 /// Load more items, this is invoked by Controls like ListView.
3236 /// </summary>
3337 /// <param name="count">How many new items want to load.</param>
3438 /// <returns>Item count actually loaded.</returns>
35- public IAsyncOperation < LoadMoreItemsResult > LoadMoreItemsAsync ( uint count )
39+ public IAsyncOperation < LoadMoreItemsResult > LoadMoreItemsAsync ( uint count = 15 )
3640 {
3741 if ( _busy )
3842 {
@@ -49,11 +53,11 @@ public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
4953
5054 public CoreDispatcher Dispatcher => dispatcher ;
5155
52- private bool any = false ;
53- public bool Any
56+ private bool isEmpty = false ;
57+ public bool IsEmpty
5458 {
55- get => any ;
56- set => SetProperty ( ref any , value ) ;
59+ get => isEmpty ;
60+ set => SetProperty ( ref isEmpty , value ) ;
5761 }
5862
5963 private bool isLoading = false ;
@@ -118,7 +122,7 @@ protected async Task<LoadMoreItemsResult> LoadMoreItemsAsync(uint count, Cancell
118122 protected override void OnCollectionChanged ( NotifyCollectionChangedEventArgs e )
119123 {
120124 base . OnCollectionChanged ( e ) ;
121- Any = Count > 0 ;
125+ IsEmpty = Count > 0 ;
122126 }
123127
124128 public delegate void EventHandler ( ) ;
@@ -127,29 +131,37 @@ protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
127131 public event EventHandler LoadMoreStarted ;
128132 public event EventHandler LoadMoreCompleted ;
129133
130- #region Overridable methods
134+ public Task AddAsync ( T item ) => Dispatcher . AwaitableRunAsync ( ( ) => Add ( item ) ) ;
131135
132- public virtual async Task AddAsync ( T item )
133- {
134- await Dispatcher . ResumeForegroundAsync ( ) ;
135- Add ( item ) ;
136- }
136+ public Task < bool > RemoveAsync ( T item ) => Dispatcher . AwaitableRunAsync ( ( ) => Remove ( item ) ) ;
137137
138- public virtual async Task RemoveAsync ( T item )
139- {
140- await Dispatcher . ResumeForegroundAsync ( ) ;
141- _ = Remove ( item ) ;
142- }
138+ public Task ClearAsync ( ) => Dispatcher . AwaitableRunAsync ( Clear ) ;
143139
144- public virtual async Task ClearAsync ( )
140+ public async IAsyncEnumerator < T > GetAsyncEnumerator ( CancellationToken cancellationToken = default )
145141 {
146- await Dispatcher . ResumeForegroundAsync ( ) ;
147- Clear ( ) ;
142+ foreach ( T item in this )
143+ {
144+ cancellationToken . ThrowIfCancellationRequested ( ) ;
145+ yield return item ;
146+ }
147+ while ( HasMoreItems )
148+ {
149+ LoadMoreItemsResult result = await LoadMoreItemsAsync ( ) . AsTask ( cancellationToken ) ;
150+ int count = ( int ) result . Count ;
151+ if ( count > 0 )
152+ {
153+ for ( int i = Count - count ; i < Count ; i ++ )
154+ {
155+ cancellationToken . ThrowIfCancellationRequested ( ) ;
156+ yield return this [ i ] ;
157+ }
158+ }
159+ }
148160 }
149161
150- protected abstract Task < uint > LoadMoreItemsOverrideAsync ( uint count , CancellationToken cancellationToken ) ;
162+ #region Overridable methods
151163
152- protected abstract bool HasMoreItemsOverride ( ) ;
164+ protected abstract ValueTask < uint > LoadMoreItemsOverrideAsync ( uint count , CancellationToken cancellationToken ) ;
153165
154166 #endregion
155167
0 commit comments