-
-
Notifications
You must be signed in to change notification settings - Fork 679
Expand file tree
/
Copy pathProfileTabsViewModel.cs
More file actions
441 lines (374 loc) · 17 KB
/
Copy pathProfileTabsViewModel.cs
File metadata and controls
441 lines (374 loc) · 17 KB
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
//
// Copyright (c) Fela Ameghino 2015-2026
//
// 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 System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using Telegram.Collections;
using Telegram.Common;
using Telegram.Navigation;
using Telegram.Navigation.Services;
using Telegram.Services;
using Telegram.Td.Api;
using Telegram.Views.Chats;
using Telegram.Views.Profile;
using Windows.UI.Xaml.Navigation;
namespace Telegram.ViewModels.Profile
{
public class ProfileTabArchivedPosts : ProfileTab
{
public override string ToString()
{
return nameof(ProfileTabArchivedPosts);
}
}
public class ProfileTabSavedChats : ProfileTab
{
public override string ToString()
{
return nameof(ProfileTabSavedChats);
}
}
public class ProfileTabTopics : ProfileTab
{
public override string ToString()
{
return nameof(ProfileTabTopics);
}
}
public class ProfileTabPreviews : ProfileTab
{
public override string ToString()
{
return nameof(ProfileTabPreviews);
}
}
public class ProfileTabGroups : ProfileTab
{
public override string ToString()
{
return nameof(ProfileTabGroups);
}
}
public class ProfileTabSimilarBots : ProfileTab
{
public override string ToString()
{
return nameof(ProfileTabSimilarBots);
}
}
public class ProfileTabSimilarChannels : ProfileTab
{
public override string ToString()
{
return nameof(ProfileTabSimilarChannels);
}
}
public class ProfileTabMembers : ProfileTab
{
public override string ToString()
{
return nameof(ProfileTabMembers);
}
}
public class ProfileTabSavedMessages : ProfileTab
{
public override string ToString()
{
return nameof(ProfileTabSavedMessages);
}
}
public class ProfileTabPolls : ProfileTab
{
public override string ToString()
{
return nameof(ProfileTabPolls);
}
}
public partial class ProfileTabItem : BindableBase
{
private readonly ICollectionWithTotalCount _items;
private readonly int _totalCount;
private readonly string _locale;
public ProfileTabItem(ProfileTab type, object parameter = null)
{
(Text, PageType) = GetText(type);
Type = type;
Parameter = parameter;
}
public ProfileTabItem(ProfileTab type, object parameter, int totalCount, string locale)
{
(Text, PageType) = GetText(type);
Type = type;
Parameter = parameter;
_totalCount = totalCount;
_locale = locale;
}
public ProfileTabItem(ProfileTab type, object parameter, ICollectionWithTotalCount items, string locale)
{
(Text, PageType) = GetText(type);
Type = type;
Parameter = parameter;
_items = items;
_items.PropertyChanged += OnPropertyChanged;
_locale = locale;
}
private (string, Type) GetText(ProfileTab type)
{
return type switch
{
ProfileTabPosts => (Strings.ProfileStories, typeof(ProfileStoriesTabPage)),
ProfileTabGifts => (Strings.ProfileGifts, typeof(ProfileGiftsTabPage)),
ProfileTabArchivedPosts => (Strings.ArchivedStories, typeof(ProfileStoriesTabPage)),
ProfileTabSavedChats => (Strings.SavedDialogsTab, typeof(ProfileSavedChatsTabPage)),
ProfileTabTopics => (Strings.Topics, typeof(ProfileTopicsTabPage)),
ProfileTabPreviews => (Strings.ProfileBotPreviewTab, typeof(ProfileStoriesTabPage)),
ProfileTabGroups => (Strings.SharedGroupsTab2, typeof(ProfileGroupsTabPage)),
ProfileTabSimilarBots => (Strings.SimilarBotsTab, typeof(ProfileBotsTabPage)),
ProfileTabSimilarChannels => (Strings.SimilarChannelsTab, typeof(ProfileChannelsTabPage)),
ProfileTabMembers => (Strings.ChannelMembers, typeof(ProfileMembersTabPage)),
ProfileTabMedia => (Strings.SharedMediaTab2, typeof(ProfileMediaTabPage)),
ProfileTabSavedMessages => (Strings.SavedMessagesTab2, typeof(ProfileSavedMessagesTabPage)),
ProfileTabFiles => (Strings.SharedFilesTab2, typeof(ProfileFilesTabPage)),
ProfileTabLinks => (Strings.SharedLinksTab2, typeof(ProfileLinksTabPage)),
ProfileTabMusic => (Strings.SharedMusicTab2, typeof(ProfileMusicTabPage)),
ProfileTabPolls => (Strings.SharedPollTab, typeof(ProfilePollsTabPage)),
ProfileTabVoice => (Strings.SharedVoiceTab2, typeof(ProfileVoiceTabPage)),
ProfileTabGifs => (Strings.SharedGIFsTab2, typeof(ProfileAnimationsTabPage)),
_ => (string.Empty, null)
};
}
public ProfileTab Type { get; set; }
public string Text { get; set; }
public Type PageType { get; set; }
public object Parameter { get; set; }
public string Subtitle => Locale.Declension(_locale, _items?.TotalCount ?? _totalCount);
private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(_items.TotalCount))
{
RaisePropertyChanged(nameof(Subtitle));
}
}
public bool CanSetAsMain => Type is ProfileTabPosts or ProfileTabGifts or ProfileTabMedia or ProfileTabFiles or ProfileTabLinks or ProfileTabMusic or ProfileTabGifs;
public override string ToString()
{
return Text;
}
}
public abstract partial class ProfileTabsViewModel : MediaTabsViewModelBase, IHandle
{
protected readonly ProfileSavedChatsTabViewModel _savedChatsTabViewModel;
protected readonly ProfileTopicsTabViewModel _topicsTabViewModel;
protected readonly ProfileStoriesTabViewModel _pinnedStoriesTabViewModel;
protected readonly ProfileStoriesTabViewModel _archivedStoriesTabViewModel;
protected readonly ProfileGroupsTabViewModel _groupsTabViewModel;
protected readonly ProfileChannelsTabViewModel _channelsTabViewModel;
protected readonly ProfileBotsTabViewModel _botsTabViewModel;
protected readonly ProfileGiftsTabViewModel _giftsTabViewModel;
protected readonly ProfileMembersTabViewModel _membersTabVieModel;
public ProfileTabsViewModel(IClientService clientService, ISettingsService settingsService, IStorageService storageService, IEventAggregator aggregator)
: base(clientService, settingsService, storageService, aggregator)
{
_savedChatsTabViewModel = Session.Resolve<ProfileSavedChatsTabViewModel>();
_topicsTabViewModel = Session.Resolve<ProfileTopicsTabViewModel>();
_pinnedStoriesTabViewModel = Session.Resolve<ProfileStoriesTabViewModel>();
_archivedStoriesTabViewModel = Session.Resolve<ProfileStoriesTabViewModel>();
_groupsTabViewModel = Session.Resolve<ProfileGroupsTabViewModel>();
_channelsTabViewModel = Session.Resolve<ProfileChannelsTabViewModel>();
_botsTabViewModel = Session.Resolve<ProfileBotsTabViewModel>();
_giftsTabViewModel = Session.Resolve<ProfileGiftsTabViewModel>();
_membersTabVieModel = Session.Resolve<ProfileMembersTabViewModel>();
_membersTabVieModel.IsEmbedded = true;
_pinnedStoriesTabViewModel.SetType(ChatStoriesType.Pinned);
_archivedStoriesTabViewModel.SetType(ChatStoriesType.Archive);
Children.Add(_savedChatsTabViewModel);
Children.Add(_topicsTabViewModel);
Children.Add(_pinnedStoriesTabViewModel);
Children.Add(_archivedStoriesTabViewModel);
Children.Add(_groupsTabViewModel);
Children.Add(_channelsTabViewModel);
Children.Add(_botsTabViewModel);
Children.Add(_giftsTabViewModel);
Children.Add(_membersTabVieModel);
Items = new RangeObservableCollection<ProfileTabItem>();
}
public RangeObservableCollection<ProfileTabItem> Items { get; }
protected ForumTopic _forumTopic;
public ForumTopic ForumTopic
{
get => _forumTopic;
set => Set(ref _forumTopic, value);
}
protected SavedMessagesTopic _savedMessagesTopic;
public SavedMessagesTopic SavedMessagesTopic
{
get => _savedMessagesTopic;
set => Set(ref _savedMessagesTopic, value);
}
public bool MyProfile { get; private set; }
public bool IsSavedMessages { get; private set; }
public override Task NavigatedToAsync(object parameter, NavigationMode mode, NavigationState state)
{
if (parameter is long chatId)
{
MyProfile = chatId == ClientService.Options.MyId;
}
else if (parameter is ChatMessageTopic chatMessageTopic)
{
IsSavedMessages = chatMessageTopic.ChatId == ClientService.Options.MyId;
}
return base.NavigatedToAsync(parameter, mode, state);
}
protected override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, NavigationState state)
{
if (parameter is ChatMessageTopic chatMessageTopic)
{
parameter = chatMessageTopic.ChatId;
}
var chatId = (long)parameter;
Chat = ClientService.GetChat(chatId);
Media.UpdateQuery(string.Empty);
Files.UpdateQuery(string.Empty);
Links.UpdateQuery(string.Empty);
Music.UpdateQuery(string.Empty);
Voice.UpdateQuery(string.Empty);
Animations.UpdateQuery(string.Empty);
if (Items.Empty())
{
await UpdateTabsAsync(Chat);
}
}
private ProfileTabItem _selectedItem;
public ProfileTabItem SelectedItem
{
get => _selectedItem;
set => Set(ref _selectedItem, value);
}
protected abstract Task UpdateTabsAsync(Chat chat);
protected async Task UpdateSharedCountAsync(Chat chat, IList<ProfileTabItem> tabs)
{
var filters = new SearchMessagesFilter[]
{
new SearchMessagesFilterPhotoAndVideo(),
new SearchMessagesFilterEmpty(),
new SearchMessagesFilterDocument(),
new SearchMessagesFilterUrl(),
new SearchMessagesFilterAudio(),
new SearchMessagesFilterPoll(),
new SearchMessagesFilterVoiceAndVideoNote(),
new SearchMessagesFilterAnimation(),
};
var sparseMessagesAvailable = Topic is MessageTopicSavedMessages or null;
var savedMessagesTopicId = 0L;
if (Topic is MessageTopicSavedMessages savedMessagesTopic)
{
savedMessagesTopicId = savedMessagesTopic.SavedMessagesTopicId;
}
async Task<Count> GetCountAsync(SearchMessagesFilter filter)
{
if (filter is SearchMessagesFilterEmpty)
{
if (IsSavedMessages || MyProfile)
{
return new Count(0);
}
var response = await ClientService.SendAsync(new GetSavedMessagesTopicHistory(chat.Id, 0, 0, 1));
if (response is Messages messages)
{
return new Count(messages.TotalCount);
}
return new Count(0);
}
if (sparseMessagesAvailable && filter is SearchMessagesFilterPhotoAndVideo or SearchMessagesFilterDocument or SearchMessagesFilterAudio or SearchMessagesFilterPoll or SearchMessagesFilterVoiceAndVideoNote or SearchMessagesFilterAnimation)
{
var source = await ClientService.SendAsync(new GetChatMessageCount(chat.Id, Topic, filter, false)) as Count;
if (source?.CountValue > 50)
{
switch (filter)
{
case SearchMessagesFilterPhotoAndVideo:
case SearchMessagesFilterPhoto:
case SearchMessagesFilterVideo:
Media.DataSource = new MediaDataSource(ClientService, chat.Id, savedMessagesTopicId, filter);
break;
case SearchMessagesFilterDocument:
Files.DataSource = new MediaDataSource(ClientService, chat.Id, savedMessagesTopicId, filter);
break;
case SearchMessagesFilterAudio:
Music.DataSource = new MediaDataSource(ClientService, chat.Id, savedMessagesTopicId, filter);
break;
case SearchMessagesFilterVoiceAndVideoNote:
Voice.DataSource = new MediaDataSource(ClientService, chat.Id, savedMessagesTopicId, filter);
break;
case SearchMessagesFilterAnimation:
Animations.DataSource = new MediaDataSource(ClientService, chat.Id, savedMessagesTopicId, filter);
break;
}
}
return source;
}
return await ClientService.SendAsync(new GetChatMessageCount(chat.Id, Topic, filter, false)) as Count;
}
for (int i = 0; i < filters.Length; i++)
{
var response = await GetCountAsync(filters[i]);
if (response is Count count)
{
if (count.CountValue > 0)
{
var item = filters[i] switch
{
SearchMessagesFilterPhotoAndVideo => new ProfileTabItem(new ProfileTabMedia(), null, count.CountValue, Strings.R.Media),
SearchMessagesFilterEmpty => new ProfileTabItem(new ProfileTabSavedMessages(), new ChatMessageTopic(ClientService.Options.MyId, new MessageTopicSavedMessages(chat.Id)), count.CountValue, Strings.R.SavedMessagesCount),
SearchMessagesFilterDocument => new ProfileTabItem(new ProfileTabFiles(), null, count.CountValue, Strings.R.Files),
SearchMessagesFilterUrl => new ProfileTabItem(new ProfileTabLinks(), null, count.CountValue, Strings.R.Links),
SearchMessagesFilterAudio => new ProfileTabItem(new ProfileTabMusic(), null, count.CountValue, Strings.R.MusicFiles),
SearchMessagesFilterPoll => new ProfileTabItem(new ProfileTabPolls(), new ChatMessageTopic(chat.Id, Topic), count.CountValue, Strings.R.ProfilePollsCount),
SearchMessagesFilterVoiceAndVideoNote => new ProfileTabItem(new ProfileTabVoice(), null, count.CountValue, Strings.R.Voice),
SearchMessagesFilterAnimation => new ProfileTabItem(new ProfileTabGifs(), null, count.CountValue, Strings.R.GIFs),
_ => null
};
tabs.Add(item);
}
}
}
}
protected override bool ShouldHandleDeleteMessages(UpdateDeleteMessages update)
{
return update.ChatId == _chat?.Id;
}
protected Chat _chat;
public Chat Chat
{
get => _chat;
set => Set(ref _chat, value);
}
public long ChatId => Chat?.Id ?? 0;
public override MediaCollection SetSearch(object sender, string query)
{
var target = sender switch
{
SearchMessagesFilterPhotoAndVideo => Media,
SearchMessagesFilterPhoto => Media,
SearchMessagesFilterVideo => Media,
SearchMessagesFilterDocument => Files,
SearchMessagesFilterAudio => Music,
SearchMessagesFilterVoiceAndVideoNote => Voice,
SearchMessagesFilterAnimation => Animations,
_ => null
};
if (sender is SearchMessagesFilter filter && (target?.DataSource == null || query.Length > 0))
{
target?.UseDataSource = false;
return new MediaCollection(ClientService, Chat.Id, Topic, filter, query);
}
target?.UseDataSource = true;
return null;
}
}
}