Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
danielchalmers committed Jul 21, 2024
1 parent 23f0326 commit 28fbaf5
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 3 additions & 20 deletions DesktopClock.Tests/DateTimeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,14 +13,14 @@ public void ToDateTimeOffset_ShouldConvertDateTimeToExpectedOffset(string dateTi
[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);
Expand All @@ -59,7 +42,7 @@ public void IsOnInterval(string nowString, string countdownString, int intervalS
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);
Expand Down
3 changes: 2 additions & 1 deletion DesktopClock/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ static string GetSha256Hash(string text)
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);
}
Expand Down
12 changes: 5 additions & 7 deletions DesktopClock/Data/Theme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

namespace DesktopClock;

/// <summary>
/// A defined color set.
/// </summary>
public readonly record struct Theme
{
/// <summary>
Expand Down Expand Up @@ -36,8 +33,8 @@ public Theme(string name, string primaryColor, string secondaryColor)
/// <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"),
Expand All @@ -47,14 +44,15 @@ public Theme(string name, string primaryColor, string secondaryColor)
new("Cyan", "#00B8D4", "#84FFFF"),
new("Green", "#00C853", "#B9F6CA"),
new("Orange", "#FF6D00", "#FFD180"),
};
];

/// <summary>
/// Returns a random theme from <see cref="DefaultThemes"/>.
/// </summary>
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];
}
}
8 changes: 4 additions & 4 deletions DesktopClock/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void Exit()
Application.Current.Shutdown();
}

private void ConfigureTrayIcon(bool showIcon, bool firstLaunch)
private void ConfigureTrayIcon(bool showIcon, bool isFirstLaunch)
{
if (showIcon)
{
Expand All @@ -156,7 +156,7 @@ private void ConfigureTrayIcon(bool showIcon, bool firstLaunch)
}

// 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
Expand All @@ -167,7 +167,7 @@ private void ConfigureTrayIcon(bool showIcon, bool firstLaunch)
}

/// <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)
{
Expand Down Expand Up @@ -233,7 +233,7 @@ private void TryPlaySound()
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
Expand Down
2 changes: 1 addition & 1 deletion DesktopClock/SettingsWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void SetFormat(DateFormatExample value)
}

/// <summary>
/// Disables countdown mode by resetting the value to default.
/// Disables countdown mode by resetting the date to default.
/// </summary>
[RelayCommand]
public void ResetCountdown()
Expand Down
28 changes: 3 additions & 25 deletions DesktopClock/Utilities/DateTimeUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -49,13 +27,13 @@ public static bool AreEqualExcludingMilliseconds(this DateTime dt1, DateTime dt2
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;
}
Expand Down
4 changes: 1 addition & 3 deletions DesktopClock/Utilities/LogScaleConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

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)
Expand Down
1 change: 0 additions & 1 deletion DesktopClock/Utilities/Tokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public static string FormatWithTokenizerOrFallBack(IFormattable formattable, str
{
if (!string.IsNullOrWhiteSpace(format))
{

try
{
if (format.Contains("}"))
Expand Down
2 changes: 1 addition & 1 deletion DesktopClock/Utilities/WindowUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 28fbaf5

Please sign in to comment.