-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathContactsViewModel.cs
244 lines (201 loc) · 6.78 KB
/
ContactsViewModel.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
//
// 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 System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Telegram.Collections;
using Telegram.Common;
using Telegram.Converters;
using Telegram.Navigation;
using Telegram.Navigation.Services;
using Telegram.Services;
using Telegram.Td.Api;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace Telegram.ViewModels
{
public partial class ContactsViewModel : MultiViewModelBase, IHandle
{
private readonly IVoipService _voipService;
private readonly DisposableMutex _loadMoreLock;
private readonly UserComparer _comparer;
public ContactsViewModel(IClientService clientService, ISettingsService settingsService, IVoipService voipService, IEventAggregator aggregator)
: base(clientService, settingsService, aggregator)
{
_voipService = voipService;
_loadMoreLock = new DisposableMutex();
_comparer = new UserComparer(Settings.IsContactsSortedByEpoch);
Items = new SortedObservableCollection<User>(_comparer);
}
protected override Task OnNavigatedToAsync(object parameter, NavigationMode mode, NavigationState state)
{
Load(Settings.IsContactsSortedByEpoch);
return Task.CompletedTask;
}
public override void Subscribe()
{
Aggregator.Subscribe<UpdateUserStatus>(this, Handle)
.Subscribe<UpdateUserIsContact>(Handle);
}
public bool IsSortedByEpoch
{
get => Settings.IsContactsSortedByEpoch;
set
{
if (Settings.IsContactsSortedByEpoch != value)
{
Settings.IsContactsSortedByEpoch = value;
Load(value);
RaisePropertyChanged(nameof(IsSortedByEpoch));
}
}
}
private async void Load(bool epoch)
{
using (await _loadMoreLock.WaitAsync())
{
_comparer.ByEpoch = epoch;
var response = await ClientService.SendAsync(new GetContacts());
if (response is Telegram.Td.Api.Users users)
{
var items = new List<User>();
foreach (var user in ClientService.GetUsers(users.UserIds))
{
items.Add(user);
}
Items.ReplaceWith(items);
IsEmpty = items.Empty();
}
}
}
private bool _isEmpty;
public bool IsEmpty
{
get => _isEmpty;
set => Set(ref _isEmpty, value);
}
private SearchUsersCollection _search;
public SearchUsersCollection Search
{
get => _search;
set => Set(ref _search, value);
}
#region Handle
public void Handle(UpdateUserStatus update)
{
var user = ClientService.GetUser(update.UserId);
if (user == null || user.Id == ClientService.Options.MyId || !user.IsContact)
{
return;
}
BeginOnUIThread(() =>
{
var first = Items.FirstOrDefault(x => x != null && x.Id == update.UserId);
if (first != null)
{
Items.Remove(first);
}
Items.Add(user);
});
}
public void Handle(UpdateUserIsContact update)
{
var user = ClientService.GetUser(update.UserId);
if (user == null || user.Id == ClientService.Options.MyId)
{
return;
}
BeginOnUIThread(() =>
{
var first = Items.FirstOrDefault(x => x != null && x.Id == update.UserId);
if (first != null)
{
Items.Remove(first);
}
if (user.IsContact)
{
Items.Add(user);
}
});
}
#endregion
public SortedObservableCollection<User> Items { get; }
#region Context menu
public void SendMessage(User user)
{
NavigationService.NavigateToUser(user.Id, true);
}
public void VoiceCall(User user)
{
Call(user, false);
}
public void VideoCall(User user)
{
Call(user, true);
}
private void Call(User user, bool video)
{
_voipService.StartPrivateCall(NavigationService, user, video);
}
public async void CreateSecretChat(User user)
{
var confirm = await ShowPopupAsync(Strings.AreYouSureSecretChat, Strings.AreYouSureSecretChatTitle, Strings.Start, Strings.Cancel);
if (confirm != ContentDialogResult.Primary)
{
return;
}
var response = await ClientService.SendAsync(new CreateNewSecretChat(user.Id));
if (response is Chat result)
{
NavigationService.NavigateToChat(result);
}
}
#endregion
}
public partial class UserComparer : IComparer<User>
{
public UserComparer(bool epoch)
{
ByEpoch = epoch;
}
public bool ByEpoch { get; set; }
public int Compare(User x, User y)
{
if (ByEpoch)
{
var epoch = LastSeenConverter.GetIndex(y).CompareTo(LastSeenConverter.GetIndex(x));
if (epoch == 0)
{
var nameX = x.FirstName.Length > 0 ? x.FirstName : x.LastName;
var nameY = y.FirstName.Length > 0 ? y.FirstName : y.LastName;
var fullName = nameX.CompareTo(nameY);
if (fullName == 0)
{
return y.Id.CompareTo(x.Id);
}
return fullName;
}
return epoch;
}
else
{
if (x.Type is UserTypeDeleted)
{
return 1;
}
var nameX = x.FirstName.Length > 0 ? x.FirstName : x.LastName;
var nameY = y.FirstName.Length > 0 ? y.FirstName : y.LastName;
var fullName = nameX.CompareTo(nameY);
if (fullName == 0)
{
return y.Id.CompareTo(x.Id);
}
return fullName;
}
}
}
}