Skip to content

Commit fb276ef

Browse files
committed
更新增量加载列表
1 parent fbc1e24 commit fb276ef

2 files changed

Lines changed: 45 additions & 33 deletions

File tree

WinGetStore/Models/IncrementalLoadingBase.cs

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
using System.Collections.ObjectModel;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
24
using System.Collections.Specialized;
35
using System.ComponentModel;
6+
using System.Linq;
47
using System.Runtime.CompilerServices;
58
using System.Runtime.InteropServices.WindowsRuntime;
69
using System.Threading;
@@ -9,6 +12,7 @@
912
using Windows.UI.Core;
1013
using Windows.UI.Xaml.Data;
1114
using WinGetStore.Common;
15+
using WinGetStore.Helpers;
1216

1317
namespace 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

WinGetStore/Models/PackageVersionSource.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@
1212

1313
namespace WinGetStore.Models
1414
{
15-
public partial class PackageVersionSource(CatalogPackage catalogPackage, CoreDispatcher dispatcher) : IncrementalLoadingBase<CatalogPackageVersion>(dispatcher)
15+
public sealed partial class PackageVersionSource(CatalogPackage catalogPackage, CoreDispatcher dispatcher) : IncrementalLoadingBase<CatalogPackageVersion>(dispatcher)
1616
{
1717
private List<PackageVersionId> availableVersions;
1818

19+
public override bool HasMoreItems => _hasMoreItems;
20+
1921
/// <summary>
2022
/// The refresh will clear current items, and re-fetch from beginning, so that we will keep a correct page number.
2123
/// </summary>
22-
public virtual async Task Reset()
24+
public async Task Reset()
2325
{
2426
//reset
2527
_currentPage = 1;
@@ -29,7 +31,7 @@ public virtual async Task Reset()
2931
_ = await LoadMoreItemsAsync(15);
3032
}
3133

32-
public virtual async Task Refresh(bool reset = false)
34+
public async Task Refresh(bool reset = false)
3335
{
3436
if (_busy) { return; }
3537
if (reset)
@@ -42,7 +44,7 @@ public virtual async Task Refresh(bool reset = false)
4244
}
4345
}
4446

45-
protected override async Task<uint> LoadMoreItemsOverrideAsync(uint count, CancellationToken cancellationToken)
47+
protected override async ValueTask<uint> LoadMoreItemsOverrideAsync(uint count, CancellationToken cancellationToken)
4648
{
4749
await ThreadSwitcher.ResumeBackgroundAsync();
4850

@@ -90,9 +92,7 @@ protected override async Task<uint> LoadMoreItemsOverrideAsync(uint count, Cance
9092
return loaded;
9193
}
9294

93-
protected override bool HasMoreItemsOverride() => _hasMoreItems;
94-
95-
protected int _currentPage = 1;
96-
protected bool _hasMoreItems = true;
95+
private int _currentPage = 1;
96+
private bool _hasMoreItems = true;
9797
}
9898
}

0 commit comments

Comments
 (0)