-
Notifications
You must be signed in to change notification settings - Fork 500
/
Copy pathIncrementalLoadingBase.h
237 lines (195 loc) · 8.21 KB
/
IncrementalLoadingBase.h
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved
//
// IncrementalLoadingBase.h
// Declaration of the IncrementalLoadingBase class
//
#pragma once
#include <ppltasks.h>
#include <collection.h>
namespace SDKSample
{
namespace DataBinding
{
// This class can used as a jumpstart for implementing ISupportIncrementalLoading.
// Implementing the ISupportIncrementalLoading interfaces allows you to create a list that loads
// more data automatically when the user scrolls to the end of a GridView or ListView.
ref class IncrementalLoadingBase
: Windows::UI::Xaml::Interop::IBindableObservableVector,
Windows::UI::Xaml::Data::ISupportIncrementalLoading
{
#pragma region Windows::UI::Xaml::Data::ISupportIncrementalLoading
internal:
Concurrency::task<Windows::UI::Xaml::Data::LoadMoreItemsResult> LoadMoreItemsAsync(Concurrency::cancellation_token c, unsigned int count)
{
try
{
return Concurrency::task<void>([=]() {})
.then([=]() {
return LoadMoreItemsOverride(c, count);
})
.then([=](Windows::Foundation::Collections::IVector<Platform::Object^>^ items) -> Windows::UI::Xaml::Data::LoadMoreItemsResult {
auto baseIndex = _storage->Size;
for(unsigned int i=0; i<items->Size; i++)
{
_storage->Append(items->GetAt(i));
}
Windows::UI::Xaml::Data::LoadMoreItemsResult result;
result.Count = items->Size;
return result;
}, Concurrency::task_continuation_context::use_current());
}
catch(Platform::Exception^ e)
{
throw e;
}
}
property Platform::Object^ default[int]
{
Platform::Object^ get(int index)
{
return _storage->GetAt(index);
}
void set(int index, Platform::Object^ value)
{
_storage->SetAt(index, value);
}
}
//<snippetISupportIncrementalLoading>
#pragma region Overridable methods
virtual Concurrency::task<Windows::Foundation::Collections::IVector<Platform::Object^>^> LoadMoreItemsOverride(Concurrency::cancellation_token c, unsigned int count)
{
return Concurrency::task<Windows::Foundation::Collections::IVector<Platform::Object^>^>(
[=]() -> Windows::Foundation::Collections::IVector<Platform::Object^>^ {
auto items = ref new Platform::Collections::Vector<Platform::Object^>();
return items;
});
}
virtual bool HasMoreItemsOverride()
{
return false;
}
#pragma endregion
IncrementalLoadingBase()
{
_storage = ref new Platform::Collections::Vector<Platform::Object^>();
_storage->VectorChanged += ref new Windows::Foundation::Collections::VectorChangedEventHandler<Platform::Object^>(this, &IncrementalLoadingBase::_storageVectorChanged);
_busy = false;
_isVectorChangedObserved = false;
}
public:
virtual Windows::Foundation::IAsyncOperation<Windows::UI::Xaml::Data::LoadMoreItemsResult>^ LoadMoreItemsAsync(unsigned int count)
{
if (_busy)
{
throw ref new Platform::FailureException("Only one operation in flight at a time");
}
_busy = true;
return Concurrency::create_async([=](Concurrency::cancellation_token c) {
return LoadMoreItemsAsync(c, count)
.then([=](Windows::UI::Xaml::Data::LoadMoreItemsResult result) -> Windows::UI::Xaml::Data::LoadMoreItemsResult {
_busy = false;
return result;
});
});
}
property bool HasMoreItems
{
virtual bool get() { return HasMoreItemsOverride(); }
}
#pragma endregion
//</snippetISupportIncrementalLoading>
#pragma region IBindableObservableVector
virtual event Windows::UI::Xaml::Interop::BindableVectorChangedEventHandler^ VectorChanged
{
virtual Windows::Foundation::EventRegistrationToken add(Windows::UI::Xaml::Interop::BindableVectorChangedEventHandler^ e)
{
_isVectorChangedObserved = true;
return _privateVectorChanged += e;
}
virtual void remove(Windows::Foundation::EventRegistrationToken t)
{
_privateVectorChanged -= t;
}
virtual void raise(Windows::UI::Xaml::Interop::IBindableObservableVector^ vector, Platform::Object^ e)
{
if(_isVectorChangedObserved)
{
_privateVectorChanged(vector, e);
}
}
}
#pragma endregion
#pragma region Windows::UI::Xaml::Interop::IBindableIterator
virtual Windows::UI::Xaml::Interop::IBindableIterator^ First()
{
return dynamic_cast<Windows::UI::Xaml::Interop::IBindableIterator^>(_storage->First());
}
#pragma endregion
#pragma region Windows::UI::Xaml::Interop::IBindableVector
virtual void Append(Platform::Object^ value)
{
_storage->Append(value);
}
virtual void Clear()
{
_storage->Clear();
}
virtual Platform::Object^ GetAt(unsigned int index)
{
return _storage->GetAt(index);
}
virtual Windows::UI::Xaml::Interop::IBindableVectorView^ GetView()
{
return safe_cast<Windows::UI::Xaml::Interop::IBindableVectorView^>(_storage->GetView());
}
virtual bool IndexOf(Platform::Object^ value, unsigned int* index)
{
return _storage->IndexOf(value, index);
}
virtual void InsertAt(unsigned int index, Platform::Object^ value)
{
_storage->InsertAt(index, value);
}
virtual void RemoveAt(unsigned int index)
{
_storage->RemoveAt(index);
}
virtual void RemoveAtEnd()
{
_storage->RemoveAtEnd();
}
virtual void SetAt(unsigned int index, Platform::Object^ value)
{
_storage->SetAt(index, value);
}
virtual property unsigned int Size
{
unsigned int get() { return _storage->Size; }
}
#pragma endregion
#pragma region State
private:
Platform::Collections::Vector<Platform::Object^>^ _storage;
bool _busy;
bool _isVectorChangedObserved;
event Windows::UI::Xaml::Interop::BindableVectorChangedEventHandler^ _privateVectorChanged;
/// <summary>
///
/// </summary>
/// <param name="e">IVectorChangedEventArgs</param>
void _storageVectorChanged(Windows::Foundation::Collections::IObservableVector<Platform::Object^>^ sender, Windows::Foundation::Collections::IVectorChangedEventArgs^ e)
{
if (_isVectorChangedObserved)
{
VectorChanged(this, e);
}
}
#pragma endregion
};
}
}