Browse Source

Base countdown date off current timezone and handle empty setting

Little more user friendly than having to define the timezone twice and because it's in the user's locale.
Daniel Chalmers 1 year ago
parent
commit
baa7fc01de

+ 1 - 1
DesktopClock/MainWindow.xaml

@@ -104,7 +104,7 @@
 
 
 			<MenuItem Command="{Binding NewClockCommand}" Header="_New clock..." />
 			<MenuItem Command="{Binding NewClockCommand}" Header="_New clock..." />
 
 
-			<MenuItem Command="{Binding CountdownToCommand}" Header="_Countdown to..." />
+			<MenuItem Command="{Binding CountdownWizardCommand}" Header="_Countdown to..." />
 
 
 			<MenuItem Command="{Binding OpenSettingsCommand}" Header="Advanced _settings..." />
 			<MenuItem Command="{Binding OpenSettingsCommand}" Header="Advanced _settings..." />
 
 

+ 23 - 15
DesktopClock/MainWindow.xaml.cs

@@ -26,10 +26,10 @@ public partial class MainWindow : Window
     private TimeZoneInfo _timeZone;
     private TimeZoneInfo _timeZone;
 
 
     /// <summary>
     /// <summary>
-    /// Indicates whether the clock should be shown as a countdown.
+    /// The date and time to countdown to, or null if regular clock is desired.
     /// </summary>
     /// </summary>
     [ObservableProperty]
     [ObservableProperty]
-    private bool _isCountdown;
+    private DateTimeOffset? _countdownTo;
 
 
     /// <summary>
     /// <summary>
     /// The current date and time in the selected time zone or countdown as a formatted string.
     /// The current date and time in the selected time zone or countdown as a formatted string.
@@ -43,7 +43,7 @@ public partial class MainWindow : Window
         DataContext = this;
         DataContext = this;
 
 
         _timeZone = App.GetTimeZone();
         _timeZone = App.GetTimeZone();
-        UpdateIsCountdown();
+        UpdateCountdown();
 
 
         Settings.Default.PropertyChanged += (s, e) => Dispatcher.Invoke(() => Settings_PropertyChanged(s, e));
         Settings.Default.PropertyChanged += (s, e) => Dispatcher.Invoke(() => Settings_PropertyChanged(s, e));
 
 
@@ -102,12 +102,11 @@ public partial class MainWindow : Window
     /// Explains how to enable countdown mode, then asks user if they want to view Advanced settings to do so.
     /// Explains how to enable countdown mode, then asks user if they want to view Advanced settings to do so.
     /// </summary>
     /// </summary>
     [RelayCommand]
     [RelayCommand]
-    public void CountdownTo()
+    public void CountdownWizard()
     {
     {
         var result = MessageBox.Show(this,
         var result = MessageBox.Show(this,
-            $"In advanced settings: change {nameof(Settings.Default.CountdownTo)}, then save.\n" +
-            "Go back by replacing it with \"0001-01-01T00:00:00+00:00\".\n\n" +
-            "Open advanced settings now?",
+            $"In advanced settings: change \"{nameof(Settings.Default.CountdownTo)}\" in the format of \"{default(DateTime)}\", then save." +
+            "\n\nOpen advanced settings now?",
             Title, MessageBoxButton.OKCancel, MessageBoxImage.Question, MessageBoxResult.OK);
             Title, MessageBoxButton.OKCancel, MessageBoxImage.Question, MessageBoxResult.OK);
 
 
         if (result != MessageBoxResult.OK)
         if (result != MessageBoxResult.OK)
@@ -202,7 +201,7 @@ public partial class MainWindow : Window
                 break;
                 break;
 
 
             case nameof(Settings.Default.CountdownTo):
             case nameof(Settings.Default.CountdownTo):
-                UpdateIsCountdown();
+                UpdateCountdown();
                 break;
                 break;
         }
         }
     }
     }
@@ -212,7 +211,16 @@ public partial class MainWindow : Window
         UpdateTimeString();
         UpdateTimeString();
     }
     }
 
 
-    private void UpdateIsCountdown() => IsCountdown = Settings.Default.CountdownTo > DateTimeOffset.MinValue;
+    private void UpdateCountdown()
+    {
+        if (Settings.Default.CountdownTo == null || Settings.Default.CountdownTo == default(DateTime))
+        {
+            CountdownTo = null;
+            return;
+        }
+
+        CountdownTo = new DateTimeOffset(Settings.Default.CountdownTo.Value, _timeZone.BaseUtcOffset);
+    }
 
 
     private void UpdateTimeString()
     private void UpdateTimeString()
     {
     {
@@ -220,16 +228,16 @@ public partial class MainWindow : Window
         {
         {
             var timeInSelectedZone = TimeZoneInfo.ConvertTime(DateTimeOffset.Now, _timeZone);
             var timeInSelectedZone = TimeZoneInfo.ConvertTime(DateTimeOffset.Now, _timeZone);
 
 
-            if (IsCountdown)
+            if (CountdownTo == null)
             {
             {
-                if (string.IsNullOrWhiteSpace(Settings.Default.CountdownFormat))
-                    return Settings.Default.CountdownTo.Humanize(timeInSelectedZone);
-
-                return Tokenizer.FormatWithTokenizerOrFallBack(Settings.Default.CountdownTo - timeInSelectedZone, Settings.Default.CountdownFormat, CultureInfo.DefaultThreadCurrentCulture);
+                return Tokenizer.FormatWithTokenizerOrFallBack(timeInSelectedZone, Settings.Default.Format, CultureInfo.DefaultThreadCurrentCulture);
             }
             }
             else
             else
             {
             {
-                return Tokenizer.FormatWithTokenizerOrFallBack(timeInSelectedZone, Settings.Default.Format, CultureInfo.DefaultThreadCurrentCulture);
+                if (string.IsNullOrWhiteSpace(Settings.Default.CountdownFormat))
+                    return CountdownTo.Humanize(timeInSelectedZone);
+
+                return Tokenizer.FormatWithTokenizerOrFallBack(Settings.Default.CountdownTo - timeInSelectedZone, Settings.Default.CountdownFormat, CultureInfo.DefaultThreadCurrentCulture);
             }
             }
         }
         }
 
 

+ 1 - 1
DesktopClock/Properties/Settings.cs

@@ -53,7 +53,7 @@ public sealed class Settings : INotifyPropertyChanged, IDisposable
 
 
     public string Format { get; set; } = "{dddd}, {MMM dd}, {HH:mm:ss}";
     public string Format { get; set; } = "{dddd}, {MMM dd}, {HH:mm:ss}";
     public string CountdownFormat { get; set; } = "";
     public string CountdownFormat { get; set; } = "";
-    public DateTimeOffset CountdownTo { get; set; } = DateTimeOffset.MinValue;
+    public DateTime? CountdownTo { get; set; } = default(DateTime);
     public string TimeZone { get; set; } = string.Empty;
     public string TimeZone { get; set; } = string.Empty;
     public string FontFamily { get; set; } = "Consolas";
     public string FontFamily { get; set; } = "Consolas";
     public Color TextColor { get; set; }
     public Color TextColor { get; set; }