Răsfoiți Sursa

Code cleanup

Daniel Chalmers 3 ani în urmă
părinte
comite
6b6f4af648

+ 2 - 4
DesktopClock/App.xaml.cs

@@ -1,5 +1,4 @@
 using System.Collections.Generic;
-using System.Reflection;
 using System.Windows;
 using DesktopClock.Properties;
 
@@ -11,7 +10,7 @@ namespace DesktopClock;
 public partial class App : Application
 {
     // https://www.materialui.co/colors
-    public static IReadOnlyList<Theme> Themes { get; } = new Theme[]
+    public static IReadOnlyList<Theme> Themes { get; } = new[]
     {
         new Theme("White", "#FFFFFF", "#000000"),
         new Theme("Black", "#000000", "#9E9E9E"),
@@ -36,7 +35,6 @@ public partial class App : Application
 
     private void Application_Exit(object sender, ExitEventArgs e)
     {
-        if (!Settings.Default.CheckIfModifiedExternally())
-            Settings.Default.Save();
+        Settings.Default.SaveIfNotModifiedExternally();
     }
 }

+ 2 - 3
DesktopClock/MainViewModel.cs

@@ -15,13 +15,12 @@ public class MainViewModel : ViewModelBase
 
     public MainViewModel()
     {
-        _systemClockTimer = new SystemClockTimer();
-        _systemClockTimer.SecondChanged += SystemClockTimer_SecondChanged;
-
         _timeZone = SettingsHelper.GetTimeZone();
 
         Settings.Default.PropertyChanged += Settings_PropertyChanged;
 
+        _systemClockTimer = new();
+        _systemClockTimer.SecondChanged += SystemClockTimer_SecondChanged;
         _systemClockTimer.Start();
     }
 

+ 8 - 8
DesktopClock/MainWindow.xaml

@@ -10,16 +10,16 @@
         Title="DesktopClock"
         AllowsTransparency="True"
         Background="Transparent"
-        FontFamily="{Binding FontFamily, Source={x:Static p:Settings.Default}, Mode=OneWay}"
-        MouseDown="Window_MouseDown"
-        MouseDoubleClick="Window_MouseDoubleClick"
-		MouseWheel="Window_MouseWheel"
+        WindowStyle="None"
         ResizeMode="NoResize"
-        ShowInTaskbar="{Binding ShowInTaskbar, Source={x:Static p:Settings.Default}, Mode=TwoWay}"
         SizeToContent="WidthAndHeight"
-        Topmost="{Binding Topmost, Source={x:Static p:Settings.Default}, Mode=TwoWay}"
         UseLayoutRounding="True"
-        WindowStyle="None"
+        Topmost="{Binding Topmost, Source={x:Static p:Settings.Default}, Mode=TwoWay}"
+        ShowInTaskbar="{Binding ShowInTaskbar, Source={x:Static p:Settings.Default}, Mode=TwoWay}"
+        FontFamily="{Binding FontFamily, Source={x:Static p:Settings.Default}, Mode=OneWay}"
+        MouseDown="Window_MouseDown"
+        MouseDoubleClick="Window_MouseDoubleClick"
+        MouseWheel="Window_MouseWheel"
         wp:WindowPlacementProperties.Placement="{Binding Placement, Source={x:Static p:Settings.Default}}">
 	<Window.DataContext>
 		<local:MainViewModel />
@@ -96,7 +96,7 @@
 			<MenuItem Click="MenuItemCountdown_OnClick" Header="_Countdown to..." />
 
 			<Separator />
-			
+
 			<MenuItem Click="MenuItemSettings_OnClick" Header="_Settings..." />
 
 			<MenuItem Click="MenuItemCheckForUpdates_OnClick" Header="Check for _updates..." />

+ 1 - 1
DesktopClock/MainWindow.xaml.cs

@@ -52,7 +52,7 @@ public partial class MainWindow : Window
 
     private void MenuItemCountdown_OnClick(object sender, RoutedEventArgs e)
     {
-        MessageBox.Show(this, 
+        MessageBox.Show(this,
             $"Go to settings, change the {nameof(Settings.Default.CountdownTo)} option, and restart.\n\n" +
             $"Get the clock back by deleting everything between the quotes.");
     }

+ 12 - 3
DesktopClock/Properties/Settings.cs

@@ -61,6 +61,12 @@ public sealed class Settings : INotifyPropertyChanged
 
     #endregion "Properties"
 
+    /// <summary>
+    /// Determines if the settings file has been modified externally since the last time it was used.
+    /// </summary>
+    public bool CheckIfModifiedExternally() =>
+        File.GetLastWriteTimeUtc(Path) > _fileLastUsed;
+
     /// <summary>
     /// Saves to the default path.
     /// </summary>
@@ -75,10 +81,13 @@ public sealed class Settings : INotifyPropertyChanged
     }
 
     /// <summary>
-    /// Determines if the settings file has been modified externally since the last time it was used.
+    /// Saves to the default path unless a save has already happened from an external source.
     /// </summary>
-    public bool CheckIfModifiedExternally() =>
-        File.GetLastWriteTimeUtc(Path) > _fileLastUsed;
+    public void SaveIfNotModifiedExternally()
+    {
+        if (!CheckIfModifiedExternally())
+            Save();
+    }
 
     /// <summary>
     /// Loads from the default path.

+ 2 - 8
DesktopClock/SystemClockTimer.cs

@@ -4,7 +4,7 @@ using System.Threading;
 namespace DesktopClock;
 
 /// <summary>
-/// A timer, synced with the system clock.
+/// A timer that is synced with the system clock.
 /// </summary>
 public sealed class SystemClockTimer : IDisposable
 {
@@ -25,13 +25,7 @@ public sealed class SystemClockTimer : IDisposable
     /// </summary>
     private int MillisecondsUntilNextSecond => 1000 - DateTimeOffset.Now.Millisecond;
 
-    /// <summary>
-    /// Releases all resources used by the current instance of <see cref="SystemClockTimer" />.
-    /// </summary>
-    public void Dispose()
-    {
-        _timer.Dispose();
-    }
+    public void Dispose() => _timer.Dispose();
 
     public void Start() => ScheduleTickForNextSecond();