浏览代码

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 年之前
父节点
当前提交
baa7fc01de
共有 3 个文件被更改,包括 25 次插入17 次删除
  1. 1 1
      DesktopClock/MainWindow.xaml
  2. 23 15
      DesktopClock/MainWindow.xaml.cs
  3. 1 1
      DesktopClock/Properties/Settings.cs

+ 1 - 1
DesktopClock/MainWindow.xaml

@@ -104,7 +104,7 @@
 
 			<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..." />
 

+ 23 - 15
DesktopClock/MainWindow.xaml.cs

@@ -26,10 +26,10 @@ public partial class MainWindow : Window
     private TimeZoneInfo _timeZone;
 
     /// <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>
     [ObservableProperty]
-    private bool _isCountdown;
+    private DateTimeOffset? _countdownTo;
 
     /// <summary>
     /// 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;
 
         _timeZone = App.GetTimeZone();
-        UpdateIsCountdown();
+        UpdateCountdown();
 
         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.
     /// </summary>
     [RelayCommand]
-    public void CountdownTo()
+    public void CountdownWizard()
     {
         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);
 
         if (result != MessageBoxResult.OK)
@@ -202,7 +201,7 @@ public partial class MainWindow : Window
                 break;
 
             case nameof(Settings.Default.CountdownTo):
-                UpdateIsCountdown();
+                UpdateCountdown();
                 break;
         }
     }
@@ -212,7 +211,16 @@ public partial class MainWindow : Window
         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()
     {
@@ -220,16 +228,16 @@ public partial class MainWindow : Window
         {
             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
             {
-                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 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 FontFamily { get; set; } = "Consolas";
     public Color TextColor { get; set; }