Skip to content

Commit a4d4f76

Browse files
extensions.
1 parent 9139eb8 commit a4d4f76

13 files changed

Lines changed: 125 additions & 12 deletions

AlphaChiTech.Virtualization/PagedSourceProviderMakeAsync.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace AlphaChiTech.Virtualization
88
{
9-
public class PagedSourceProviderMakeAsync<T> : BasePagedSourceProvider<T>, IPagedSourceProviderAsync<T>
9+
public class PagedSourceProviderMakeAsync<T> : BasePagedSourceProvider<T>, IPagedSourceProviderAsync<T>, IProviderPreReset
1010
{
1111
public PagedSourceProviderMakeAsync()
1212
{
@@ -17,11 +17,29 @@ public PagedSourceProviderMakeAsync(
1717
Func<int> funcGetCount = null,
1818
Func<T, int> funcIndexOf = null,
1919
Action<int> actionOnReset = null,
20-
Func<int, int, int, T> funcGetPlaceHolder = null
20+
Func<int, int, int, T> funcGetPlaceHolder = null,
21+
Action actionOnBeforeReset = null
2122
)
2223
: base(funcGetItemsAt, funcGetCount, funcIndexOf, actionOnReset)
2324
{
2425
this.FuncGetPlaceHolder = funcGetPlaceHolder;
26+
this.ActionOnBeforeReset = actionOnBeforeReset;
27+
}
28+
29+
public virtual void OnBeforeReset()
30+
{
31+
if (this.ActionOnBeforeReset != null)
32+
{
33+
this.ActionOnBeforeReset.Invoke();
34+
}
35+
}
36+
37+
Action _ActionOnBeforeReset = null;
38+
39+
public Action ActionOnBeforeReset
40+
{
41+
get { return _ActionOnBeforeReset; }
42+
set { _ActionOnBeforeReset = value; }
2543
}
2644

2745
private Func<int, int, int, T> _FuncGetPlaceHolder = null;
@@ -32,6 +50,7 @@ public Func<int, int, int, T> FuncGetPlaceHolder
3250
set { _FuncGetPlaceHolder = value; }
3351
}
3452

53+
3554
public Task<PagedSourceItemsPacket<T>> GetItemsAtAsync(int pageoffset, int count, bool usePlaceholder)
3655
{
3756
var tcs = new TaskCompletionSource<PagedSourceItemsPacket<T>>();

AlphaChiTech.Virtualization/PagedSourceProviderMakeSync.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace AlphaChiTech.Virtualization
88
{
9-
public class PagedSourceProviderMakeSync<T> : IPagedSourceProviderAsync<T>
9+
public class PagedSourceProviderMakeSync<T> : IPagedSourceProviderAsync<T>, IProviderPreReset
1010
{
1111
public PagedSourceProviderMakeSync()
1212
{
@@ -18,7 +18,8 @@ public PagedSourceProviderMakeSync(
1818
Func<T, int> funcIndexOf = null,
1919
Func<T, Task<int>> funcIndexOfAsync = null,
2020
Action<int> actionOnReset = null,
21-
Func<int, int, int, T> funcGetPlaceHolder = null
21+
Func<int, int, int, T> funcGetPlaceHolder = null,
22+
Action actionOnBeforeReset = null
2223
)
2324
{
2425
this.FuncGetItemsAtAsync = funcGetItemsAtAsync;
@@ -27,8 +28,26 @@ public PagedSourceProviderMakeSync(
2728
this.FuncIndexOfAsync = funcIndexOfAsync;
2829
this.ActionOnReset = actionOnReset;
2930
this.FuncGetPlaceHolder = funcGetPlaceHolder;
31+
this.ActionOnBeforeReset = actionOnBeforeReset;
3032
}
3133

34+
public virtual void OnBeforeReset()
35+
{
36+
if(this.ActionOnBeforeReset != null)
37+
{
38+
this.ActionOnBeforeReset.Invoke();
39+
}
40+
}
41+
42+
Action _ActionOnBeforeReset = null;
43+
44+
public Action ActionOnBeforeReset
45+
{
46+
get { return _ActionOnBeforeReset; }
47+
set { _ActionOnBeforeReset = value; }
48+
}
49+
50+
3251
Func<T, Task<int>> _FuncIndexOfAsync = null;
3352

3453
public Func<T, Task<int>> FuncIndexOfAsync

AlphaChiTech.Virtualization/PaginationManager.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,17 @@
99
namespace AlphaChiTech.Virtualization
1010
{
1111

12-
public class PaginationManager<T> : IItemSourceProvider<T>, IEditableProvider<T>, IReclaimableService, INotifyCountChanged
12+
public interface IAsyncResetProvider
13+
{
14+
Task<int> GetCountAsync();
15+
}
16+
17+
public interface IProviderPreReset
18+
{
19+
void OnBeforeReset();
20+
}
21+
22+
public class PaginationManager<T> : IItemSourceProvider<T>, IEditableProvider<T>, IReclaimableService, IAsyncResetProvider, IProviderPreReset, INotifyCountChanged
1323
{
1424
Dictionary<int, ISourcePage<T>> _Pages = new Dictionary<int, ISourcePage<T>>();
1525

@@ -218,6 +228,7 @@ protected void DropAllDeltasAndPages()
218228
{
219229
_Deltas.Clear();
220230
_Pages.Clear();
231+
_BasePage = 0;
221232
CancelAllRequests();
222233
}
223234
}
@@ -810,6 +821,25 @@ public int GetCount(bool asyncOK)
810821

811822
}
812823

824+
public async Task<int> GetCountAsync()
825+
{
826+
int ret = 0;
827+
828+
829+
if (!IsAsync)
830+
{
831+
ret = this.Provider.Count;
832+
}
833+
else
834+
{
835+
ret = await this.ProviderAsync.GetCountAsync();
836+
}
837+
838+
_HasGotCount = true;
839+
840+
return ret;
841+
}
842+
813843
private async void GetCountAsync(CancellationTokenSource cts)
814844
{
815845
if (!cts.IsCancellationRequested)
@@ -897,6 +927,24 @@ public void OnReset(int count)
897927
RaiseCountChanged(true, count);
898928
}
899929

930+
public void OnBeforeReset()
931+
{
932+
if(!IsAsync)
933+
{
934+
if(this.Provider is IProviderPreReset)
935+
{
936+
(this.Provider as IProviderPreReset).OnBeforeReset();
937+
}
938+
}
939+
else
940+
{
941+
if(this.ProviderAsync is IProviderPreReset)
942+
{
943+
(this.ProviderAsync as IProviderPreReset).OnBeforeReset();
944+
}
945+
}
946+
}
947+
900948
/// <summary>
901949
/// Raises the count changed.
902950
/// </summary>

AlphaChiTech.Virtualization/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@
2626
// You can specify all the values or you can default the Build and Revision Numbers
2727
// by using the '*' as shown below:
2828
// [assembly: AssemblyVersion("1.0.*")]
29-
[assembly: AssemblyVersion("1.1.3.0")]
30-
[assembly: AssemblyFileVersion("1.1.3.0")]
29+
[assembly: AssemblyVersion("1.1.5.0")]
30+
[assembly: AssemblyFileVersion("1.1.5.0")]

AlphaChiTech.Virtualization/VirtualizingObservableCollection.cs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -682,9 +682,17 @@ void InternalClear()
682682
{
683683
if(this.Provider != null)
684684
{
685+
if (this.Provider is IProviderPreReset)
686+
{
687+
(this.Provider as IProviderPreReset).OnBeforeReset();
688+
}
685689
this.Provider.OnReset(this.Provider.GetCount(false));
686690
} else
687691
{
692+
if (this.ProviderAsync is IProviderPreReset)
693+
{
694+
(this.ProviderAsync as IProviderPreReset).OnBeforeReset();
695+
}
688696
this.ProviderAsync.OnReset(Task.Run(() => this.ProviderAsync.Count).Result);
689697
}
690698
}
@@ -693,19 +701,38 @@ public async void ResetAsync()
693701
{
694702
if(this.Provider != null)
695703
{
704+
if (this.Provider is IProviderPreReset)
705+
{
706+
(this.Provider as IProviderPreReset).OnBeforeReset();
707+
}
696708
this.Provider.OnReset(-1);
697709

698-
Task.Run(() =>
710+
Task.Run( async () =>
699711
{
700-
int count = this.Provider.GetCount(false);
701-
VirtualizationManager.Instance.RunOnUI(() =>
702-
this.Provider.OnReset(count)
703-
);
712+
if (this.Provider is IAsyncResetProvider)
713+
{
714+
int count = await (this.Provider as IAsyncResetProvider).GetCountAsync();
715+
VirtualizationManager.Instance.RunOnUI(() =>
716+
this.Provider.OnReset(count)
717+
);
718+
719+
}
720+
else
721+
{
722+
int count = this.Provider.GetCount(false);
723+
VirtualizationManager.Instance.RunOnUI(() =>
724+
this.Provider.OnReset(count)
725+
);
726+
}
704727
});
705728

706729
}
707730
else
708731
{
732+
if (this.ProviderAsync is IProviderPreReset)
733+
{
734+
(this.ProviderAsync as IProviderPreReset).OnBeforeReset();
735+
}
709736
this.ProviderAsync.OnReset(await this.ProviderAsync.Count);
710737
}
711738
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)