Daniel Chalmers 1 an în urmă
părinte
comite
28fbaf51b4

+ 1 - 1
.editorconfig

@@ -46,7 +46,7 @@ dotnet_style_require_accessibility_modifiers = for_non_interface_members
 dotnet_style_coalesce_expression = true
 dotnet_style_collection_initializer = true:silent
 dotnet_style_explicit_tuple_names = true:warning
-dotnet_style_namespace_match_folder = true
+dotnet_style_namespace_match_folder = false
 dotnet_style_null_propagation = true
 dotnet_style_object_initializer = true:silent
 dotnet_style_operator_placement_when_wrapping = beginning_of_line

+ 3 - 20
DesktopClock.Tests/DateTimeTests.cs

@@ -5,23 +5,6 @@ namespace DesktopClock.Tests;
 
 public class DateTimeTests
 {
-    [Theory]
-    [InlineData("2024-07-15T00:00:00Z", "00:00:00")]
-    [InlineData("2024-07-15T00:00:00Z", "01:00:00")]
-    [InlineData("0001-01-01T00:00:00Z", "00:00:00")]
-    public void ToDateTimeOffset_ShouldConvertDateTimeToExpectedOffset(string dateTimeString, string offsetString)
-    {
-        // Arrange
-        var dateTime = DateTime.Parse(dateTimeString);
-        var offset = TimeSpan.Parse(offsetString);
-
-        // Act
-        var dateTimeOffset = dateTime.ToDateTimeOffset(offset);
-
-        // Assert
-        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
@@ -30,14 +13,14 @@ public class DateTimeTests
     [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)
+    public void EqualExcludingMilliseconds(string dt1String, string dt2String, bool expected)
     {
         // Arrange
         var dt1 = DateTime.Parse(dt1String);
         var dt2 = DateTime.Parse(dt2String);
 
         // Act
-        var result = dt1.AreEqualExcludingMilliseconds(dt2);
+        var result = dt1.EqualExcludingMillisecond(dt2);
 
         // Assert
         Assert.Equal(expected, result);
@@ -59,7 +42,7 @@ public class DateTimeTests
         var interval = TimeSpan.FromSeconds(intervalSeconds);
 
         // Act
-        var result = DateTimeUtil.IsEitherNowOrCountdownOnInterval(dateTime, countdownTo, interval);
+        var result = DateTimeUtil.IsNowOrCountdownOnInterval(dateTime, countdownTo, interval);
 
         // Assert
         Assert.Equal(expected, result);

+ 2 - 1
DesktopClock/App.xaml.cs

@@ -30,11 +30,12 @@ public partial class App : Application
             return BitConverter.ToString(hash).Replace("-", string.Empty);
         }
 
+        // Use the path as the name so we can handle multiple exes, but hash it or Windows won't like it.
         var keyName = GetSha256Hash(MainFileInfo.FullName);
         using var key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
 
         if (runOnStartup)
-            key?.SetValue(keyName, MainFileInfo.FullName); // Use the path as the name so we can handle multiple exes, but hash it or Windows won't like it.
+            key?.SetValue(keyName, MainFileInfo.FullName);
         else
             key?.DeleteValue(keyName, false);
     }

+ 5 - 7
DesktopClock/Data/Theme.cs

@@ -3,9 +3,6 @@ using System.Collections.Generic;
 
 namespace DesktopClock;
 
-/// <summary>
-/// A defined color set.
-/// </summary>
 public readonly record struct Theme
 {
     /// <summary>
@@ -36,8 +33,8 @@ public readonly record struct Theme
     /// <remarks>
     /// https://www.materialui.co/colors - A100, A700.
     /// </remarks>
-    public static IReadOnlyList<Theme> DefaultThemes { get; } = new Theme[]
-    {
+    public static IReadOnlyList<Theme> DefaultThemes { get; } =
+    [
         new("Light Text", "#F5F5F5", "#212121"),
         new("Dark Text", "#212121", "#F5F5F5"),
         new("Red", "#D50000", "#FF8A80"),
@@ -47,7 +44,7 @@ public readonly record struct Theme
         new("Cyan", "#00B8D4", "#84FFFF"),
         new("Green", "#00C853", "#B9F6CA"),
         new("Orange", "#FF6D00", "#FFD180"),
-    };
+    ];
 
     /// <summary>
     /// Returns a random theme from <see cref="DefaultThemes"/>.
@@ -55,6 +52,7 @@ public readonly record struct Theme
     public static Theme GetRandomDefaultTheme()
     {
         var random = new Random();
-        return DefaultThemes[random.Next(0, DefaultThemes.Count)];
+        var index = random.Next(0, DefaultThemes.Count);
+        return DefaultThemes[index];
     }
 }

+ 4 - 4
DesktopClock/MainWindow.xaml.cs

@@ -137,7 +137,7 @@ public partial class MainWindow : Window
         Application.Current.Shutdown();
     }
 
