diff --git a/BetterGenshinImpact/App.xaml.cs b/BetterGenshinImpact/App.xaml.cs index 8b5fb7dc..24a268b6 100644 --- a/BetterGenshinImpact/App.xaml.cs +++ b/BetterGenshinImpact/App.xaml.cs @@ -185,6 +185,8 @@ public partial class App : Application protected override async void OnExit(ExitEventArgs e) { base.OnExit(e); + + TempManager.CleanUp(); await _host.StopAsync(); _host.Dispose(); diff --git a/BetterGenshinImpact/BetterGenshinImpact.csproj b/BetterGenshinImpact/BetterGenshinImpact.csproj index 9e81a682..b138ab2d 100644 --- a/BetterGenshinImpact/BetterGenshinImpact.csproj +++ b/BetterGenshinImpact/BetterGenshinImpact.csproj @@ -47,6 +47,7 @@ + diff --git a/BetterGenshinImpact/Helpers/TempManager.cs b/BetterGenshinImpact/Helpers/TempManager.cs new file mode 100644 index 00000000..25ae16f2 --- /dev/null +++ b/BetterGenshinImpact/Helpers/TempManager.cs @@ -0,0 +1,26 @@ +using System; +using System.IO; +using BetterGenshinImpact.Core.Config; + +namespace BetterGenshinImpact.Helpers; + +public class TempManager +{ + public static readonly string TempDirectory = Global.Absolute("User/Temp"); + static TempManager() + { + Directory.CreateDirectory(TempDirectory); + } + + public static void CleanUp() + { + try + { + DirectoryHelper.DeleteDirectoryRecursively(TempDirectory); + } + catch + { + // Suppress any exceptions to avoid exposing errors + } + } +} \ No newline at end of file diff --git a/BetterGenshinImpact/Service/Notification/NotificationConfig.cs b/BetterGenshinImpact/Service/Notification/NotificationConfig.cs index f8826963..1da20fe9 100644 --- a/BetterGenshinImpact/Service/Notification/NotificationConfig.cs +++ b/BetterGenshinImpact/Service/Notification/NotificationConfig.cs @@ -26,4 +26,11 @@ public partial class NotificationConfig : ObservableObject /// [ObservableProperty] private bool _includeScreenShot = false; + + /// + /// windows uwp 通知是否启用 + /// + [ObservableProperty] + private bool _windowsUwpNotificationEnabled = false; + } diff --git a/BetterGenshinImpact/Service/Notification/NotificationService.cs b/BetterGenshinImpact/Service/Notification/NotificationService.cs index 4e62ce91..98942c0f 100644 --- a/BetterGenshinImpact/Service/Notification/NotificationService.cs +++ b/BetterGenshinImpact/Service/Notification/NotificationService.cs @@ -6,11 +6,13 @@ using BetterGenshinImpact.Service.Notifier.Exception; using BetterGenshinImpact.Service.Notifier.Interface; using Microsoft.Extensions.Hosting; using System; +using System.Drawing; using System.Net.Http; using System.Text; using System.Text.Json; using System.Threading; using System.Threading.Tasks; +using System.Windows.Media.Imaging; using BetterGenshinImpact.GameTask; using BetterGenshinImpact.Service.Notification.Model.Enum; @@ -56,6 +58,10 @@ public class NotificationService : IHostedService { _notifierManager.RegisterNotifier(new WebhookNotifier(NotifyHttpClient, TaskContext.Instance().Config.NotificationConfig.WebhookEndpoint)); } + if (TaskContext.Instance().Config.NotificationConfig.WindowsUwpNotificationEnabled) + { + _notifierManager.RegisterNotifier(new WindowsUwpNotifier()); + } } public void RefreshNotifiers() @@ -73,12 +79,13 @@ public class NotificationService : IHostedService { return NotificationTestResult.Error("通知类型未启用"); } - await notifier.SendNotificationAsync(new TestNotificationData + await notifier.SendAsync(new TestNotificationData { Event = NotificationEvent.Test, Action = NotificationAction.Started, Conclusion = NotificationConclusion.Success, - Message = "测试通知" + Message = "测试通知", + // Screenshot = }); return NotificationTestResult.Success(); } diff --git a/BetterGenshinImpact/Service/Notifier/Interface/INotifier.cs b/BetterGenshinImpact/Service/Notifier/Interface/INotifier.cs index b705cdd5..d4023bc3 100644 --- a/BetterGenshinImpact/Service/Notifier/Interface/INotifier.cs +++ b/BetterGenshinImpact/Service/Notifier/Interface/INotifier.cs @@ -8,5 +8,5 @@ public interface INotifier { string Name { get; } - Task SendNotificationAsync(INotificationData content); + Task SendAsync(INotificationData data); } diff --git a/BetterGenshinImpact/Service/Notifier/NotifierManager.cs b/BetterGenshinImpact/Service/Notifier/NotifierManager.cs index da1a344c..17e4e42a 100644 --- a/BetterGenshinImpact/Service/Notifier/NotifierManager.cs +++ b/BetterGenshinImpact/Service/Notifier/NotifierManager.cs @@ -41,7 +41,7 @@ public class NotifierManager { try { - await notifier.SendNotificationAsync(content); + await notifier.SendAsync(content); } catch (System.Exception ex) { diff --git a/BetterGenshinImpact/Service/Notifier/WebhookNotifier.cs b/BetterGenshinImpact/Service/Notifier/WebhookNotifier.cs index 93f73a90..8600dcf4 100644 --- a/BetterGenshinImpact/Service/Notifier/WebhookNotifier.cs +++ b/BetterGenshinImpact/Service/Notifier/WebhookNotifier.cs @@ -15,6 +15,11 @@ public class WebhookNotifier : INotifier public string Endpoint { get; set; } private readonly HttpClient _httpClient; + + private readonly JsonSerializerOptions _jsonSerializerOptions = new() + { + PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower, + }; public WebhookNotifier(HttpClient httpClient, string endpoint = "") { @@ -22,7 +27,7 @@ public class WebhookNotifier : INotifier Endpoint = endpoint; } - public async Task SendNotificationAsync(INotificationData content) + public async Task SendAsync(INotificationData content) { try { @@ -47,10 +52,7 @@ public class WebhookNotifier : INotifier private StringContent TransformData(INotificationData notificationData) { // using object type here so it serializes the interface correctly - var serializedData = JsonSerializer.Serialize(notificationData, new JsonSerializerOptions - { - PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower, - }); + var serializedData = JsonSerializer.Serialize(notificationData, _jsonSerializerOptions); return new StringContent(serializedData, Encoding.UTF8, "application/json"); } diff --git a/BetterGenshinImpact/Service/Notifier/WindowsUwpNotifier.cs b/BetterGenshinImpact/Service/Notifier/WindowsUwpNotifier.cs new file mode 100644 index 00000000..e151e714 --- /dev/null +++ b/BetterGenshinImpact/Service/Notifier/WindowsUwpNotifier.cs @@ -0,0 +1,40 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using BetterGenshinImpact.Helpers; +using BetterGenshinImpact.Service.Notification.Model; +using BetterGenshinImpact.Service.Notifier.Interface; +using Microsoft.Toolkit.Uwp.Notifications; + +namespace BetterGenshinImpact.Service.Notifier; + +public class WindowsUwpNotifier : INotifier +{ + public string Name => "Windows通知"; + + public Task SendAsync(INotificationData data) + { + var toastBuilder = new ToastContentBuilder() + .AddHeader("BetterGI", "BetterGI", "action=click"); + + if (data.Screenshot != null) + { + string uniqueFileName = $"notification_image_{Guid.NewGuid()}.png"; + string imagePath = Path.Combine(TempManager.TempDirectory, uniqueFileName); + data.Screenshot.Save(imagePath, System.Drawing.Imaging.ImageFormat.Png); + toastBuilder.AddHeroImage(new Uri(imagePath)); + } + + if (!string.IsNullOrEmpty(data.Message)) + { + toastBuilder.AddText(data.Message); + } + + toastBuilder.Show(toast => + { + toast.Group = data.Event.ToString(); + toast.ExpirationTime = DateTime.Now.AddHours(12); + }); + return Task.CompletedTask; + } +} \ No newline at end of file diff --git a/BetterGenshinImpact/View/Pages/CommonSettingsPage.xaml b/BetterGenshinImpact/View/Pages/CommonSettingsPage.xaml index a9e0f1ef..75959825 100644 --- a/BetterGenshinImpact/View/Pages/CommonSettingsPage.xaml +++ b/BetterGenshinImpact/View/Pages/CommonSettingsPage.xaml @@ -453,7 +453,7 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + (this, "RefreshSettings", new object(), "重新计算控件位置")); } - + [RelayCommand] private void OnSwitchMaskEnabled() { @@ -106,7 +109,7 @@ public partial class CommonSettingsPageViewModel : ObservableObject, INavigation Process.Start("explorer.exe", path); } - + [RelayCommand] private async Task OnTestWebhook() { @@ -119,4 +122,18 @@ public partial class CommonSettingsPageViewModel : ObservableObject, INavigation IsLoading = false; } -} + + [RelayCommand] + private async Task OnTestWindowsUwpNotification() + { + var res = await _notificationService.TestNotifierAsync(); + if(res.IsSuccess) + { + Toast.Success(res.Message); + } + else + { + Toast.Error(res.Message); + } + } +} \ No newline at end of file