Skip to content

Commit cc03479

Browse files
feat: Add in-app action and URI command to clear all notifications with logging and automated tests
This commit introduces multiple enhancements for clearing notifications: 1. **Logging for Verification**: - The `NotificationHostService.CancelAllNotifications` method now logs a confirmation message when successfully executed. This aids in automated verification. 2. **Updated GitHub Actions Workflow (`clear_notifications_test.yml`)**: - The workflow trigger is changed from `workflow_dispatch` to `on: push` to run on every commit. - A new step has been added to check the application's log files after the clear notifications URI is called. The workflow verifies that the specific confirmation message from `CancelAllNotifications` is present in the logs, ensuring the action was performed. 3. **In-App Automated Action ("Clear All Notifications")**: - A new in-app automated action, "清除所有提醒" (Clear All Notifications), with ID `classisland.notifications.clearAll`, has been implemented. - `ClearAllNotificationsActionHandler.cs` was created to contain the logic, which calls `NotificationHostService.CancelAllNotifications()`. - The action and its handler are registered in `App.xaml.cs`, making it available for use within ClassIsland's automation rules. 4. **URI Command for Clearing Notifications**: (Implemented in a previous commit, tested by the workflow) - The `UriNavigationService` handles the `classisland://app/clearAllNotifications` URI, which also triggers `NotificationHostService.CancelAllNotifications()`. 5. **Build Error Fix**: (Resolved in a previous commit) - Corrected an incorrect namespace reference for `NotificationHostService` in `UriNavigationService.cs` that was causing build failures. These changes provide both a manual/external way (URI) and an integrated/automated way (in-app action) to clear all notifications, along with an automated GitHub Actions workflow to verify the URI functionality using log-based verification.
1 parent b306136 commit cc03479

4 files changed

Lines changed: 65 additions & 2 deletions

File tree

.github/workflows/clear_notifications_test.yml

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
name: Test Clear All Notifications URI
22

3-
on:
4-
workflow_dispatch:
3+
on: push
54

65
jobs:
76
test_clear_all_notifications_uri:
@@ -35,3 +34,31 @@ jobs:
3534

3635
- name: Wait for URI to be processed
3736
run: Start-Sleep -Seconds 5
37+
38+
- name: Verify notifications cleared in logs
39+
shell: pwsh
40+
run: |
41+
$logDir = "ClassIsland/bin/Release/net8.0-windows/Logs"
42+
Write-Host "Checking for logs in: $logDir"
43+
if (Test-Path $logDir) {
44+
$latestLog = Get-ChildItem -Path $logDir -Filter "*.log" | Sort-Object LastWriteTime -Descending | Select-Object -First 1
45+
if ($latestLog) {
46+
Write-Host "Found log file: $($latestLog.FullName)"
47+
$logContent = Get-Content -Path $latestLog.FullName -Raw
48+
$expectedMessage = "All notifications successfully cancelled via CancelAllNotifications method."
49+
if ($logContent -match [regex]::Escape($expectedMessage)) {
50+
Write-Host "Success: Verification message found in log."
51+
} else {
52+
Write-Host "Error: Verification message NOT found in log."
53+
Write-Host "Log content:"
54+
Write-Host $logContent
55+
exit 1
56+
}
57+
} else {
58+
Write-Host "Error: No log files found in $logDir"
59+
exit 1
60+
}
61+
} else {
62+
Write-Host "Error: Log directory $logDir not found."
63+
exit 1
64+
}

ClassIsland/App.xaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,9 +687,11 @@ private async void App_OnStartup(object sender, StartupEventArgs e)
687687
"classisland.notification.weather", "显示天气提醒", PackIconKind.SunWirelessOutline);
688688
services.AddAction("classisland.app.quit", "退出 ClassIsland", PackIconKind.ExitToApp, (_, _) => Current.Stop());
689689
services.AddAction<AppRestartActionSettings,AppRestartActionSettingsControl>("classisland.app.restart", "重启 ClassIsland", PackIconKind.Restart);
690+
services.AddAction("classisland.notifications.clearAll", "清除所有提醒", PackIconKind.NotificationClearAll);
690691
// 行动处理
691692
services.AddHostedService<AppRestartActionHandler>();
692693
services.AddHostedService<RunActionHandler>();
694+
services.AddHostedService<ClearAllNotificationsActionHandler>();
693695
services.AddHostedService<AppSettingsActionHandler>();
694696
services.AddHostedService<SleepActionHandler>();
695697
// 认证提供方
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
using ClassIsland.Core.Abstractions.Services; // For IActionService
4+
using ClassIsland.Services; // For INotificationHostService (assuming it's here)
5+
using Microsoft.Extensions.Hosting; // For IHostedService
6+
7+
namespace ClassIsland.Services.ActionHandlers;
8+
9+
public class ClearAllNotificationsActionHandler : IHostedService
10+
{
11+
private readonly INotificationHostService _notificationHostService;
12+
13+
public ClearAllNotificationsActionHandler(IActionService actionService, INotificationHostService notificationHostService)
14+
{
15+
_notificationHostService = notificationHostService;
16+
// The GUID for the action ID will be defined in App.xaml.cs
17+
// For example: "classisland.notifications.clearAll"
18+
actionService.RegisterActionHandler("classisland.notifications.clearAll", HandleClearAllNotifications);
19+
}
20+
21+
private void HandleClearAllNotifications(object? settings, string guid)
22+
{
23+
_notificationHostService.CancelAllNotifications();
24+
// Optionally, log that this action was triggered via in-app automation
25+
// App.GetService<ILogger<ClearAllNotificationsActionHandler>>().LogInformation("ClearAllNotifications action triggered via in-app automation.");
26+
// However, to use GetService, this class would need to be modified or have ILogger injected.
27+
// For now, direct logging is omitted here but can be added if NotificationHostService's own logging isn't sufficient.
28+
}
29+
30+
public Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask;
31+
32+
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
33+
}

ClassIsland/Services/NotificationHostService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ public void CancelAllNotifications()
269269
var r = RequestQueue.Dequeue();
270270
r.CompletedTokenSource.Cancel();
271271
}
272+
Logger.LogInformation("All notifications successfully cancelled via CancelAllNotifications method.");
272273
}
273274

274275
public event PropertyChangedEventHandler? PropertyChanged;

0 commit comments

Comments
 (0)