浏览代码

Add setting to play wav file when countdown elapses

Enabled by default
Daniel Chalmers 1 年之前
父节点
当前提交
14125234d2

+ 21 - 0
DesktopClock.Tests/DateTimeTests.cs

@@ -21,6 +21,27 @@ public class DateTimeTests
         Assert.Equal(new DateTimeOffset(dateTime.Ticks, offset), dateTimeOffset);
         Assert.Equal(new DateTimeOffset(dateTime.Ticks, offset), dateTimeOffset);
     }
     }
 
 
+    [Theory]
+    [InlineData("2024-07-18T12:30:45.123Z", "2024-07-18T12:30:45.456Z", true)] // Different millisecond
+    [InlineData("2024-07-18T12:30:45.123Z", "2024-07-18T12:30:46.123Z", false)] // Different second
+    [InlineData("2024-07-18T12:30:45.123Z", "2024-07-18T12:31:45.123Z", false)] // Different minute
+    [InlineData("2024-07-18T12:30:45.123Z", "2024-07-18T13:30:45.123Z", false)] // Different hour
+    [InlineData("2024-07-18T12:30:45.123Z", "2024-07-19T12:30:45.123Z", false)] // Different day
+    [InlineData("2024-07-18T12:30:45.123Z", "2024-08-18T12:30:45.123Z", false)] // Different month
+    [InlineData("2024-07-18T12:30:45.123Z", "2025-07-18T12:30:45.123Z", false)] // Different year
+    public void AreEqualExcludingMilliseconds(string dt1String, string dt2String, bool expected)
+    {
+        // Arrange
+        var dt1 = DateTimeOffset.Parse(dt1String);
+        var dt2 = DateTimeOffset.Parse(dt2String);
+
+        // Act
+        var result = dt1.AreEqualExcludingMilliseconds(dt2);
+
+        // Assert
+        Assert.Equal(expected, result);
+    }
+
     [Theory]
     [Theory]
     [InlineData("dddd, MMMM dd", "Monday, January 01")]
     [InlineData("dddd, MMMM dd", "Monday, January 01")]
     [InlineData("yyyy-MM-dd", "2024-01-01")]
     [InlineData("yyyy-MM-dd", "2024-01-01")]

+ 3 - 1
DesktopClock/MainWindow.xaml.cs

@@ -259,7 +259,9 @@ public partial class MainWindow : Window
             (int)DateTimeOffset.Now.TimeOfDay.TotalSeconds % (int)Settings.Default.WavFileInterval.TotalSeconds == 0 :
             (int)DateTimeOffset.Now.TimeOfDay.TotalSeconds % (int)Settings.Default.WavFileInterval.TotalSeconds == 0 :
             (int)(CountdownTo.Value - DateTimeOffset.Now).TotalSeconds % (int)Settings.Default.WavFileInterval.TotalSeconds == 0;
             (int)(CountdownTo.Value - DateTimeOffset.Now).TotalSeconds % (int)Settings.Default.WavFileInterval.TotalSeconds == 0;
 
 
-        if (!isOnInterval)
+        var isCountdownReached = DateTimeOffset.Now.AreEqualExcludingMilliseconds(Settings.Default.CountdownTo);
+
+        if (!isOnInterval && !isCountdownReached)
             return;
             return;
 
 
         try
         try

+ 5 - 0
DesktopClock/Properties/Settings.cs

@@ -200,6 +200,11 @@ public sealed class Settings : INotifyPropertyChanged, IDisposable
     /// </summary>
     /// </summary>
     public TimeSpan WavFileInterval { get; set; }
     public TimeSpan WavFileInterval { get; set; }
 
 
+    /// <summary>
+    /// Play the WAV file when the countdown time elapses.
+    /// </summary>
+    public bool PlaySoundOnCountdown { get; set; } = true;
+
     /// <summary>
     /// <summary>
     /// The index of the selected tab in the settings window.
     /// The index of the selected tab in the settings window.
     /// </summary>
     /// </summary>

+ 7 - 1
DesktopClock/SettingsWindow.xaml

@@ -95,7 +95,7 @@
                            FontStyle="Italic"
                            FontStyle="Italic"
                            FontSize="10"
                            FontSize="10"
                            Margin="0,0,0,12" />
                            Margin="0,0,0,12" />
-                
+
                 <TextBlock Text="Font Style:" />
                 <TextBlock Text="Font Style:" />
                 <ComboBox ItemsSource="{Binding FontStyles}" SelectedItem="{Binding Settings.FontStyle, Mode=TwoWay}" />
                 <ComboBox ItemsSource="{Binding FontStyles}" SelectedItem="{Binding Settings.FontStyle, Mode=TwoWay}" />
                 <TextBlock Text="Style of font to use for the clock's text."
                 <TextBlock Text="Style of font to use for the clock's text."
@@ -232,6 +232,12 @@
                            FontStyle="Italic"
                            FontStyle="Italic"
                            FontSize="10"
                            FontSize="10"
                            Margin="0,0,0,12" />
                            Margin="0,0,0,12" />
+
+                <CheckBox Content="Play WAV File on Countdown" IsChecked="{Binding Settings.PlaySoundOnCountdown, Mode=TwoWay}" />
+                <TextBlock Text="Play the WAV file when the countdown time elapses."
+                           FontStyle="Italic"
+                           FontSize="10"
+                           Margin="0,0,0,12" />
             </StackPanel>
             </StackPanel>
         </TabItem>
         </TabItem>
 
 

+ 23 - 0
DesktopClock/Utilities/DateTimeUtil.cs

@@ -25,4 +25,27 @@ public static class DateTimeUtil
 
 
         return new DateTimeOffset(dt.Ticks, offset);
         return new DateTimeOffset(dt.Ticks, offset);
     }
     }
+
+    public static bool AreEqualExcludingMilliseconds(this DateTimeOffset dt1, DateTimeOffset dt2)
+    {
+        if (dt1.Year != dt2.Year)
+            return false;
+
+        if (dt1.Month != dt2.Month)
+            return false;
+
+        if (dt1.Day != dt2.Day)
+            return false;
+
+        if (dt1.Hour != dt2.Hour)
+            return false;
+
+        if (dt1.Minute != dt2.Minute)
+            return false;
+
+        if (dt1.Second != dt2.Second)
+            return false;
+
+        return true;
+    }
 }
 }