Skip to content

Commit

Permalink
Code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
danielchalmers committed Jul 16, 2024
1 parent f3deb57 commit ee6f9ce
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 69 deletions.
33 changes: 0 additions & 33 deletions DesktopClock.Tests/DateTimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
3 changes: 0 additions & 3 deletions DesktopClock/DateFormatExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
22 changes: 1 addition & 21 deletions DesktopClock/DateTimeUtil.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
using System;
using System.Collections.Generic;

namespace DesktopClock;

public static class DateTimeUtil
{
/// <summary>
/// A cached collection of all the time zones about which information is available on the local system.
/// </summary>
public static IReadOnlyCollection<TimeZoneInfo> 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;
}
}

/// <summary>
/// Converts a DateTime to a DateTimeOffset, without risking any onerous exceptions
/// the framework quite unfortunately throws within the DateTimeOffset constructor,
Expand All @@ -45,4 +25,4 @@ public static DateTimeOffset ToDateTimeOffset(this DateTime dt, TimeSpan offset)

return new DateTimeOffset(dt.Ticks, offset);
}
}
}
14 changes: 12 additions & 2 deletions DesktopClock/Properties/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,18 @@ public void ScaleHeight(double steps)
/// <summary>
/// Gets the time zone selected in settings, or local by default.
/// </summary>
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()
{
Expand Down
2 changes: 1 addition & 1 deletion DesktopClock/SettingsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<ComboBox Grid.Column="1"
DisplayMemberPath="Example"
ItemsSource="{x:Static local:DateFormatExample.DefaultExamples}"
SelectionChanged="FormatComboBox_SelectionChanged" />
SelectionChanged="SelectFormat" />
</Grid>
<TextBlock Text=".NET format string for the time shown on the clock. Format specific parts inside { and }."
FontStyle="Italic"
Expand Down
31 changes: 22 additions & 9 deletions DesktopClock/SettingsWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
Expand All @@ -20,14 +19,18 @@ public SettingsWindow()
DataContext = new SettingsWindowViewModel(Settings.Default);
}

private void FormatComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
private SettingsWindowViewModel ViewModel => (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)
Expand All @@ -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)
Expand All @@ -57,7 +60,7 @@ private void BrowseWavFilePath(object sender, RoutedEventArgs e)
return;
}

((SettingsWindowViewModel)DataContext).Settings.WavFilePath = openFileDialog.FileName;
ViewModel.Settings.WavFilePath = openFileDialog.FileName;
}
}

Expand All @@ -72,14 +75,24 @@ public SettingsWindowViewModel(Settings settings)
TimeZones = TimeZoneInfo.GetSystemTimeZones().Select(tz => tz.Id).ToList();
}

/// <summary>
/// All available font families reported by the system.
/// </summary>
public IList<string> FontFamilies { get; }

/// <summary>
/// All available time zones reported by the system.
/// </summary>
public IList<string> TimeZones { get; }

/// <summary>
/// Sets the format string in settings to the given string.
/// Sets the format string in settings.
/// </summary>
[RelayCommand]
public void SetFormat(DateFormatExample formatExample) => Settings.Default.Format = formatExample.Format;
public void SetFormat(DateFormatExample value)
{
Settings.Default.Format = value.Format;
}

/// <summary>
/// Disables countdown mode by resetting the value to default.
Expand Down

0 comments on commit ee6f9ce

Please sign in to comment.