-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathLiveItemViewModel.cs
110 lines (96 loc) · 3.92 KB
/
LiveItemViewModel.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
// 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.Core;
using CommunityToolkit.Mvvm.Input;
using Humanizer;
using Microsoft.Extensions.Logging;
using Richasy.BiliKernel.Bili.User;
using Richasy.BiliKernel.Models.Media;
using Richasy.WinUI.Share.Base;
using Richasy.WinUI.Share.ViewModels;
using Windows.System;
using WinRT;
namespace BiliCopilot.UI.ViewModels.Items;
/// <summary>
/// 直播条目视图模型.
/// </summary>
[GeneratedBindableCustomProperty]
public sealed partial class LiveItemViewModel : ViewModelBase<LiveInformation>
{
private readonly Action<LiveItemViewModel>? _removeAction;
/// <summary>
/// Initializes a new instance of the <see cref="LiveItemViewModel"/> class.
/// </summary>
public LiveItemViewModel(LiveInformation data, LiveCardStyle style, Action<LiveItemViewModel>? removeAction = default)
: base(data)
{
Title = data.Identifier.Title;
Cover = data.Identifier.Cover?.Uri;
Author = data.User?.Name;
Avatar = data.User?.Avatar?.Uri;
Style = style;
ViewerCount = data.GetExtensionIfNotNull<double>(LiveExtensionDataId.ViewerCount);
Subtitle = data.GetExtensionIfNotNull<string>(VideoExtensionDataId.Subtitle);
TagName = data.GetExtensionIfNotNull<string>(LiveExtensionDataId.TagName);
IsLiving = data.GetExtensionIfNotNull<bool?>(LiveExtensionDataId.IsLiving);
var collectTime = data.GetExtensionIfNotNull<DateTimeOffset?>(LiveExtensionDataId.CollectTime);
if (collectTime is not null)
{
CollectRelativeTime = collectTime.Value.Humanize();
}
_removeAction = removeAction;
}
[RelayCommand]
private void Play()
{
var preferDisplayMode = SettingsToolkit.ReadLocalSetting(SettingNames.DefaultPlayerDisplayMode, PlayerDisplayMode.Default);
if (preferDisplayMode == PlayerDisplayMode.NewWindow)
{
OpenInNewWindowCommand.Execute(default);
return;
}
var preferPlayer = SettingsToolkit.ReadLocalSetting(SettingNames.PlayerType, PlayerType.Island);
var useWebPlayer = SettingsToolkit.ReadLocalSetting(SettingNames.UseWebPlayerWhenLive, false);
if (preferPlayer == PlayerType.Web || useWebPlayer)
{
this.Get<NavigationViewModel>().NavigateToOver(typeof(WebPlayerPage), GetWebUrl());
return;
}
this.Get<NavigationViewModel>().NavigateToOver(typeof(LivePlayerPage), Data.Identifier);
}
[RelayCommand]
private Task OpenInBroswerAsync()
=> Launcher.LaunchUriAsync(new Uri(GetWebUrl())).AsTask();
[RelayCommand]
private void OpenInNewWindow()
=> new PlayerWindow().OpenLive(Data.Identifier);
[RelayCommand]
private async Task RemoveHistoryAsync()
{
try
{
await this.Get<IViewHistoryService>().RemoveLiveHistoryItemAsync(Data);
_removeAction?.Invoke(this);
}
catch (Exception ex)
{
this.Get<ILogger<LiveItemViewModel>>().LogError(ex, "移除直播历史记录失败.");
this.Get<AppViewModel>().ShowTipCommand.Execute((ResourceToolkit.GetLocalizedString(Models.Constants.StringNames.FailedToRemoveVideoFromHistory), InfoType.Error));
}
}
[RelayCommand]
private void ShowUserSpace()
=> this.Get<NavigationViewModel>().NavigateToOver(typeof(UserSpacePage), Data.User);
[RelayCommand]
private void Pin()
{
var pinItem = new PinItem(Data.Identifier.Id, Data.Identifier.Title, Data.Identifier.Cover.Uri.ToString(), PinContentType.Live);
this.Get<PinnerViewModel>().AddItemCommand.Execute(pinItem);
}
private string GetWebUrl()
=> $"https://live.bilibili.com/{Data.Identifier.Id}";
}