-    private void ConfigureTrayIcon(bool showIcon, bool firstLaunch)
+    private void ConfigureTrayIcon(bool showIcon, bool isFirstLaunch)
     {
         if (showIcon)
         {
@@ -156,7 +156,7 @@ public partial class MainWindow : Window
             }
 
             // Show a notice if the icon was moved during runtime, but not at the start because the user will already expect it.
-            if (!firstLaunch)
+            if (!isFirstLaunch)
                 _trayIcon.ShowNotification("Hidden from taskbar", "Icon was moved to the tray");
         }
         else
@@ -167,7 +167,7 @@ public partial class MainWindow : Window
     }
 
     /// <summary>
-    /// Handles property changes in settings and updates the corresponding properties in the UI.
+    /// Handles setting changes.
     /// </summary>
     private void Settings_PropertyChanged(object sender, PropertyChangedEventArgs e)
     {
@@ -233,7 +233,7 @@ public partial class MainWindow : Window
         if (_soundPlayer == null)
             return;
 
-        if (!DateTimeUtil.IsEitherNowOrCountdownOnInterval(DateTime.Now, Settings.Default.CountdownTo, Settings.Default.WavFileInterval))
+        if (!DateTimeUtil.IsNowOrCountdownOnInterval(DateTime.Now, Settings.Default.CountdownTo, Settings.Default.WavFileInterval))
             return;
 
         try

+ 1 - 1
DesktopClock/SettingsWindow.xaml.cs

@@ -109,7 +109,7 @@ public partial class SettingsWindowViewModel : ObservableObject
     }
 
     /// <summary>
-    /// Disables countdown mode by resetting the value to default.
+    /// Disables countdown mode by resetting the date to default.
     /// </summary>
     [RelayCommand]
     public void ResetCountdown()

+ 3 - 25
DesktopClock/Utilities/DateTimeUtil.cs

@@ -4,29 +4,7 @@ namespace DesktopClock;
 
 public static class DateTimeUtil
 {
-    /// <summary>
-    /// Converts a DateTime to a DateTimeOffset, without risking any onerous exceptions
-    /// the framework quite unfortunately throws within the DateTimeOffset constructor,
-    /// such as they do when the source DateTime's Kind is not set to UTC. The best and
-    /// most performant way around this, which we do herein, is to simply construct the
-    /// new DateTimeOffset with the overload that excepts Ticks. Also, we will simply
-    /// return <see cref="DateTimeOffset.MinValue"/> if the source DateTime was
-    /// <see cref="DateTime.MinValue"/>.
-    /// </summary>
-    /// <remarks>https://stackoverflow.com/a/48511228</remarks>
-    /// <param name="dt">Source DateTime.</param>
-    /// <param name="offset">Offset</param>
-    public static DateTimeOffset ToDateTimeOffset(this DateTime dt, TimeSpan offset)
-    {
-        // adding negative offset to a min-datetime will throw, this is a 
-        // sufficient catch. Note however that a DateTime of just a few hours can still throw
-        if (dt == DateTime.MinValue)
-            return DateTimeOffset.MinValue;
-
-        return new DateTimeOffset(dt.Ticks, offset);
-    }
-
-    public static bool AreEqualExcludingMilliseconds(this DateTime dt1, DateTime dt2)
+    public static bool EqualExcludingMillisecond(this DateTime dt1, DateTime dt2)
     {
         if (dt1.Year != dt2.Year)
             return false;
@@ -49,13 +27,13 @@ public static class DateTimeUtil
         return true;
     }
 
-    public static bool IsEitherNowOrCountdownOnInterval(DateTime now, DateTime countdown, TimeSpan interval)
+    public static bool IsNowOrCountdownOnInterval(DateTime now, DateTime countdown, TimeSpan interval)
     {
         var time = countdown == default ? now.TimeOfDay : countdown - now;
 
         var isOnInterval = interval != default && (int)time.TotalSeconds % (int)interval.TotalSeconds == 0;
 
-        var isCountdownReached = now.AreEqualExcludingMilliseconds(countdown);
+        var isCountdownReached = now.EqualExcludingMillisecond(countdown);
 
         return isOnInterval || isCountdownReached;
     }

+ 1 - 3
DesktopClock/Utilities/LogScaleConverter.cs

@@ -5,9 +5,7 @@ using System.Windows.Markup;
 
 namespace DesktopClock;
 
-/// <summary>
-/// https://stackoverflow.com/a/13850984.
-/// </summary>
+// https://stackoverflow.com/a/13850984
 public class LogScaleConverter : MarkupExtension, IValueConverter
 {
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

+ 0 - 1
DesktopClock/Utilities/Tokenizer.cs

@@ -18,7 +18,6 @@ public static class Tokenizer
     {
         if (!string.IsNullOrWhiteSpace(format))
         {
-
             try
             {
                 if (format.Contains("}"))

+ 1 - 1
DesktopClock/Utilities/WindowUtil.cs

@@ -5,7 +5,7 @@ namespace DesktopClock;
 public static class WindowUtil
 {
     /// <summary>
-    /// Hides the window until the user opens it through the taskbar, tray, alt-tab, etc.
+    /// Hides the window until the user opens it again through the taskbar, tray, alt-tab, etc.
     /// </summary>
     public static void HideFromScreen(this Window window)
     {