1
1
// Copyright (c) Bili Copilot. All rights reserved.
2
2
3
3
using System ;
4
+ using System . Collections . Generic ;
4
5
using System . Linq ;
5
6
using System . Text . Json ;
6
7
using System . Threading . Tasks ;
7
8
using System . Timers ;
8
9
using Bili . Copilot . Libs . Provider ;
9
10
using Bili . Copilot . Libs . Toolkit ;
10
11
using Bili . Copilot . Models . Constants . App ;
12
+ using Bili . Copilot . Models . Data . Dynamic ;
11
13
using Bili . Copilot . Models . Data . Pgc ;
12
14
using Bili . Copilot . Models . Data . Video ;
13
15
using CommunityToolkit . Mvvm . Input ;
14
16
using Microsoft . Windows . AppNotifications ;
15
17
using Microsoft . Windows . AppNotifications . Builder ;
18
+ using Windows . ApplicationModel ;
19
+ using Windows . UI . Notifications ;
20
+ using Windows . UI . StartScreen ;
16
21
17
22
namespace Bili . Copilot . ViewModels . Components ;
18
23
@@ -27,14 +32,16 @@ private NotificationViewModel()
27
32
_timer . Elapsed += OnTimerElapsedAsync ;
28
33
}
29
34
30
- private static async Task CheckVideoDynamicNotificationsAsync ( )
35
+ [ RelayCommand ]
36
+ private async Task CheckVideoDynamicNotificationsAsync ( )
31
37
{
32
38
var lastSeen = Convert . ToInt64 ( SettingsToolkit . ReadLocalSetting ( SettingNames . LastReadVideoDynamicId , string . Empty ) ) ;
33
39
var latestDynamics = await CommunityProvider . Instance . GetDynamicVideoListAsync ( true ) ;
34
40
if ( latestDynamics != null )
35
41
{
36
42
var dynamics = latestDynamics . Dynamics . Where ( p => p . DynamicType != Models . Constants . Community . DynamicItemType . Forward ) . ToList ( ) ;
37
43
var currentSeen = Convert . ToInt64 ( SettingsToolkit . ReadLocalSetting ( SettingNames . LastReadVideoDynamicId , string . Empty ) ) ;
44
+ var notifiedItems = new List < DynamicInformation > ( ) ;
38
45
if ( currentSeen > lastSeen )
39
46
{
40
47
// 有更新,发送通知.
@@ -80,19 +87,32 @@ private static async Task CheckVideoDynamicNotificationsAsync()
80
87
. SetHeroImage ( new Uri ( cover ) )
81
88
. BuildNotification ( ) ;
82
89
90
+ notifiedItems . Add ( d ) ;
83
91
AppNotificationManager . Default . Show ( notification ) ;
84
92
}
85
93
}
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
+ }
86
104
}
87
105
}
88
106
89
107
[ RelayCommand ]
90
- private void TryStart ( )
108
+ private async Task TryStartAsync ( )
91
109
{
92
110
var isNotifyEnabled = SettingsToolkit . ReadLocalSetting ( SettingNames . IsNotifyEnabled , true ) ;
93
111
if ( isNotifyEnabled && ! _timer . Enabled )
94
112
{
95
113
_timer . Start ( ) ;
114
+ await InitializeAsync ( ) ;
115
+ CheckVideoDynamicNotificationsCommand . Execute ( default ) ;
96
116
}
97
117
}
98
118
@@ -105,6 +125,17 @@ private void TryStop()
105
125
}
106
126
}
107
127
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
+
108
139
private async void OnTimerElapsedAsync ( object sender , ElapsedEventArgs e )
109
140
{
110
141
var isNotifyEnabled = SettingsToolkit . ReadLocalSetting ( SettingNames . IsNotifyEnabled , true ) ;
0 commit comments