From 6a151a5c2f6991559456413687871e042efa6da5 Mon Sep 17 00:00:00 2001 From: Ionite Date: Sun, 28 Jan 2024 17:37:30 -0500 Subject: [PATCH] Add NotificationServiceExtensions for package completion handling --- .../NotificationServiceExtensions.cs | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 StabilityMatrix.Avalonia/Extensions/NotificationServiceExtensions.cs diff --git a/StabilityMatrix.Avalonia/Extensions/NotificationServiceExtensions.cs b/StabilityMatrix.Avalonia/Extensions/NotificationServiceExtensions.cs new file mode 100644 index 00000000..d895382f --- /dev/null +++ b/StabilityMatrix.Avalonia/Extensions/NotificationServiceExtensions.cs @@ -0,0 +1,54 @@ +using System.Threading.Tasks; +using AsyncAwaitBestPractices; +using DesktopNotifications; +using NLog; +using StabilityMatrix.Avalonia.Services; +using StabilityMatrix.Core.Models.PackageModification; +using StabilityMatrix.Core.Models.Settings; + +namespace StabilityMatrix.Avalonia.Extensions; + +public static class NotificationServiceExtensions +{ + private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); + + public static void OnPackageInstallCompleted( + this INotificationService notificationService, + IPackageModificationRunner runner + ) + { + OnPackageInstallCompletedAsync(notificationService, runner) + .SafeFireAndForget(ex => Logger.Error(ex, "Error Showing Notification")); + } + + private static async Task OnPackageInstallCompletedAsync( + this INotificationService notificationService, + IPackageModificationRunner runner + ) + { + if (runner.Failed) + { + Logger.Error(runner.Exception, "Error Installing Package"); + + await notificationService.ShowAsync( + NotificationKey.Package_Install_Failed, + new Notification + { + Title = runner.ModificationFailedTitle, + Body = runner.ModificationFailedMessage + } + ); + } + else + { + await notificationService.ShowAsync( + NotificationKey.Package_Install_Completed, + new Notification + { + Title = runner.ModificationCompleteTitle, + Body = runner.ModificationCompleteMessage + } + ); + } + } +}