-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathVideoPlayerPageViewModel.Operations.cs
385 lines (343 loc) · 11.4 KB
/
VideoPlayerPageViewModel.Operations.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// Copyright (c) Bili Copilot. All rights reserved.
using BiliCopilot.UI.Forms;
using BiliCopilot.UI.Models;
using BiliCopilot.UI.Models.Constants;
using BiliCopilot.UI.Pages.Overlay;
using BiliCopilot.UI.Toolkits;
using BiliCopilot.UI.ViewModels.Components;
using BiliCopilot.UI.ViewModels.Core;
using BiliCopilot.UI.ViewModels.Items;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.Logging;
using Richasy.BiliKernel.Bili.User;
using Richasy.BiliKernel.Models.Media;
using Richasy.WinUI.Share.Base;
using Windows.ApplicationModel.DataTransfer;
using Windows.Storage.Streams;
using Windows.System;
using WinUIEx;
namespace BiliCopilot.UI.ViewModels.View;
/// <summary>
/// 视频播放器页面视图模型.
/// </summary>
public sealed partial class VideoPlayerPageViewModel
{
[RelayCommand]
private async Task ToggleFollowAsync()
{
try
{
if (IsFollow)
{
await _relationshipService.UnfollowUserAsync(_view.Information.Publisher.User.Id);
IsFollow = false;
}
else
{
await _relationshipService.FollowUserAsync(_view.Information.Publisher.User.Id);
IsFollow = true;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "尝试关注/取消关注 UP 主时失败.");
}
}
[RelayCommand]
private async Task ToggleLikeAsync()
{
try
{
var state = !IsLiked;
await _service.ToggleVideoLikeAsync(_view.Information.Identifier.Id, state);
IsLiked = state;
}
catch (Exception ex)
{
_logger.LogError(ex, "尝试点赞/取消点赞视频时失败.");
}
}
[RelayCommand]
private async Task CoinAsync(int count = 1)
{
try
{
await _service.CoinVideoAsync(_view.Information.Identifier.Id, count, IsCoinAlsoLike);
IsCoined = true;
if (IsCoinAlsoLike && !IsLiked)
{
IsLiked = true;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "尝试投币/取消投币视频时失败.");
}
}
[RelayCommand]
private async Task TripleAsync()
{
try
{
await _service.TripleVideoAsync(_view.Information.Identifier.Id);
IsLiked = true;
IsCoined = true;
IsFavorited = true;
}
catch (Exception ex)
{
_logger.LogError(ex, "尝试一键三连视频时失败.");
}
}
[RelayCommand]
private async Task InitializeFavoritesAsync()
{
if (FavoriteFolders is not null)
{
return;
}
try
{
var (folders, ids) = await _favoriteService.GetPlayingVideoFavoriteFoldersAsync(_view.Information.Identifier.Id);
FavoriteFolders = folders.Select(p => new PlayerFavoriteFolderViewModel(p, ids.Contains(p.Id))).ToList();
}
catch (Exception ex)
{
_logger.LogError(ex, "尝试获取收藏夹列表时失败.");
}
}
[RelayCommand]
private async Task FavoriteAsync()
{
if (FavoriteFolders is null)
{
return;
}
var selectedFolders = FavoriteFolders?.Where(p => p.IsSelected).Select(p => p.Data.Id).ToList();
var unselectedFolders = FavoriteFolders?.Where(p => !p.IsSelected).Select(p => p.Data.Id).ToList();
try
{
await _service.FavoriteVideoAsync(_view.Information.Identifier.Id, selectedFolders, unselectedFolders);
IsFavorited = selectedFolders.Any();
FavoriteFolders = default;
}
catch (Exception ex)
{
_logger.LogError(ex, "尝试收藏视频时失败.");
}
}
[RelayCommand]
private void CopyVideoUrl()
{
var url = GetWebLink();
var dp = new DataPackage();
dp.SetText(url);
dp.SetWebLink(new Uri(url));
Clipboard.SetContent(dp);
this.Get<AppViewModel>().ShowTipCommand.Execute((ResourceToolkit.GetLocalizedString(StringNames.Copied), InfoType.Success));
}
[RelayCommand]
private async Task AddToViewLaterAsync()
{
try
{
await this.Get<IViewLaterService>().AddAsync(_view.Information.Identifier.Id);
this.Get<AppViewModel>().ShowTipCommand.Execute((ResourceToolkit.GetLocalizedString(StringNames.AddViewLaterSucceed), InfoType.Success));
}
catch (Exception ex)
{
this.Get<ILogger<VideoItemViewModel>>().LogError(ex, "添加稍后再看失败");
this.Get<AppViewModel>().ShowTipCommand.Execute((ResourceToolkit.GetLocalizedString(StringNames.AddViewLaterFailed), InfoType.Error));
}
}
[RelayCommand]
private void ShareVideoUrl()
{
var handle = this.Get<AppViewModel>().ActivatedWindow.GetWindowHandle();
var transferManager = DataTransferManagerInterop.GetForWindow(handle);
transferManager.DataRequested += OnTransferDataRequested;
DataTransferManagerInterop.ShowShareUIForWindow(handle);
void OnTransferDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
var url = GetWebLink();
var dp = new DataPackage();
dp.SetText(url);
dp.SetWebLink(new Uri(url));
dp.Properties.Title = Title;
dp.Properties.Description = Description;
dp.Properties.Thumbnail = RandomAccessStreamReference.CreateFromUri(_view.Information.Identifier.Cover.Uri);
args.Request.Data = dp;
sender.DataRequested -= OnTransferDataRequested;
}
}
[RelayCommand]
private Task OpenInBroswerAsync()
=> Launcher.LaunchUriAsync(new Uri(GetWebLink())).AsTask();
[RelayCommand]
private async Task PlayNextVideoAsync()
{
if (IsPageLoading || Player.IsPlayerDataLoading)
{
return;
}
var nextPart = FindNextVideo();
if (nextPart is null)
{
Player.BackToDefaultModeCommand.Execute(default);
return;
}
_initialProgress = 0;
if (Player is MpvPlayerViewModel)
{
if (!Player.IsPaused)
{
Player.TogglePlayPauseCommand.Execute(default);
}
// 留出时间给 MPV 清理资源.
await Task.Delay(1500);
}
if (nextPart is VideoPart part)
{
ChangePart(part);
}
else if (nextPart is VideoInformation video)
{
InitializePageCommand.Execute(new VideoSnapshot(video, IsPrivatePlay));
}
else
{
Player.BackToDefaultModeCommand.Execute(default);
}
}
[RelayCommand]
private void OpenUserSpace()
{
var profile = _view.Information.Publisher.User;
this.Get<NavigationViewModel>().NavigateToOver(typeof(UserSpacePage), profile);
}
[RelayCommand]
private void OpenInNewWindow()
{
if (Player.Position > 0)
{
ReportProgressCommand.Execute(Player.Position);
}
if (!Player.IsPaused)
{
Player.TogglePlayPauseCommand.Execute(default);
}
new PlayerWindow().OpenVideo(new VideoSnapshot(_view.Information, IsPrivatePlay));
}
[RelayCommand]
private void Pin()
{
var pinItem = new PinItem(_view.Information.Identifier.Id, _view.Information.Identifier.Title, _view.Information.Identifier.Cover.Uri.ToString(), PinContentType.Video);
this.Get<PinnerViewModel>().AddItemCommand.Execute(pinItem);
}
private string GetWebLink()
=> $"https://www.bilibili.com/video/av{_view.Information.Identifier.Id}";
private void ChangePart(VideoPart part)
{
if (_initialProgress == 0)
{
_initialProgress = -1;
}
_part = part;
AI.InjectVideo(_view, part);
Player.CancelNotification();
Danmaku?.ResetData(_view.Information.Identifier.Id, part.Identifier.Id);
Subtitle?.ResetData(_view.Information.Identifier.Id, part.Identifier.Id);
InitializeDashMediaCommand.Execute(part);
Subtitle.InitializeCommand.Execute(default);
InitializeNextVideo();
var partSection = Sections.FirstOrDefault(p => p is VideoPlayerPartSectionDetailViewModel) as VideoPlayerPartSectionDetailViewModel;
partSection?.UpdateSelectedPartCommand.Execute(part);
}
private void PlayerProgressChanged(int progress, int duration)
{
if (progress < duration && progress > 1 && (Danmaku?.IsEmpty() ?? false))
{
Danmaku?.ResetData(_view?.Information.Identifier.Id, _part?.Identifier.Id);
Danmaku?.LoadDanmakusCommand.Execute(duration);
}
Danmaku?.UpdatePosition(progress);
Subtitle?.UpdatePosition(progress);
}
private void PlayerStateChanged(PlayerState state)
{
if (_view == null)
{
return;
}
if (state == PlayerState.Playing)
{
Danmaku?.Resume();
}
else
{
if (state == PlayerState.Paused)
{
// 记录播放进度.
ReportProgressCommand.Execute(Player.Position);
}
Danmaku?.Pause();
}
}
private void PlayerSpeedChanged(double speed)
{
if (Danmaku is not null)
{
Danmaku.ExtraSpeed = speed;
}
}
private void PlayerMediaEnded()
{
this.Get<Microsoft.UI.Dispatching.DispatcherQueue>().TryEnqueue(() =>
{
// 清除弹幕.
Danmaku.ClearDanmaku();
Subtitle.ClearSubttile();
ReportProgressCommand.Execute(Player.Duration);
// 单视频循环.
if (CurrentLoop == VideoLoopType.Single)
{
Player.SeekCommand.Execute(0);
return;
}
var autoNext = SettingsToolkit.ReadLocalSetting(SettingNames.AutoPlayNext, true);
if ((!autoNext || !HasNextVideo) && !_isFormatChanging)
{
Player.BackToDefaultModeCommand.Execute(default);
return;
}
if (IsPageLoading || Player.IsPlayerDataLoading)
{
return;
}
var next = FindNextVideo();
if (next is null)
{
return;
}
var withoutTip = SettingsToolkit.ReadLocalSetting(SettingNames.PlayNextWithoutTip, false);
if (withoutTip)
{
PlayNextVideoCommand.Execute(default);
}
else
{
string tip = default;
if (next is VideoPart part)
{
tip = string.Format(ResourceToolkit.GetLocalizedString(StringNames.NextVideoNotificationTemplate), part.Identifier.Title);
}
else if (next is VideoInformation video)
{
tip = string.Format(ResourceToolkit.GetLocalizedString(StringNames.NextVideoNotificationTemplate), video.Identifier.Title);
}
var notification = new PlayerNotification(() => PlayNextVideoCommand.Execute(default), tip, 5);
Player.ShowNotification(notification);
}
});
}
}