-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVirtualCollection.cs
executable file
·115 lines (97 loc) · 3.23 KB
/
VirtualCollection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace MobCAT.MVVM
{
public class VirtualCollection<TItem> : ObservableCollection<TItem>
{
public int VirtualPage => Count > 0 ? (((Count - 1) / VirtualPageSize) + 1) : 0;
public bool FullyLoaded => Count >= VirtualCount;
private int _virtualPageSize = 25;
public int VirtualPageSize
{
get { return _virtualPageSize; }
set
{
if (value <= 0)
{
throw new ArgumentException($"Unable to set {nameof(VirtualPageSize)} smaller than 0");
}
if (RaiseAndUpdate(ref _virtualPageSize, value))
{
Raise(nameof(VirtualPage));
Raise(nameof(FullyLoaded));
}
}
}
private int _virtualCount;
public int VirtualCount
{
get { return _virtualCount; }
set
{
// We cannot set new Virtual count lower than number of already loaded items
var newValue = Math.Max(value, Count);
if (RaiseAndUpdate(ref _virtualCount, newValue))
{
Raise(nameof(VirtualPage));
Raise(nameof(FullyLoaded));
}
}
}
public VirtualCollection()
{
}
public VirtualCollection(IEnumerable<TItem> collection)
: base(collection)
{
}
public void AddPage(IEnumerable<TItem> collection, int? virtualCount = null, int? pageNumber = null, int? pageSize = null)
{
foreach (var article in collection)
{
this.Add(article);
}
if (virtualCount != null)
{
VirtualCount = virtualCount.Value;
}
if (pageSize != null)
{
VirtualPageSize = pageSize.Value;
}
}
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
base.OnCollectionChanged(e);
if (Count > VirtualCount)
{
VirtualCount = Count;
}
else
{
Raise(nameof(VirtualPage));
Raise(nameof(FullyLoaded));
}
// System.Diagnostics.Debug.WriteLine($"Count = {Count} | VirtualCount = {VirtualCount} | VirtualPageSize {VirtualPageSize} | VirtualPage = {VirtualPage}");
}
protected bool RaiseAndUpdate<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value))
return false;
field = value;
Raise(propertyName);
return true;
}
protected void Raise(string propertyName)
{
if (!string.IsNullOrEmpty(propertyName))
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
}
}
}