forked from microsoft/Appsample-Photosharing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IncrementalLoadingCollection.cs
154 lines (137 loc) · 6.01 KB
/
IncrementalLoadingCollection.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
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
//-----------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ---------------------------------------------------------------------------------
using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using PhotoSharingApp.Portable.DataContracts;
using PhotoSharingApp.Universal.Models;
using Windows.Foundation;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;
namespace PhotoSharingApp.Universal.ComponentModel
{
/// <summary>
/// Observable collection that supports loading data incrementally.
/// </summary>
/// <typeparam name="T">The data type.</typeparam>
public class IncrementalLoadingCollection<T> : ObservableCollection<T>, ISupportIncrementalLoading
{
private string _continuationToken;
private readonly Action _errorFunction;
private readonly Action _loadingFinished;
private readonly Func<string, Task<PagedResponse<T>>> _loadingFunction;
/// <summary>
/// Creates a new instance.
/// </summary>
/// <param name="loadDataFunction">The delegate that is used to load data.</param>
/// <param name="errorFunction">The delegate that is called when an error occurs while loading data.</param>
/// <param name="loadingFinished">The delegate that is called when loading data has been finished.</param>
public IncrementalLoadingCollection(Func<string, Task<PagedResponse<T>>> loadDataFunction,
Action errorFunction, Action loadingFinished = null)
{
_loadingFunction = loadDataFunction;
_errorFunction = errorFunction;
_loadingFinished = loadingFinished;
}
/// <summary>
/// Determines if more data is available to load.
/// </summary>
public bool HasMoreItems { get; private set; } = true;
/// <summary>
/// Raised when all data items have been loaded
/// and added to the list.
/// </summary>
public event EventHandler<EventArgs> LoadingFinished;
/// <summary>
/// Initializes incremental loading from the view.
/// </summary>
/// <param name="count">The number of items to load.</param>
/// <returns>
/// The wrapped results of the load operation.
/// </returns>
public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
{
var dispatcher = Window.Current.Dispatcher;
return
Task.Run(async () =>
{
try
{
// Load more data without continuation token
var response = await _loadingFunction(_continuationToken);
// Add loaded elements to list. This needs to be done
// in UI thread.
await dispatcher.RunAsync(
CoreDispatcherPriority.Normal,
() =>
{
foreach (var item in response.Items)
{
Add(item);
}
});
_continuationToken = response.ContinuationToken;
// No more loading if continuation token is null.
HasMoreItems = _continuationToken != null;
await dispatcher.RunAsync(
CoreDispatcherPriority.Normal,
OnLoadingFinished);
return new LoadMoreItemsResult
{
Count = (uint) response.Items.Count
};
}
catch (Exception)
{
if (_errorFunction != null)
{
// Stop re-trying automatically if it fails.
// Otherwise we'll get recurring error messages.
HasMoreItems = false;
_errorFunction();
}
}
return new LoadMoreItemsResult
{
Count = 0
};
}).AsAsyncOperation();
}
private void OnLoadingFinished()
{
_loadingFinished?.Invoke();
LoadingFinished?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// Reloads the list with the specified number of elements.
/// </summary>
/// <param name="count">The number of elements to load.</param>
public async Task Refresh(uint count = 10)
{
Clear();
await LoadMoreItemsAsync(count);
}
}
}