-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathFolderViewModel.cs
628 lines (536 loc) · 21.2 KB
/
FolderViewModel.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
//
// 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.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Telegram.Collection;
using Telegram.Collections;
using Telegram.Common;
using Telegram.Controls.Media;
using Telegram.Navigation;
using Telegram.Navigation.Services;
using Telegram.Services;
using Telegram.Td.Api;
using Telegram.Views.Folders;
using Telegram.Views.Folders.Popups;
using Telegram.Views.Popups;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace Telegram.ViewModels.Folders
{
public partial class FolderViewModel : ViewModelBase, IDiffHandler<ChatFolderElement>
{
public FolderViewModel(IClientService clientService, ISettingsService settingsService, IEventAggregator aggregator)
: base(clientService, settingsService, aggregator)
{
Include = new BatchedObservableCollection<ChatFolderElement>(8, this, Constants.DiffOptions);
Exclude = new BatchedObservableCollection<ChatFolderElement>(8, this, Constants.DiffOptions);
AvailableColors = new ObservableCollection<NameColor>(ClientService.GetAvailableAccentColors()
.Where(x => x.Id == x.BuiltInAccentColorId)
.Append(new NameColor(-1)));
Links = new MvxObservableCollection<ChatFolderInviteLink>();
}
public bool CompareItems(ChatFolderElement oldItem, ChatFolderElement newItem)
{
if (oldItem is FolderFlag oldFlag && newItem is FolderFlag newFlag)
{
return oldFlag.Flag == newFlag.Flag;
}
else if (oldItem is FolderChat oldChat && newItem is FolderChat newChat)
{
return oldChat.ChatId == newChat.ChatId;
}
return false;
}
public void UpdateItem(ChatFolderElement oldItem, ChatFolderElement newItem)
{
}
protected override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, NavigationState state)
{
ChatFolder folder = null;
if (parameter is int id)
{
var response = await ClientService.SendAsync(new GetChatFolder(id));
if (response is ChatFolder result)
{
Id = id;
IsShareable = result.IsShareable;
Folder = result;
folder = result;
}
else
{
// TODO
}
}
else
{
Id = null;
IsShareable = false;
Folder = null;
folder = new ChatFolder();
folder.PinnedChatIds = new List<long>();
folder.IncludedChatIds = new List<long>();
folder.ExcludedChatIds = new List<long>();
if (parameter is FolderPageCreateArgs createArgs)
{
folder.IncludedChatIds.Add(createArgs.IncludeChatId);
}
}
if (folder == null)
{
return;
}
_pinnedChatIds = folder.PinnedChatIds;
_iconPicked = !string.IsNullOrEmpty(folder.Icon?.Name);
_originalColorId = folder.ColorId;
//Title = new FormattedText(folder.Title, Array.Empty<TextEntity>());
Title = folder.Name?.Text ?? string.Empty.AsFormattedText();
AnimateCustomEmoji = folder.Name?.AnimateCustomEmoji ?? true;
Icon = Icons.ParseFolder(folder);
SelectedColor = IsPremium && folder.ColorId != -1
? ClientService.GetAccentColor(folder.ColorId)
: AvailableColors[^1];
Links.Clear();
Include.Clear();
Exclude.Clear();
var includeFlags = new List<ChatFolderElement>();
var includeChats = new List<FolderChat>();
var excludeFlags = new List<ChatFolderElement>();
var excludeChats = new List<FolderChat>();
if (folder.IncludeContacts) includeFlags.Add(new FolderFlag(ChatListFolderFlags.IncludeContacts));
if (folder.IncludeNonContacts) includeFlags.Add(new FolderFlag(ChatListFolderFlags.IncludeNonContacts));
if (folder.IncludeGroups) includeFlags.Add(new FolderFlag(ChatListFolderFlags.IncludeGroups));
if (folder.IncludeChannels) includeFlags.Add(new FolderFlag(ChatListFolderFlags.IncludeChannels));
if (folder.IncludeBots) includeFlags.Add(new FolderFlag(ChatListFolderFlags.IncludeBots));
if (folder.ExcludeMuted) excludeFlags.Add(new FolderFlag(ChatListFolderFlags.ExcludeMuted));
if (folder.ExcludeRead) excludeFlags.Add(new FolderFlag(ChatListFolderFlags.ExcludeRead));
if (folder.ExcludeArchived) excludeFlags.Add(new FolderFlag(ChatListFolderFlags.ExcludeArchived));
foreach (var chatId in folder.PinnedChatIds.Union(folder.IncludedChatIds))
{
includeChats.Add(new FolderChat(chatId));
}
foreach (var chatId in folder.ExcludedChatIds)
{
excludeChats.Add(new FolderChat(chatId));
}
Include.ReplaceDiff(includeFlags.Union(includeChats.OrderBy(x => x.ChatId)));
Exclude.ReplaceDiff(excludeFlags.Union(excludeChats.OrderBy(x => x.ChatId)));
UpdateIcon();
UpdateLinks();
RaisePropertyChanged(nameof(HasChanged));
}
public override async void NavigatingFrom(NavigatingEventArgs args)
{
if (!_completed && HasChanged)
{
var message = Id.HasValue
? Strings.FilterDiscardAlert
: Strings.FilterDiscardNewAlert;
var title = Id.HasValue
? Strings.FilterDiscardTitle
: Strings.FilterDiscardNewTitle;
var primary = Id.HasValue
? Strings.ApplyTheme
: Strings.FilterDiscardNewSave;
args.Cancel = true;
var confirm = await ShowPopupAsync(message, title, primary, Strings.PassportDiscard);
if (confirm == ContentDialogResult.Primary)
{
Continue();
}
else if (confirm == ContentDialogResult.Secondary)
{
_completed = true;
NavigationService.GoBack();
}
}
}
public int? Id { get; set; }
private ChatFolder _folder;
public ChatFolder Folder
{
get => _folder;
set => Set(ref _folder, value);
}
private FormattedText _title;
public FormattedText Title
{
get => _title;
set
{
if (_title.AreTheSame(value))
{
return;
}
Invalidate(ref _title, value);
RaisePropertyChanged(nameof(Name));
RaisePropertyChanged(nameof(HasCustomEmoji));
}
}
private bool _animateCustomEmoji;
public bool AnimateCustomEmoji
{
get => _animateCustomEmoji;
set
{
if (Invalidate(ref _animateCustomEmoji, value))
{
RaisePropertyChanged(nameof(Name));
RaisePropertyChanged(nameof(HasCustomEmoji));
}
}
}
public bool HasCustomEmoji => Title?.Entities?.Count > 0;
public ChatFolderName Name => new ChatFolderName(Title, AnimateCustomEmoji);
private bool _isShareable;
public bool IsShareable
{
get => _isShareable;
set => Invalidate(ref _isShareable, value);
}
private bool _iconPicked;
private int _originalColorId = -1;
private ChatFolderIcon2 _icon;
public ChatFolderIcon2 Icon
{
get => _icon;
private set => Invalidate(ref _icon, value);
}
public void SetIcon(ChatFolderIcon2 icon)
{
_iconPicked = true;
Icon = icon;
}
private void UpdateIcon()
{
if (_iconPicked)
{
return;
}
Icon = Icons.ParseFolder(GetFolder());
}
public ObservableCollection<NameColor> AvailableColors { get; }
private NameColor _selectedColor;
public NameColor SelectedColor
{
get => _selectedColor;
set => Invalidate(ref _selectedColor, value);
}
private async void UpdateLinks()
{
if (Id is int id)
{
var response = await ClientService.SendAsync(new GetChatFolderInviteLinks(id));
if (response is ChatFolderInviteLinks links && links.InviteLinks.Count > 0)
{
Links.ReplaceWith(links.InviteLinks);
Exclude.Clear();
Exclude.SynchronizeHead();
IsShareable = true;
}
else
{
Links.Clear();
IsShareable = false;
}
}
}
private IList<long> _pinnedChatIds = Array.Empty<long>();
public BatchedObservableCollection<ChatFolderElement> Include { get; private set; }
public BatchedObservableCollection<ChatFolderElement> Exclude { get; private set; }
public MvxObservableCollection<ChatFolderInviteLink> Links { get; private set; }
public async void AddIncluded()
{
await AddIncludeAsync();
UpdateIcon();
RaisePropertyChanged(nameof(HasChanged));
}
public async Task AddIncludeAsync()
{
var result = await ChooseChatsPopup.AddExecute(NavigationService, true, _folder == null || (!_folder.IsShareable && Links.Count == 0), false, Include.ToList());
if (result != null)
{
foreach (var item in result.OfType<FolderChat>())
{
var already = Exclude.OfType<FolderChat>().FirstOrDefault(x => x.ChatId == item.ChatId);
if (already != null)
{
Exclude.Remove(already);
}
}
var flags = result.OfType<FolderFlag>().Cast<ChatFolderElement>();
var chats = result.OfType<FolderChat>().OrderBy(x => x.ChatId);
Include.ReplaceDiff(flags.Union(chats));
Exclude.SynchronizeHead();
}
}
public async void AddExcluded()
{
await AddExcludeAsync();
UpdateIcon();
RaisePropertyChanged(nameof(HasChanged));
}
public async Task AddExcludeAsync()
{
var result = await ChooseChatsPopup.AddExecute(NavigationService, false, true, false, Exclude.ToList());
if (result != null)
{
foreach (var item in result.OfType<FolderChat>())
{
var already = Include.OfType<FolderChat>().FirstOrDefault(x => x.ChatId == item.ChatId);
if (already != null)
{
Include.Remove(already);
}
}
var flags = result.OfType<FolderFlag>().Cast<ChatFolderElement>();
var chats = result.OfType<FolderChat>().OrderBy(x => x.ChatId);
Include.SynchronizeHead();
Exclude.ReplaceDiff(flags.Union(chats));
}
}
public void RemoveIncluded(ChatFolderElement chat)
{
Include.Remove(chat);
Include.SynchronizeHead();
UpdateIcon();
RaisePropertyChanged(nameof(HasChanged));
}
public void RemoveExcluded(ChatFolderElement chat)
{
Exclude.Remove(chat);
Exclude.SynchronizeHead();
UpdateIcon();
RaisePropertyChanged(nameof(HasChanged));
}
private ChatFolder _cached;
private bool _completed;
public bool HasChanged => CanBeSaved && (_folder == null || !_folder.AreTheSame(GetFolder()));
public bool CanBeSaved => !string.IsNullOrEmpty(Title?.Text) && Include.Count > 0;
protected bool Invalidate<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Set(ref storage, value, propertyName))
{
RaisePropertyChanged(nameof(HasChanged));
return true;
}
return false;
}
public async void Continue()
{
var response = await SendAsync();
if (response is ChatFolderInfo)
{
_completed = true;
NavigationService.GoBack();
}
}
public Task<BaseObject> SendAsync()
{
Function function;
if (Id is int id)
{
function = new EditChatFolder(id, GetFolder());
}
else
{
function = new CreateChatFolder(GetFolder());
}
return ClientService.SendAsync(function);
}
private ChatFolder GetFolder()
{
var folder = new ChatFolder();
folder.Name = new ChatFolderName(Title, AnimateCustomEmoji);
folder.Icon = new ChatFolderIcon(_iconPicked ? Enum.GetName(typeof(ChatFolderIcon2), Icon) : string.Empty);
folder.ColorId = IsPremium ? SelectedColor?.Id ?? -1 : _originalColorId;
folder.IsShareable = IsShareable;
folder.PinnedChatIds = new List<long>();
folder.IncludedChatIds = new List<long>();
folder.ExcludedChatIds = new List<long>();
foreach (var item in _pinnedChatIds)
{
if (Include.Contains(new FolderChat(item)))
{
folder.PinnedChatIds.Add(item);
}
}
foreach (var item in Include)
{
if (item is FolderFlag flag)
{
switch (flag.Flag)
{
case ChatListFolderFlags.IncludeContacts:
folder.IncludeContacts = true;
break;
case ChatListFolderFlags.IncludeNonContacts:
folder.IncludeNonContacts = true;
break;
case ChatListFolderFlags.IncludeGroups:
folder.IncludeGroups = true;
break;
case ChatListFolderFlags.IncludeChannels:
folder.IncludeChannels = true;
break;
case ChatListFolderFlags.IncludeBots:
folder.IncludeBots = true;
break;
}
}
else if (item is FolderChat chat && !folder.PinnedChatIds.Contains(chat.ChatId))
{
folder.IncludedChatIds.Add(chat.ChatId);
}
}
foreach (var item in Exclude)
{
if (item is FolderFlag flag)
{
switch (flag.Flag)
{
case ChatListFolderFlags.ExcludeMuted:
folder.ExcludeMuted = true;
break;
case ChatListFolderFlags.ExcludeRead:
folder.ExcludeRead = true;
break;
case ChatListFolderFlags.ExcludeArchived:
folder.ExcludeArchived = true;
break;
}
}
else if (item is FolderChat chat)
{
folder.ExcludedChatIds.Add(chat.ChatId);
}
}
return folder;
}
public async void OpenLink(ChatFolderInviteLink link)
{
var tsc = new TaskCompletionSource<object>();
var confirm = await ShowPopupAsync(new ShareFolderPopup(tsc), Tuple.Create(Id.Value, link));
if (confirm == ContentDialogResult.Primary)
{
var result = await tsc.Task;
if (result is IList<long> chats)
{
if (link != null)
{
result = await ClientService.SendAsync(new EditChatFolderInviteLink(Id.Value, link.InviteLink, string.Empty, chats));
}
else
{
result = await ClientService.SendAsync(new CreateChatFolderInviteLink(Id.Value, string.Empty, chats));
}
if (result is ChatFolderInviteLink inviteLink)
{
Links.Insert(0, inviteLink);
Exclude.Clear();
Exclude.SynchronizeHead();
IsShareable = true;
}
}
}
}
public async void CreateLink()
{
if (string.IsNullOrEmpty(Title?.Text))
{
ShowPopup(Strings.FilterInviteErrorEmptyName, Strings.AppName, Strings.OK);
return;
}
if (Exclude.Any())
{
ShowPopup(Strings.FilterInviteErrorExcluded, Strings.AppName, Strings.OK);
return;
}
if (Include.Any(x => x is FolderFlag) || Include.Empty())
{
ShowPopup(Strings.FilterInviteErrorTypes, Strings.AppName, Strings.OK);
return;
}
if (Id == null)
{
// TODO: IMHO folder should be created here
ShowPopup(Strings.FilterFinishCreating, Strings.AppName, Strings.OK);
return;
}
var shareableItems = Include.OfType<FolderChat>()
.Select(x => x.ChatId)
.ToList();
for (int i = 0; i < shareableItems.Count; i++)
{
var chat = ClientService.GetChat(shareableItems[i]);
if (chat.Permissions.CanInviteUsers)
{
continue;
}
else if (ClientService.TryGetSupergroup(chat, out Supergroup supergroup))
{
if (supergroup.CanInviteUsers())
{
continue;
}
else if (supergroup.HasActiveUsername() && !supergroup.JoinByRequest)
{
continue;
}
}
else if (ClientService.TryGetBasicGroup(chat, out BasicGroup basicGroup))
{
if (basicGroup.CanInviteUsers())
{
continue;
}
}
shareableItems.RemoveAt(i);
i--;
}
if (shareableItems.Count > 0)
{
var response = await ClientService.SendAsync(new CreateChatFolderInviteLink(Id.Value, string.Empty, shareableItems));
if (response is ChatFolderInviteLink link)
{
OpenLink(link);
}
else if (response is Error error)
{
if (error.MessageEquals(ErrorType.USER_CHANNELS_TOO_MUCH))
{
ShowPopup(Strings.FolderLinkOtherAdminLimitError, Strings.AppName, Strings.OK);
}
else if (error.MessageEquals(ErrorType.CHANNELS_TOO_MUCH))
{
NavigationService.ShowLimitReached(new PremiumLimitTypeSupergroupCount());
}
else if (error.MessageEquals(ErrorType.INVITES_TOO_MUCH))
{
NavigationService.ShowLimitReached(new PremiumLimitTypeChatFolderInviteLinkCount());
}
else if (error.MessageEquals(ErrorType.CHATLISTS_TOO_MUCH))
{
NavigationService.ShowLimitReached(new PremiumLimitTypeShareableChatFolderCount());
}
}
}
else
{
OpenLink(null);
}
}
}
public record ChatFolderElement;
public record FolderFlag(ChatListFolderFlags Flag) : ChatFolderElement;
public record FolderChat(long ChatId) : ChatFolderElement;
}