diff --git a/RadioDJViewer/Main.cs b/RadioDJViewer/Main.cs index 86d74d6..2d17459 100644 --- a/RadioDJViewer/Main.cs +++ b/RadioDJViewer/Main.cs @@ -25,6 +25,8 @@ namespace RadioDJViewer { private string _currentTrackKey = string.Empty; private string _playbackTrackKey = string.Empty; + private string _previousToastTitle = string.Empty; + private string _previousToastArtist = string.Empty; private double _playbackElapsedSeconds = 0; private double _playbackDurationSeconds = 0; private bool _playbackPaused = true; @@ -293,38 +295,6 @@ namespace RadioDJViewer var xml = await response.Content.ReadAsStringAsync(); lastApiXml = xml; ParseAndDisplaySongInfo(xml); - try - { - var newKey = $"{marqueeTextArtist}|{marqueeTextTitle}"; - if (!string.Equals(newKey, _currentTrackKey, StringComparison.Ordinal)) - { - _currentTrackKey = newKey; - ShowTemporaryStatus("Song Change Detected", 2000); - bool toastEnabled = IsToastEnabledForActiveProfile(); -#if DEBUG - System.Diagnostics.Debug.WriteLine($"[Toast] Track changed. Enabled={toastEnabled}, Profile={loadedProfile?.Name ?? "(null)"}"); -#endif - if (toastEnabled) - { - try - { - string outputImagePath = GetProfileOutputImagePath(); -#if DEBUG - System.Diagnostics.Debug.WriteLine($"[Toast] Trigger with title='{marqueeTextTitle}', artist='{marqueeTextArtist}', imagePath='{outputImagePath}'"); -#endif - ShowTrackChangeToast(marqueeTextTitle, marqueeTextArtist, outputImagePath); - } - catch (Exception ex) - { -#if DEBUG - System.Diagnostics.Debug.WriteLine($"[Toast] Trigger error: {ex}"); - MessageBox.Show($"Toast trigger failed: {ex.Message}", "Toast Debug", MessageBoxButtons.OK, MessageBoxIcon.Warning); -#endif - } - } - } - } - catch { } } } catch { } @@ -700,6 +670,8 @@ namespace RadioDJViewer { UpdateCurrentSongImage(null); } + + HandleToastForTrackChange(title, artist); } catch { @@ -1009,6 +981,67 @@ namespace RadioDJViewer return loadedProfile != null && loadedProfile.ToastNotificationsEnabled; } + private void HandleToastForTrackChange(string title, string artist) + { + string normalizedTitle = NormalizeTrackMetadata(title); + string normalizedArtist = NormalizeTrackMetadata(artist); + + if (string.Equals(normalizedTitle, _previousToastTitle, StringComparison.OrdinalIgnoreCase) && + string.Equals(normalizedArtist, _previousToastArtist, StringComparison.OrdinalIgnoreCase)) + { + return; + } + + _previousToastTitle = normalizedTitle; + _previousToastArtist = normalizedArtist; + + if (!IsToastEnabledForActiveProfile()) + return; + + if (!HasValidTrackMetadata(normalizedTitle, normalizedArtist)) + return; + + try + { + string outputImagePath = GetProfileOutputImagePath(); +#if DEBUG + System.Diagnostics.Debug.WriteLine($"[Toast] Track change accepted. title='{normalizedTitle}', artist='{normalizedArtist}', imagePath='{outputImagePath}'"); +#endif + ShowTrackChangeToast(normalizedTitle, normalizedArtist, outputImagePath); + } + catch (Exception ex) + { +#if DEBUG + System.Diagnostics.Debug.WriteLine($"[Toast] Trigger error: {ex}"); + MessageBox.Show($"Toast trigger failed: {ex.Message}", "Toast Debug", MessageBoxButtons.OK, MessageBoxIcon.Warning); +#endif + } + } + + private static string NormalizeTrackMetadata(string value) + { + return string.IsNullOrWhiteSpace(value) ? string.Empty : value.Trim(); + } + + private static bool HasValidTrackMetadata(string title, string artist) + { + return IsValidToastValue(title) && IsValidToastValue(artist); + } + + private static bool IsValidToastValue(string value) + { + if (string.IsNullOrWhiteSpace(value)) + return false; + + string normalized = value.Trim(); + return !normalized.Equals("No Title", StringComparison.OrdinalIgnoreCase) + && !normalized.Equals("No Artist", StringComparison.OrdinalIgnoreCase) + && !normalized.Equals("Unknown", StringComparison.OrdinalIgnoreCase) + && !normalized.Equals("No title", StringComparison.OrdinalIgnoreCase) + && !normalized.Equals("No artist", StringComparison.OrdinalIgnoreCase) + && !normalized.Equals("unknown", StringComparison.OrdinalIgnoreCase); + } + private string GetProfileOutputImagePath() { if (string.IsNullOrWhiteSpace(outputFolderPath) || string.IsNullOrWhiteSpace(loadedProfile?.OutputImageName)) @@ -1017,8 +1050,7 @@ namespace RadioDJViewer try { string imageName = Path.ChangeExtension(loadedProfile.OutputImageName, ".png"); - string fullPath = Path.GetFullPath(Path.Combine(outputFolderPath, imageName)); - return fullPath; + return Path.GetFullPath(Path.Combine(outputFolderPath, imageName)); } catch { @@ -1032,17 +1064,7 @@ namespace RadioDJViewer string safeArtist = string.IsNullOrWhiteSpace(artist) ? "No artist" : artist; DateTime now = DateTime.Now; string attribution = $"Date: {now:MM/dd/yyyy} | Time: {now:hh:mm tt}"; - try - { - ToastNotificationService.ShowTrackChangeToast(safeTitle, safeArtist, attribution, outputImagePath); - } - catch (Exception ex) - { -#if DEBUG - System.Diagnostics.Debug.WriteLine($"[Toast] ShowTrackChangeToast failed: {ex}"); - MessageBox.Show($"Toast display failed: {ex.Message}", "Toast Debug", MessageBoxButtons.OK, MessageBoxIcon.Warning); -#endif - } + ToastNotificationService.ShowTrackChangeToast(safeTitle, safeArtist, attribution, outputImagePath); } }