Skip to content

Commit 61d35f9

Browse files
authored
[试验]添加磁贴通知 (#274)
1 parent 702f17b commit 61d35f9

File tree

3 files changed

+102
-2
lines changed

3 files changed

+102
-2
lines changed

src/ViewModels/Components/NotificationViewModel/NotificationViewModel.Properties.cs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public sealed partial class NotificationViewModel
1212
{
1313
private static readonly Lazy<NotificationViewModel> _lazyInstance = new(() => new NotificationViewModel());
1414
private readonly Timer _timer;
15+
private bool _isTileSupport;
1516

1617
/// <summary>
1718
/// 实例.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) Bili Copilot. All rights reserved.
2+
3+
using System.Collections.Generic;
4+
using Bili.Copilot.Models.Data.Dynamic;
5+
using Bili.Copilot.Models.Data.Pgc;
6+
using Bili.Copilot.Models.Data.Video;
7+
using Windows.Data.Xml.Dom;
8+
using Windows.UI.Notifications;
9+
10+
namespace Bili.Copilot.ViewModels.Components;
11+
12+
/// <summary>
13+
/// 通知视图模型.
14+
/// </summary>
15+
public sealed partial class NotificationViewModel
16+
{
17+
/// <summary>
18+
/// 更新磁贴.
19+
/// </summary>
20+
public static void UpdateTile(List<DynamicInformation> dynamics)
21+
{
22+
foreach (var d in dynamics)
23+
{
24+
var title = string.Empty;
25+
var cover = string.Empty;
26+
if (d.Data is VideoInformation videoInfo)
27+
{
28+
title = videoInfo.Identifier.Title;
29+
cover = videoInfo.Identifier.Cover.Uri;
30+
}
31+
else if (d.Data is EpisodeInformation episodeInfo)
32+
{
33+
title = episodeInfo.Identifier.Title;
34+
cover = episodeInfo.Identifier.Cover.Uri;
35+
}
36+
37+
var avatar = d.User.Avatar.Uri;
38+
var publisher = d.User.Name;
39+
40+
var xmlContent = $"""
41+
<?xml version="1.0" encoding="utf-8"?>
42+
<tile>
43+
<visual>
44+
<binding template="TileMedium" branding="name">
45+
<image src="{cover}" placement="background" hint-overlay="60" />
46+
<image src="{avatar}" placement="peek" hint-crop="circle" />
47+
<text hint-style="caption" hint-wrap="true">{title}</text>
48+
</binding>
49+
<binding template="TileWide" branding="nameAndLogo">
50+
<image src="{cover}" placement="background" hint-overlay="60" />
51+
<text hint-maxLines="2" hint-style="base" hint-wrap="true">{title}</text>
52+
<text hint-style="caption">{publisher}</text>
53+
</binding>
54+
<binding template="TileLarge" branding="nameAndLogo">
55+
<image src="{cover}" placement="background" hint-overlay="60" />
56+
<text hint-style="subtitle" hint-wrap="true">{title}</text>
57+
<text hint-style="base"></text>
58+
<text hint-style="base">{publisher}</text>
59+
</binding>
60+
</visual>
61+
</tile>
62+
""";
63+
var xml = new XmlDocument();
64+
xml.LoadXml(xmlContent);
65+
TileUpdateManager.CreateTileUpdaterForApplication().Update(new TileNotification(xml) { Tag = d.Id });
66+
}
67+
}
68+
}

src/ViewModels/Components/NotificationViewModel/NotificationViewModel.cs

+33-2
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
// Copyright (c) Bili Copilot. All rights reserved.
22

33
using System;
4+
using System.Collections.Generic;
45
using System.Linq;
56
using System.Text.Json;
67
using System.Threading.Tasks;
78
using System.Timers;
89
using Bili.Copilot.Libs.Provider;
910
using Bili.Copilot.Libs.Toolkit;
1011
using Bili.Copilot.Models.Constants.App;
12+
using Bili.Copilot.Models.Data.Dynamic;
1113
using Bili.Copilot.Models.Data.Pgc;
1214
using Bili.Copilot.Models.Data.Video;
1315
using CommunityToolkit.Mvvm.Input;
1416
using Microsoft.Windows.AppNotifications;
1517
using Microsoft.Windows.AppNotifications.Builder;
18+
using Windows.ApplicationModel;
19+
using Windows.UI.Notifications;
20+
using Windows.UI.StartScreen;
1621

1722
namespace Bili.Copilot.ViewModels.Components;
1823

@@ -27,14 +32,16 @@ private NotificationViewModel()
2732
_timer.Elapsed += OnTimerElapsedAsync;
2833
}
2934

30-
private static async Task CheckVideoDynamicNotificationsAsync()
35+
[RelayCommand]
36+
private async Task CheckVideoDynamicNotificationsAsync()
3137
{
3238
var lastSeen = Convert.ToInt64(SettingsToolkit.ReadLocalSetting(SettingNames.LastReadVideoDynamicId, string.Empty));
3339
var latestDynamics = await CommunityProvider.Instance.GetDynamicVideoListAsync(true);
3440
if (latestDynamics != null)
3541
{
3642
var dynamics = latestDynamics.Dynamics.Where(p => p.DynamicType != Models.Constants.Community.DynamicItemType.Forward).ToList();
3743
var currentSeen = Convert.ToInt64(SettingsToolkit.ReadLocalSetting(SettingNames.LastReadVideoDynamicId, string.Empty));
44+
var notifiedItems = new List<DynamicInformation>();
3845
if (currentSeen > lastSeen)
3946
{
4047
// 有更新,发送通知.
@@ -80,19 +87,32 @@ private static async Task CheckVideoDynamicNotificationsAsync()
8087
.SetHeroImage(new Uri(cover))
8188
.BuildNotification();
8289

90+
notifiedItems.Add(d);
8391
AppNotificationManager.Default.Show(notification);
8492
}
8593
}
94+
95+
if (_isTileSupport)
96+
{
97+
if (notifiedItems.Count == 0)
98+
{
99+
dynamics.Take(5).ToList().ForEach(notifiedItems.Add);
100+
}
101+
102+
UpdateTile(notifiedItems);
103+
}
86104
}
87105
}
88106

89107
[RelayCommand]
90-
private void TryStart()
108+
private async Task TryStartAsync()
91109
{
92110
var isNotifyEnabled = SettingsToolkit.ReadLocalSetting(SettingNames.IsNotifyEnabled, true);
93111
if (isNotifyEnabled && !_timer.Enabled)
94112
{
95113
_timer.Start();
114+
await InitializeAsync();
115+
CheckVideoDynamicNotificationsCommand.Execute(default);
96116
}
97117
}
98118

@@ -105,6 +125,17 @@ private void TryStop()
105125
}
106126
}
107127

128+
private async Task InitializeAsync()
129+
{
130+
var entry = (await Package.Current.GetAppListEntriesAsync()).First();
131+
_isTileSupport = StartScreenManager.GetDefault().SupportsAppListEntry(entry);
132+
133+
if (_isTileSupport)
134+
{
135+
TileUpdateManager.CreateTileUpdaterForApplication().EnableNotificationQueue(true);
136+
}
137+
}
138+
108139
private async void OnTimerElapsedAsync(object sender, ElapsedEventArgs e)
109140
{
110141
var isNotifyEnabled = SettingsToolkit.ReadLocalSetting(SettingNames.IsNotifyEnabled, true);

0 commit comments

Comments
 (0)