-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathChooseSoundViewModel.cs
215 lines (184 loc) · 6.97 KB
/
ChooseSoundViewModel.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
//
// 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.Linq;
using System.Threading.Tasks;
using Telegram.Common;
using Telegram.Converters;
using Telegram.Navigation;
using Telegram.Navigation.Services;
using Telegram.Services;
using Telegram.Td.Api;
using Windows.Storage.Pickers;
using Windows.UI.Xaml.Navigation;
namespace Telegram.ViewModels
{
public partial class ChooseSoundViewModel : ViewModelBase, IHandle
{
public ChooseSoundViewModel(IClientService clientService, ISettingsService settingsService, IEventAggregator aggregator)
: base(clientService, settingsService, aggregator)
{
Items = new DiffObservableCollection<NotificationSoundViewModel>(new NotificationSoundDiffHandler());
}
public DiffObservableCollection<NotificationSoundViewModel> Items { get; }
public bool CanUploadMore => Items.Count < ClientService.Options.NotificationSoundCountMax;
public async void Handle(UpdateSavedNotificationSounds update)
{
var response = await ClientService.SendAsync(new GetSavedNotificationSounds());
if (response is NotificationSounds sounds)
{
BeginOnUIThread(() =>
{
Items.ReplaceDiff(sounds.NotificationSoundsValue.Select(x => new NotificationSoundViewModel(this, x, false)));
RaisePropertyChanged(nameof(CanUploadMore));
});
}
}
protected override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, NavigationState state)
{
var response = await ClientService.SendAsync(new GetSavedNotificationSounds());
if (response is NotificationSounds sounds && parameter is long selected)
{
Items.ReplaceDiff(sounds.NotificationSoundsValue.Select(x => new NotificationSoundViewModel(this, x, x.Id == selected)));
RaisePropertyChanged(nameof(CanUploadMore));
}
}
public override void Subscribe()
{
Aggregator.Subscribe<UpdateSavedNotificationSounds>(this, Handle);
}
public async void Upload()
{
try
{
var picker = new FileOpenPicker();
picker.ViewMode = PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = PickerLocationId.MusicLibrary;
picker.FileTypeFilter.Add(".mp3");
var file = await picker.PickSingleFileAsync();
if (file != null)
{
var properties = await file.GetBasicPropertiesAsync();
if ((long)properties.Size > ClientService.Options.NotificationSoundSizeMax)
{
// TODO: ...
return;
}
var music = await file.Properties.GetMusicPropertiesAsync();
if (music.Duration.TotalSeconds > ClientService.Options.NotificationSoundDurationMax)
{
// TODO: ...
return;
}
ClientService.Send(new AddSavedNotificationSound(await file.ToGeneratedAsync()));
}
}
catch { }
}
}
public partial class NotificationSoundViewModel : BindableBase
{
private readonly ChooseSoundViewModel _parent;
private readonly NotificationSound _notificationSound;
private long _soundToken;
public NotificationSoundViewModel(ChooseSoundViewModel parent, NotificationSound notificationSound, bool selected)
{
_parent = parent;
_notificationSound = notificationSound;
_isSelected = selected;
Sound = notificationSound.Sound;
Data = notificationSound.Data;
if (notificationSound.Title.Length > 0)
{
Title = notificationSound.Title;
}
else
{
Title = string.Format(Strings.SoundNameEmpty, Formatter.Date(notificationSound.Date));
}
Date = notificationSound.Date;
Duration = notificationSound.Duration;
Id = notificationSound.Id;
}
public NotificationSound Get()
{
return _notificationSound;
}
private bool _isSelected;
public bool IsSelected
{
get => _isSelected;
set => SetSelected(value);
}
private void SetSelected(bool value)
{
if (Set(ref _isSelected, value, nameof(IsSelected)) && _isSelected)
{
var file = _notificationSound.Sound;
if (file.Local.IsDownloadingCompleted)
{
SoundEffects.Play(file);
}
else
{
UpdateManager.Subscribe(this, _parent.ClientService, file, ref _soundToken, UpdateFile, true);
if (file.Local.CanBeDownloaded && !file.Local.IsDownloadingCompleted)
{
_parent.ClientService.DownloadFile(file.Id, 16);
}
}
}
else
{
UpdateManager.Unsubscribe(this, ref _soundToken, true);
}
}
private void UpdateFile(object sender, File file)
{
if (sender is NotificationSoundViewModel notificationSound && notificationSound.IsSelected)
{
SoundEffects.Play(file);
}
}
/// <summary>
/// File containing the sound.
/// </summary>
public File Sound { get; }
/// <summary>
/// Arbitrary data, defined while the sound was uploaded.
/// </summary>
public string Data { get; }
/// <summary>
/// Title of the notification sound.
/// </summary>
public string Title { get; }
/// <summary>
/// Point in time (Unix timestamp) when the sound was created.
/// </summary>
public int Date { get; }
/// <summary>
/// Duration of the sound, in seconds.
/// </summary>
public int Duration { get; }
/// <summary>
/// Unique identifier of the notification sound.
/// </summary>
public long Id { get; }
}
public partial class NotificationSoundDiffHandler : IDiffHandler<NotificationSoundViewModel>
{
public bool CompareItems(NotificationSoundViewModel oldItem, NotificationSoundViewModel newItem)
{
return oldItem?.Id == newItem?.Id;
}
public void UpdateItem(NotificationSoundViewModel oldItem, NotificationSoundViewModel newItem)
{
// ...
}
}
}