-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathDownloadsViewModel.cs
339 lines (279 loc) · 11.2 KB
/
DownloadsViewModel.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//
// Copyright Fela Ameghino 2015-2025
//
// Distributed under the GNU General Public License v3.0. (See accompanying
// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
//
using Rg.DiffUtils;
using System;
using System.Collections.Concurrent;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Telegram.Collections;
using Telegram.Common;
using Telegram.Navigation;
using Telegram.Services;
using Telegram.Td.Api;
using Telegram.Views.Settings;
using Windows.Foundation;
using Windows.UI.Xaml.Data;
namespace Telegram.ViewModels
{
public partial class DownloadsViewModel : ViewModelBase, IHandle
{
private readonly IStorageService _storageService;
public DownloadsViewModel(IClientService clientService, ISettingsService settingsService, IStorageService storageService, IEventAggregator aggregator)
: base(clientService, settingsService, aggregator)
{
_storageService = storageService;
//Items = new ItemCollection(this, string.Empty);
Items = new SearchCollection<FileDownloadViewModel, ItemCollection>(SetSearch, new FileDownloadDiffHandler());
Items.UpdateQuery(string.Empty);
}
public SearchCollection<FileDownloadViewModel, ItemCollection> Items { get; private set; }
private ItemCollection SetSearch(object sender, string query)
{
return new ItemCollection(this, query);
}
private bool _isEmpty = false;
public bool IsEmpty
{
get => _isEmpty;
set => Set(ref _isEmpty, value);
}
private int _totalCompletedCount;
public int TotalCompletedCount
{
get => _totalCompletedCount;
set => Set(ref _totalCompletedCount, value);
}
private int _totalPausedCount;
public int TotalPausedCount
{
get => _totalPausedCount;
set => Set(ref _totalPausedCount, value);
}
private int _totalActiveCount;
public int TotalActiveCount
{
get => _totalActiveCount;
set => Set(ref _totalActiveCount, value);
}
public void Handle(UpdateFileDownload update)
{
if (Items.Source.TryGetValue(update.FileId, out FileDownloadViewModel fileDownload))
{
Dispatcher.Dispatch(() =>
{
if (update.CompleteDate != 0 && update.CompleteDate != fileDownload.CompleteDate)
{
var first = Items.FirstOrDefault(x => x.IsFirst && x.CompleteDate != 0);
var next = Items.IndexOf(first);
var prev = Items.IndexOf(fileDownload);
if (prev == 0 && next > 1)
{
Items[1].IsFirst = true;
}
// If the future position is after the current(supposedly
// it's always the case) we have to decrease the index
// otherwise the item will move after the one.
if (next > prev)
{
next--;
}
if (next != prev)
{
Items.Remove(fileDownload);
Items.Insert(next >= 0 ? next : Items.Count, fileDownload);
}
if (first != null)
{
first.IsFirst = false;
}
fileDownload.IsFirst = true;
}
fileDownload.CompleteDate = update.CompleteDate;
fileDownload.IsPaused = update.IsPaused;
Items.Source.UpdateProperties(update.Counts);
});
}
}
public void Handle(UpdateFileAddedToDownloads update)
{
Dispatcher.Dispatch(() => Items.Source.UpdateProperties(update.Counts));
}
public void Handle(UpdateFileRemovedFromDownloads update)
{
Dispatcher.Dispatch(() => Items.Source.RemoveById(update));
}
public override void Subscribe()
{
Aggregator.Subscribe<UpdateFileDownload>(this, Handle)
.Subscribe<UpdateFileAddedToDownloads>(Handle)
.Subscribe<UpdateFileRemovedFromDownloads>(Handle);
}
public void RemoveAll()
{
ClientService.Send(new RemoveAllFilesFromDownloads(false, false, true));
}
public void ToggleAllPaused()
{
ClientService.Send(new ToggleAllDownloadsArePaused(_totalActiveCount > 0));
}
public void OpenSettings()
{
NavigationService.Navigate(typeof(SettingsStoragePage));
}
public void RemoveFileDownload(FileDownloadViewModel fileDownload)
{
ClientService.Send(new RemoveFileFromDownloads(fileDownload.FileId, true));
}
public void ViewFileDownload(FileDownloadViewModel fileDownload)
{
NavigationService.NavigateToChat(fileDownload.Message.ChatId, message: fileDownload.Message.Id);
}
public async void ShowFileDownload(FileDownloadViewModel fileDownload)
{
var file = await ClientService.SendAsync(new GetFile(fileDownload.FileId)) as File;
if (file != null)
{
await _storageService.OpenFolderAsync(file);
}
}
public partial class ItemCollection : ObservableCollection<FileDownloadViewModel>, ISupportIncrementalLoading
{
private readonly ConcurrentDictionary<int, FileDownloadViewModel> _items = new();
private readonly DownloadsViewModel _viewModel;
private readonly string _query;
private bool _onlyActive;
private bool _onlyCompleted;
private string _offset = string.Empty;
private bool _hasMoreItems = true;
private bool _first = true;
public ItemCollection(DownloadsViewModel viewModel, string query)
{
_viewModel = viewModel;
_query = query ?? string.Empty;
_onlyActive = true;
_onlyCompleted = false;
}
public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
{
return AsyncInfo.Run(async token =>
{
var response = await _viewModel.ClientService.SendAsync(new SearchFileDownloads(_query, _onlyActive, _onlyCompleted, _offset, 100));
if (response is FoundFileDownloads found)
{
foreach (var file in found.Files)
{
var item = new FileDownloadViewModel(file)
{
IsFirst = _first
};
_first = false;
_items[item.FileId] = item;
Add(item);
}
if (string.IsNullOrEmpty(found.NextOffset))
{
_offset = found.NextOffset;
_hasMoreItems = _onlyActive;
_onlyActive = false;
_onlyCompleted = true;
_first = true;
if (_hasMoreItems)
{
return await LoadMoreItemsAsync(count);
}
}
else
{
_offset = found.NextOffset;
}
UpdateProperties(found.TotalCounts);
return new LoadMoreItemsResult { Count = (uint)found.Files.Count };
}
_offset = string.Empty;
_hasMoreItems = false;
UpdateProperties(null);
return new LoadMoreItemsResult { Count = 0 };
});
}
public void UpdateProperties(DownloadedFileCounts counts)
{
_viewModel.TotalCompletedCount = counts?.CompletedCount ?? 0;
_viewModel.TotalPausedCount = counts?.PausedCount ?? 0;
_viewModel.TotalActiveCount = counts?.ActiveCount ?? 0;
_viewModel.IsEmpty = _items.Count == 0;
}
public bool HasMoreItems => _hasMoreItems;
public void RemoveById(UpdateFileRemovedFromDownloads update)
{
if (_items.TryRemove(update.FileId, out FileDownloadViewModel fileDownload))
{
Remove(fileDownload);
}
UpdateProperties(update.Counts);
}
public bool TryGetValue(int fileId, out FileDownloadViewModel fileDownload)
{
return _items.TryGetValue(fileId, out fileDownload);
}
}
}
public partial class FileDownloadViewModel : BindableBase
{
private readonly FileDownload _fileDownload;
public FileDownloadViewModel(FileDownload fileDownload)
{
_fileDownload = fileDownload;
}
private bool _isFirst;
public bool IsFirst
{
get => _isFirst;
set => Set(ref _isFirst, value);
}
/// <summary>
/// True, if downloading of the file is paused.
/// </summary>
public bool IsPaused { get => _fileDownload.IsPaused; set => _fileDownload.IsPaused = value; }
/// <summary>
/// Point in time (Unix timestamp) when the file downloading was completed; 0 if
/// the file downloading isn't completed.
/// </summary>
public int CompleteDate
{
get => _fileDownload.CompleteDate;
set
{
_fileDownload.CompleteDate = value;
RaisePropertyChanged(nameof(CompleteDate));
}
}
/// <summary>
/// Point in time (Unix timestamp) when the file was added to the download list.
/// </summary>
public int AddDate => _fileDownload.AddDate;
/// <summary>
/// The message with the file.
/// </summary>
public Message Message => _fileDownload.Message;
/// <summary>
/// File identifier.
/// </summary>
public int FileId => _fileDownload.FileId;
}
public partial class FileDownloadDiffHandler : IDiffHandler<FileDownloadViewModel>
{
public bool CompareItems(FileDownloadViewModel oldItem, FileDownloadViewModel newItem)
{
return oldItem?.FileId == newItem?.FileId;
}
public void UpdateItem(FileDownloadViewModel oldItem, FileDownloadViewModel newItem)
{
oldItem.IsFirst = newItem.IsFirst;
}
}
}