From ee6f9cecb37f287e5c10a7d3e371949e82dc430c Mon Sep 17 00:00:00 2001 From: Daniel Chalmers Date: Tue, 16 Jul 2024 16:20:59 -0500 Subject: [PATCH] Code quality --- DesktopClock.Tests/DateTimeTests.cs | 33 ----------------------------- DesktopClock/DateFormatExample.cs | 3 --- DesktopClock/DateTimeUtil.cs | 22 +------------------ DesktopClock/Properties/Settings.cs | 14 ++++++++++-- DesktopClock/SettingsWindow.xaml | 2 +- DesktopClock/SettingsWindow.xaml.cs | 31 +++++++++++++++++++-------- 6 files changed, 36 insertions(+), 69 deletions(-) diff --git a/DesktopClock.Tests/DateTimeTests.cs b/DesktopClock.Tests/DateTimeTests.cs index 16862d7..bb5cc87 100644 --- a/DesktopClock.Tests/DateTimeTests.cs +++ b/DesktopClock.Tests/DateTimeTests.cs @@ -4,39 +4,6 @@ namespace DesktopClock.Tests; public class DateTimeTests { - [Fact] - public void TimeZones_ShouldContainSystemTimeZones() - { - // Act - var timeZones = DateTimeUtil.TimeZones; - - // Assert - Assert.NotEmpty(timeZones); - Assert.Contains(timeZones, tz => tz.Id == "UTC"); - } - - [Theory] - [InlineData("UTC", true)] - [InlineData("Pacific Standard Time", true)] - [InlineData("NonExistentTimeZone", false)] - public void TryFindSystemTimeZoneById_ShouldReturnExpectedResult(string timeZoneId, bool expectedResult) - { - // Act - var result = DateTimeUtil.TryFindSystemTimeZoneById(timeZoneId, out var timeZoneInfo); - - // Assert - Assert.Equal(expectedResult, result); - if (expectedResult) - { - Assert.NotNull(timeZoneInfo); - Assert.Equal(timeZoneId, timeZoneInfo.Id); - } - else - { - Assert.Null(timeZoneInfo); - } - } - [Theory] [InlineData("2024-07-15T00:00:00Z", "00:00:00")] [InlineData("2024-07-15T00:00:00Z", "01:00:00")] diff --git a/DesktopClock/DateFormatExample.cs b/DesktopClock/DateFormatExample.cs index 45ec038..d6b9416 100644 --- a/DesktopClock/DateFormatExample.cs +++ b/DesktopClock/DateFormatExample.cs @@ -2,9 +2,6 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; -using System.Windows.Data; -using System.Windows.Markup; -using DesktopClock.Properties; namespace DesktopClock; diff --git a/DesktopClock/DateTimeUtil.cs b/DesktopClock/DateTimeUtil.cs index 49724b0..38d45a0 100644 --- a/DesktopClock/DateTimeUtil.cs +++ b/DesktopClock/DateTimeUtil.cs @@ -1,29 +1,9 @@ using System; -using System.Collections.Generic; namespace DesktopClock; public static class DateTimeUtil { - /// - /// A cached collection of all the time zones about which information is available on the local system. - /// - public static IReadOnlyCollection TimeZones { get; } = TimeZoneInfo.GetSystemTimeZones(); - - public static bool TryFindSystemTimeZoneById(string timeZoneId, out TimeZoneInfo timeZoneInfo) - { - try - { - timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId); - return true; - } - catch (TimeZoneNotFoundException) - { - timeZoneInfo = null; - return false; - } - } - /// /// Converts a DateTime to a DateTimeOffset, without risking any onerous exceptions /// the framework quite unfortunately throws within the DateTimeOffset constructor, @@ -45,4 +25,4 @@ public static DateTimeOffset ToDateTimeOffset(this DateTime dt, TimeSpan offset) return new DateTimeOffset(dt.Ticks, offset); } -} \ No newline at end of file +} diff --git a/DesktopClock/Properties/Settings.cs b/DesktopClock/Properties/Settings.cs index 8ae0248..0b4f5e4 100644 --- a/DesktopClock/Properties/Settings.cs +++ b/DesktopClock/Properties/Settings.cs @@ -328,8 +328,18 @@ public void ScaleHeight(double steps) /// /// Gets the time zone selected in settings, or local by default. /// - public TimeZoneInfo GetTimeZoneInfo() => - DateTimeUtil.TryFindSystemTimeZoneById(TimeZone, out var timeZoneInfo) ? timeZoneInfo : TimeZoneInfo.Local; + public TimeZoneInfo GetTimeZoneInfo() + { + + try + { + return TimeZoneInfo.FindSystemTimeZoneById(TimeZone); + } + catch (TimeZoneNotFoundException) + { + return TimeZoneInfo.Local; + } + } public void Dispose() { diff --git a/DesktopClock/SettingsWindow.xaml b/DesktopClock/SettingsWindow.xaml index afb662a..5eae2e6 100644 --- a/DesktopClock/SettingsWindow.xaml +++ b/DesktopClock/SettingsWindow.xaml @@ -26,7 +26,7 @@ + SelectionChanged="SelectFormat" /> (SettingsWindowViewModel)DataContext; + + private void SelectFormat(object sender, SelectionChangedEventArgs e) { - var formatExample = e.AddedItems[0] as DateFormatExample; + var value = e.AddedItems[0] as DateFormatExample; - if (formatExample == null) + if (value == null) + { return; + } - ((SettingsWindowViewModel)DataContext).Settings.Format = formatExample.Format; + ViewModel.Settings.Format = value.Format; } private void BrowseBackgroundImagePath(object sender, RoutedEventArgs e) @@ -42,7 +45,7 @@ private void BrowseBackgroundImagePath(object sender, RoutedEventArgs e) return; } - ((SettingsWindowViewModel)DataContext).Settings.BackgroundImagePath = openFileDialog.FileName; + ViewModel.Settings.BackgroundImagePath = openFileDialog.FileName; } private void BrowseWavFilePath(object sender, RoutedEventArgs e) @@ -57,7 +60,7 @@ private void BrowseWavFilePath(object sender, RoutedEventArgs e) return; } - ((SettingsWindowViewModel)DataContext).Settings.WavFilePath = openFileDialog.FileName; + ViewModel.Settings.WavFilePath = openFileDialog.FileName; } } @@ -72,14 +75,24 @@ public SettingsWindowViewModel(Settings settings) TimeZones = TimeZoneInfo.GetSystemTimeZones().Select(tz => tz.Id).ToList(); } + /// + /// All available font families reported by the system. + /// public IList FontFamilies { get; } + + /// + /// All available time zones reported by the system. + /// public IList TimeZones { get; } /// - /// Sets the format string in settings to the given string. + /// Sets the format string in settings. /// [RelayCommand] - public void SetFormat(DateFormatExample formatExample) => Settings.Default.Format = formatExample.Format; + public void SetFormat(DateFormatExample value) + { + Settings.Default.Format = value.Format; + } /// /// Disables countdown mode by resetting the value to default.