-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathSettingsViewModel.cs
192 lines (166 loc) · 6.67 KB
/
SettingsViewModel.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
//
// Copyright Fela Ameghino & Contributors 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.Text.RegularExpressions;
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.ViewModels.Delegates;
using Telegram.Views;
using Telegram.Views.Popups;
using Telegram.Views.Premium.Popups;
using Telegram.Views.Settings;
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 MvxObservableCollection<SettingsSearchEntry>();
}
public IStorageService StorageService => _storageService;
public string OwnedStarCount => ClientService.OwnedStarCount.IsPositive()
? ClientService.OwnedStarCount.ToValue()
: string.Empty;
public MvxObservableCollection<SettingsSearchEntry> Results { get; private set; }
protected override Task OnNavigatedToAsync(object parameter, NavigationMode mode, NavigationState state)
{
if (ClientService.TryGetUser(ClientService.Options.MyId, out User item))
{
Delegate?.UpdateUser(null, item, false);
var cache = ClientService.GetUserFull(item.Id);
if (cache == null)
{
ClientService.Send(new GetUserFullInfo(item.Id));
}
else
{
Delegate?.UpdateUserFullInfo(null, item, cache, false, false);
}
}
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)
{
BeginOnUIThread(() => Delegate?.UpdateUser(null, update.User, false));
}
}
public void Handle(UpdateUserFullInfo update)
{
if (update.UserId == ClientService.Options.MyId)
{
BeginOnUIThread(() => Delegate?.UpdateUserFullInfo(null, ClientService.GetUser(update.UserId), 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 user = await ChooseChatsPopup.PickUserAsync(ClientService, NavigationService, Strings.SelectContact, false);
if (user != null)
{
ClientService.TryGetUserFull(user.Id, out UserFullInfo fullInfo);
fullInfo ??= await ClientService.SendAsync(new GetUserFullInfo(user.Id)) as UserFullInfo;
if (fullInfo != null)
{
await ShowPopupAsync(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();
}
}
}
}