-
-
Notifications
You must be signed in to change notification settings - Fork 679
Expand file tree
/
Copy pathSettingsViewModel.cs
More file actions
249 lines (212 loc) · 8.74 KB
/
Copy pathSettingsViewModel.cs
File metadata and controls
249 lines (212 loc) · 8.74 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
//
// 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.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Telegram.Collections;
using Telegram.Common;
using Telegram.Controls.Cells;
using Telegram.Navigation;
using Telegram.Navigation.Services;
using Telegram.Services;
using Telegram.Td.Api;
using Telegram.ViewModels.Delegates;
using Telegram.Views;
using Telegram.Views.Popups;
using Telegram.Views.Premium.Popups;
using Telegram.Views.Settings;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace Telegram.ViewModels
{
public partial class SettingsViewModel : ViewModelBase, IChildViewModel, IDelegable<ISettingsDelegate>, IHandle
{
private readonly ISettingsSearchService _searchService;
private readonly IStorageService _storageService;
public ISettingsDelegate Delegate { get; set; }
public SettingsViewModel(IClientService clientService, ISettingsService settingsService, IStorageService storageService, IEventAggregator aggregator, ISettingsSearchService searchService)
: base(clientService, settingsService, aggregator)
{
_searchService = searchService;
_storageService = storageService;
NavigateCommand = new RelayCommand<SettingsSearchEntry>(Navigate);
Results = new RangeObservableCollection<SettingsSearchEntry>();
}
public IStorageService StorageService => _storageService;
public string OwnedStarCount => ClientService.OwnedStarCount.IsPositive()
? ClientService.OwnedStarCount.ToValue()
: string.Empty;
public RangeObservableCollection<SettingsSearchEntry> Results { get; private set; }
protected override Task OnNavigatedToAsync(object parameter, NavigationMode mode, NavigationState state)
{
if (ClientService.TryGetUser(ClientService.Options.MyId, out User item))
{
var cache = ClientService.GetUserFull(item.Id);
Delegate?.UpdateUser(null, item, cache, false, false);
ClientService.Send(new GetUserFullInfo(item.Id));
}
return Task.CompletedTask;
}
public override void Subscribe()
{
Aggregator.Subscribe<UpdateUser>(this, Handle)
.Subscribe<UpdateUserFullInfo>(Handle)
.Subscribe<UpdateOwnedStarCount>(Handle)
.Subscribe<UpdateOption>(Handle);
}
public async void Activate()
{
await OnNavigatedToAsync(null, NavigationMode.Refresh, null);
}
public void Deactivate() { }
public void Handle(UpdateUser update)
{
if (update.User.Id == ClientService.Options.MyId)
{
ClientService.TryGetUserFull(update.User.Id, out UserFullInfo fullInfo);
BeginOnUIThread(() => Delegate?.UpdateUser(null, update.User, fullInfo, false, false));
}
}
public void Handle(UpdateUserFullInfo update)
{
if (update.UserId == ClientService.Options.MyId && ClientService.TryGetUser(update.UserId, out User user))
{
BeginOnUIThread(() => Delegate?.UpdateUser(null, user, update.UserFullInfo, false, false));
}
}
private void Handle(UpdateOwnedStarCount update)
{
BeginOnUIThread(() => RaisePropertyChanged(nameof(OwnedStarCount)));
}
public void Handle(UpdateOption update)
{
if (update.Name == OptionsService.R.IsPremium || update.Name == OptionsService.R.IsPremiumAvailable)
{
BeginOnUIThread(() => RaisePropertyChanged(nameof(IsPremiumAvailable)));
}
}
public async void Ask()
{
var text = Regex.Replace(Strings.AskAQuestionInfo, "<!\\[CDATA\\[(.*?)\\]\\]>", "$1");
var confirm = await ShowPopupAsync(text, Strings.AskAQuestion, Strings.AskButton, tertiary: Strings.Cancel);
if (confirm == ContentDialogResult.Primary)
{
var response = await ClientService.SendAsync(new GetSupportUser());
if (response is User user)
{
response = await ClientService.SendAsync(new CreatePrivateChat(user.Id, false));
if (response is Chat chat)
{
NavigationService.NavigateToChat(chat);
}
}
}
}
public async void PremiumGifting()
{
var popup = ChooseChatsPopup.Create(NavigationService, Strings.GiftTelegramPremiumOrStarsTitle);
if (ClientService.TryGetUser(ClientService.Options.MyId, out User self))
{
var selfChat = await ClientService.SendAsync(new CreatePrivateChat(self.Id, false)) as Chat;
var profile = new ProfileCell();
profile.UpdateUser(ClientService, self, 36, true);
profile.Subtitle = Strings.Gift2Myself;
var button = new Button
{
Content = profile,
Style = BootStrapper.Current.Resources["ListEmptyButtonStyle"] as Style,
Margin = new Thickness(12, 0, 12, 0),
CornerRadius = new CornerRadius(4)
};
button.Click += (s, args) =>
{
popup.ViewModel.SelectedItems = new RangeObservableCollection<Chat> { selfChat };
popup.Hide(ContentDialogResult.Primary);
};
popup.Header = button;
}
var options = new ChooseChatsOptions()
{
AllowChannelChats = false,
AllowGroupChats = false,
AllowBotChats = false,
AllowUserChats = true,
AllowSecretChats = false,
AllowSelf = false,
CanPostMessages = false,
CanInviteUsers = false,
CanShareContact = false,
Mode = ChooseChatsMode.Chats,
ShowMessages = false
};
var confirm = await popup.PickAsync(XamlRoot, Array.Empty<long>(), options, ListViewSelectionMode.None);
if (confirm != ContentDialogResult.Primary)
{
return;
}
var chat = popup.ViewModel.SelectedItems.FirstOrDefault();
var user = ClientService.GetUser(chat);
if (user != null)
{
ClientService.TryGetUserFull(user.Id, out UserFullInfo fullInfo);
fullInfo ??= await ClientService.SendAsync(new GetUserFullInfo(user.Id)) as UserFullInfo;
if (fullInfo != null)
{
ShowPopup(new GiftPopup(ClientService, NavigationService, user, fullInfo));
}
}
}
public void Search(string query)
{
Results.ReplaceWith(_searchService.Search(query));
}
public RelayCommand<SettingsSearchEntry> NavigateCommand { get; }
public void Navigate(SettingsSearchEntry entry)
{
if (entry is SettingsSearchPage page && page.Page != null)
{
if (page.Page == typeof(SettingsPasswordPage))
{
NavigationService.NavigateToPassword();
}
else if (page.Page == typeof(SettingsPasscodePage))
{
NavigationService.NavigateToPasscode();
}
else if (page.Page == typeof(InstantPage))
{
NavigationService.NavigateToInstant(Strings.TelegramFaqUrl);
}
//else if (page.Page == typeof(WalletPage))
//{
// NavigationService.NavigateToWallet();
//}
else
{
NavigationService.Navigate(page.Page, page.Parameter);
}
}
else if (entry is SettingsSearchFaq faq)
{
NavigationService.NavigateToInstant(faq.Url);
}
else if (entry is SettingsSearchAction action)
{
action.Action();
}
}
public void ShareUsername()
{
if (ClientService.TryGetUser(ClientService.Options.MyId, out User user))
{
ShowPopup(new QrCodePopup(ClientService, NavigationService, Settings, user));
}
}
}
}