Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 58 additions & 26 deletions Common/Infrastructure/GlobalNotification.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.UI.Xaml.Media.Imaging;
using System.Diagnostics;
using Microsoft.UI.Xaml.Media.Imaging;

namespace Tunetastic.Common.Infrastructure;

Expand All @@ -22,9 +23,9 @@ private static IconSource GetImageIconSource()
/// The notification will be shown with a blue color and will automatically close.
/// </summary>
/// <param name="message">The message content to display in the notification.</param>
public async static void Info(string message)
public static void Info(string message)
{
Growl.InfoGlobal(new GrowlInfo
Show(() => Growl.InfoGlobal(new GrowlInfo
{
ShowDateTime = false,
IsIconVisible = true,
Expand All @@ -36,21 +37,17 @@ public async static void Info(string message)
IsClosable = true,
Title = $"{ProcessInfoHelper.ProductName} ({ProcessInfoHelper.VersionWithPrefix})",
Message = message,
});

await Task.Delay(20);

MainWindow._instance.BringToFront();
}));
}

/// <summary>
/// Displays a success notification to the user with the specified message.
/// The notification will be shown with a green color and will automatically close.
/// </summary>
/// <param name="message">The message content to display in the notification.</param>
public async static void Success(string message)
public static void Success(string message)
{
Growl.SuccessGlobal(new GrowlInfo
Show(() => Growl.SuccessGlobal(new GrowlInfo
{
ShowDateTime = false,
IsIconVisible = true,
Expand All @@ -62,21 +59,17 @@ public async static void Success(string message)
IsClosable = true,
Title = $"{ProcessInfoHelper.ProductName} ({ProcessInfoHelper.VersionWithPrefix})",
Message = message,
});

await Task.Delay(20);

MainWindow._instance.BringToFront();
}));
}

/// <summary>
/// Displays an error notification to the user with the specified message.
/// The notification will be shown with a red color and will automatically close.
/// </summary>
/// <param name="message">The error message content to display in the notification.</param>
public async static void Error(string message)
public static void Error(string message)
{
Growl.ErrorGlobal(new GrowlInfo
Show(() => Growl.ErrorGlobal(new GrowlInfo
{
ShowDateTime = false,
IsIconVisible = true,
Expand All @@ -87,21 +80,17 @@ public async static void Error(string message)
WaitTime = TimeSpan.FromSeconds(10),
Title = "Tunetastic",
Message = message
});

await Task.Delay(20);

MainWindow._instance.BringToFront();
}));
}

/// <summary>
/// Displays a warning notification to the user with the specified message.
/// The notification will be shown with a yellow color and will automatically close.
/// </summary>
/// <param name="message">The message content to display in the warning notification.</param>
public async static void Warning(string message)
public static void Warning(string message)
{
Growl.WarningGlobal(new GrowlInfo
Show(() => Growl.WarningGlobal(new GrowlInfo
{
ShowDateTime = false,
IsIconVisible = true,
Expand All @@ -112,10 +101,53 @@ public async static void Warning(string message)
IsClosable = true,
Title = "Tunetastic",
Message = message
}));
}

/// <summary>
/// Dispatches the notification creation and display onto the UI thread safely.
/// </summary>
/// <param name="growlAction">The delegate creating and presenting the Growl notification.</param>
private static void Show(Action growlAction)
{
RunOnUIThread(async () =>
{
try
{
growlAction();
await Task.Delay(20);
MainWindow._instance?.BringToFront();
}
catch (Exception ex)
{
Debug.WriteLine($"[GlobalNotification] Failed to show notification: {ex}");
}
});
}

await Task.Delay(20);
/// <summary>
/// Runs the given action on the UI thread using the main window's DispatcherQueue.
/// If no dispatcher is available (e.g. during early startup or late teardown),
/// the action is safely dropped to avoid cross-thread COM exceptions.
/// </summary>
/// <param name="action">The action to run.</param>
private static void RunOnUIThread(Action action)
{
var dispatcherQueue = App.MainWindow?.DispatcherQueue;
if (dispatcherQueue == null)
{
Debug.WriteLine("[GlobalNotification] No UI DispatcherQueue available; notification dropped.");
return;
}

MainWindow._instance.BringToFront();
if (dispatcherQueue.HasThreadAccess)
{
action();
}
else
{
dispatcherQueue.TryEnqueue(() => action());
}
}
}

2 changes: 1 addition & 1 deletion Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<Identity
Name="AMit-KP.Tunetastic"
Publisher="CN=B90A5939-9B2E-481A-9AAB-88AC3440759B"
Version="1.77.17.0"/>
Version="1.78.3.0"/>

<mp:PhoneIdentity PhoneProductId="1e56cd21-fffd-4018-bebd-94b2cb6d6063" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>

Expand Down
6 changes: 3 additions & 3 deletions Tunetastic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<IsAotCompatible>False</IsAotCompatible>
<WindowsPackageType>MSIX</WindowsPackageType>
<ApplicationIcon>Assets\AppIcon.ico</ApplicationIcon>
<Version>1.77.17</Version>
<Version>1.78.3</Version>
<!--Remove git hash from assembly informational version in .NET >= 8, Example: false = 1.0.0, true = 1.0.0+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-->
<IncludeSourceRevisionInInformationalVersion>true</IncludeSourceRevisionInInformationalVersion>
<NoWarn>$(NoWarn);CS0618</NoWarn>
Expand All @@ -37,8 +37,8 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/AMit-KP/Tunetastic.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<AssemblyVersion>1.77.17.3940</AssemblyVersion>
<FileVersion>1.77.17.3940</FileVersion>
<AssemblyVersion>1.78.3.3943</AssemblyVersion>
<FileVersion>1.78.3.3943</FileVersion>
<ShouldCreateLogs>True</ShouldCreateLogs>
<AdvancedSettingsExpanded>False</AdvancedSettingsExpanded>
<UpdateAssemblyVersion>False</UpdateAssemblyVersion>
Expand Down
Loading