/
PlaylistViewModel.cs
309 lines (249 loc) · 11.3 KB
/
PlaylistViewModel.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
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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MpcNET;
using MpcNET.Commands.Playback;
using MpcNET.Commands.Playlist;
using MpcNET.Commands.Queue;
using MpcNET.Commands.Reflection;
using SkiaSharp;
using Stylophone.Common.Helpers;
using Stylophone.Common.Interfaces;
using Stylophone.Common.Services;
using Stylophone.Localization.Strings;
namespace Stylophone.Common.ViewModels
{
public partial class PlaylistViewModel : ViewModelBase, IDisposable
{
private INotificationService _notificationService;
private INavigationService _navigationService;
private IDialogService _dialogService;
private IInteropService _interop;
private MPDConnectionService _mpdService;
private AlbumArtService _albumArtService;
private TrackViewModelFactory _trackVmFactory;
public PlaylistViewModel(INotificationService notificationService, INavigationService navigationService, IDispatcherService dispatcherService, IDialogService dialogService, IInteropService interop, MPDConnectionService mpdService, AlbumArtService albumArtService, TrackViewModelFactory trackVmFactory):
base(dispatcherService)
{
_notificationService = notificationService;
_navigationService = navigationService;
_dispatcherService = dispatcherService;
_dialogService = dialogService;
_interop = interop;
_mpdService = mpdService;
_albumArtService = albumArtService;
_trackVmFactory = trackVmFactory;
Source.CollectionChanged += (s, e) => OnPropertyChanged(nameof(IsSourceEmpty));
DominantColor = _interop.GetAccentColor();
}
private NotifyCollectionChangedAction _previousAction;
private int _oldId;
private CancellationTokenSource _albumArtCts;
public ObservableCollection<TrackViewModel> Source { get; private set; } = new ObservableCollection<TrackViewModel>();
public bool IsSourceEmpty => Source.Count == 0;
[ObservableProperty]
private string _name;
[ObservableProperty]
private string _artists;
[ObservableProperty]
private string _playlistInfo;
[ObservableProperty]
private bool _artLoaded;
[ObservableProperty]
private SKImage _playlistArt;
[ObservableProperty]
private SKImage _playlistArt2;
[ObservableProperty]
private SKImage _playlistArt3;
[ObservableProperty]
private SKColor _dominantColor;
/// <summary>
/// If the dominant color of the album is too light to show white text on top of, this boolean will be true.
/// </summary>
[ObservableProperty]
private bool _isLight;
#region Commands
private bool IsSingleTrackSelected(object list)
{
// Cast the received __ComObject
var selectedTracks = (IList<object>)list;
return (selectedTracks?.Count == 1);
}
[RelayCommand]
private async void RemovePlaylist()
{
var result = await _dialogService.ShowConfirmDialogAsync(Resources.DeletePlaylistContentDialog, "", Resources.OKButtonText, Resources.CancelButtonText);
if (result)
{
var res = await _mpdService.SafelySendCommandAsync(new RmCommand(Name));
if (res != null)
{
_notificationService.ShowInAppNotification(Resources.NotificationPlaylistRemoved);
_navigationService.GoBack();
}
}
}
[RelayCommand]
private async void LoadPlaylist()
{
var res = await _mpdService.SafelySendCommandAsync(new LoadCommand(Name));
if (res != null)
_notificationService.ShowInAppNotification(Resources.NotificationAddedToQueue);
}
[RelayCommand]
private async void PlayPlaylist()
{
// Clear queue, add playlist and play
var commandList = new CommandList(new IMpcCommand<object>[] { new ClearCommand() , new LoadCommand(Name), new PlayCommand(0) });
if (await _mpdService.SafelySendCommandAsync(commandList) != null)
{
// Auto-navigate to the queue
_navigationService.Navigate<QueueViewModel>();
}
}
[RelayCommand]
private async void AddToQueue(object list)
{
// Cast the received __ComObject
var selectedTracks = (IList<object>)list;
if (selectedTracks?.Count > 0)
{
var commandList = new CommandList();
foreach (var f in selectedTracks)
{
var trackVM = f as TrackViewModel;
commandList.Add(new AddIdCommand(trackVM.File.Path));
}
var r = await _mpdService.SafelySendCommandAsync(commandList);
if (r != null)
_notificationService.ShowInAppNotification(Resources.NotificationAddedToQueue);
}
}
[RelayCommand(CanExecute = nameof(IsSingleTrackSelected))]
private void ViewAlbum(object list)
{
// Cast the received __ComObject
var selectedTracks = (IList<object>)list;
if (selectedTracks?.Count > 0)
{
var trackVM = selectedTracks.First() as TrackViewModel;
trackVM.ViewAlbumCommand.Execute(trackVM.File);
}
}
[RelayCommand]
private async void RemoveTrackFromPlaylist(object list)
{
// Cast the received __ComObject
var selectedTracks = (IList<object>)list;
if (selectedTracks?.Count > 0)
{
var commandList = new CommandList();
// We can't batch PlaylistDeleteCommands cleanly, since they're index-based and logically, said indexes will shift as we remove stuff from the playlist.
// To simulate this behavior, we copy our Source list and incrementally remove the affected tracks from it to get the valid indexes as we move down the commandList.
IList<TrackViewModel> copy = Source.ToList();
foreach (var f in selectedTracks)
{
var trackVM = f as TrackViewModel;
commandList.Add(new PlaylistDeleteCommand(Name, copy.IndexOf(trackVM)));
copy.Remove(trackVM);
}
var r = await _mpdService.SafelySendCommandAsync(commandList);
if (r != null) // Reload playlist
await LoadDataAsync(Name);
}
}
#endregion
public async Task LoadDataAsync(string playlistName)
{
var placeholder = await _interop.GetPlaceholderImageAsync();
_albumArtCts = new CancellationTokenSource();
ArtLoaded = false;
PlaylistArt = placeholder;
PlaylistArt2 = placeholder;
PlaylistArt3 = placeholder;
Name = playlistName;
Source.CollectionChanged -= Source_CollectionChanged;
Source.Clear();
var findReq = await _mpdService.SafelySendCommandAsync(new ListPlaylistInfoCommand(playlistName));
if (findReq == null) return;
foreach (var item in findReq)
{
Source.Add(_trackVmFactory.GetTrackViewModel(item));
}
Source.CollectionChanged += Source_CollectionChanged;
var artistList = findReq.Select(f => f.Artist).Distinct().Where(f => f != null && f != "").ToList();
Artists = artistList.Count > 0 ? artistList.Aggregate((f1, f2) => $"{f1}, {f2}") : "";
var totalTime = Source.Count > 0 ? Source.Select(tr => tr.File.Time).Aggregate((t1, t2) => t1 + t2) : 0;
TimeSpan t = TimeSpan.FromSeconds(totalTime);
PlaylistInfo = $"{Source.Count} Tracks, Total Time: {Miscellaneous.ToReadableString(t)}";
if (Source.Count > 0)
{
await Task.Run(async () =>
{
// Get album art for three albums to display in the playlist view
Random r = new Random();
var distinctAlbums = Source.GroupBy(tr => tr.File.Album).Select(tr => tr.First()).OrderBy((item) => r.Next()).ToList();
if (distinctAlbums.Count > 1)
{
var art = await _albumArtService.GetAlbumArtAsync(distinctAlbums[0].File, true, _albumArtCts.Token);
PlaylistArt = art != null ? art.ArtBitmap : PlaylistArt;
DominantColor = (art?.DominantColor?.Color).GetValueOrDefault();
if (DominantColor == default(SKColor))
{
await _dispatcherService.ExecuteOnUIThreadAsync(() =>
{
DominantColor = _interop.GetAccentColor();
});
}
IsLight = (!art?.DominantColor?.IsDark).GetValueOrDefault();
}
if (distinctAlbums.Count > 2)
{
var art = await _albumArtService.GetAlbumArtAsync(distinctAlbums[1].File, false, _albumArtCts.Token);
PlaylistArt2 = art != null ? art.ArtBitmap : PlaylistArt2;
}
else PlaylistArt2 = PlaylistArt;
if (distinctAlbums.Count > 3)
{
var art = await _albumArtService.GetAlbumArtAsync(distinctAlbums[2].File, false, _albumArtCts.Token);
PlaylistArt3 = art != null ? art.ArtBitmap : PlaylistArt3;
}
else PlaylistArt3 = PlaylistArt2;
ArtLoaded = true;
});
}
}
private async void Source_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add && _previousAction == NotifyCollectionChangedAction.Remove)
{
// User reordered tracks, send matching command
await _mpdService.SafelySendCommandAsync(new PlaylistMoveCommand(Name, _oldId, e.NewStartingIndex));
_previousAction = NotifyCollectionChangedAction.Move;
}
if (e.Action == NotifyCollectionChangedAction.Remove)
{
_previousAction = e.Action;
_oldId = e.OldStartingIndex;
}
}
public void Dispose()
{
_albumArtCts?.Cancel();
PlaylistArt?.Dispose();
PlaylistArt = null;
PlaylistArt2?.Dispose();
PlaylistArt2 = null;
PlaylistArt3?.Dispose();
PlaylistArt3 = null;
ArtLoaded = false;
}
}
}