From ff87740483f84002c7e28b4690768faedd69882c Mon Sep 17 00:00:00 2001 From: Timothy Johnson Date: Sat, 23 Jun 2018 22:19:12 -0400 Subject: [PATCH] Fixed logic for switching between day and night and ensuring only one timer runs at a time --- src/DesktopHelper.cs | 2 +- src/FormWrapper.cs | 2 +- src/Properties/AssemblyInfo.cs | 2 +- src/UwpDesktop.cs | 4 ++-- src/UwpHelper.cs | 2 +- src/WallpaperChangeScheduler.cs | 24 +++++++++++++----------- 6 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/DesktopHelper.cs b/src/DesktopHelper.cs index 1516cdf..6d5e8b7 100644 --- a/src/DesktopHelper.cs +++ b/src/DesktopHelper.cs @@ -17,7 +17,7 @@ public static string GetCurrentDirectory() } } - class DesktopStartupManager : IStartupManager + class DesktopStartupManager : StartupManager { private bool startOnBoot; private string registryStartupLocation = @"Software\Microsoft\Windows\CurrentVersion\Run"; diff --git a/src/FormWrapper.cs b/src/FormWrapper.cs index ebab0d4..f986933 100644 --- a/src/FormWrapper.cs +++ b/src/FormWrapper.cs @@ -16,7 +16,7 @@ class FormWrapper : ApplicationContext private InputDialog locationDialog; private NotifyIcon notifyIcon; - public IStartupManager _startupManager; + public StartupManager _startupManager; public WallpaperChangeScheduler _wcsService; public FormWrapper() diff --git a/src/Properties/AssemblyInfo.cs b/src/Properties/AssemblyInfo.cs index 8f32199..20d7955 100644 --- a/src/Properties/AssemblyInfo.cs +++ b/src/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.2.2")] +[assembly: AssemblyVersion("1.2.3")] //[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/src/UwpDesktop.cs b/src/UwpDesktop.cs index 62b32aa..ff4dee8 100644 --- a/src/UwpDesktop.cs +++ b/src/UwpDesktop.cs @@ -8,7 +8,7 @@ namespace WinDynamicDesktop { - abstract class IStartupManager + abstract class StartupManager { public abstract void ToggleStartOnBoot(); } @@ -34,7 +34,7 @@ public static string GetCurrentDirectory() } } - public static IStartupManager GetStartupManager(MenuItem startupMenuItem) + public static StartupManager GetStartupManager(MenuItem startupMenuItem) { if (!IsRunningAsUwp()) { diff --git a/src/UwpHelper.cs b/src/UwpHelper.cs index a4aa9f5..05679cf 100644 --- a/src/UwpHelper.cs +++ b/src/UwpHelper.cs @@ -15,7 +15,7 @@ public static string GetCurrentDirectory() } } - class UwpStartupManager : IStartupManager + class UwpStartupManager : StartupManager { private bool startOnBoot; diff --git a/src/WallpaperChangeScheduler.cs b/src/WallpaperChangeScheduler.cs index 7f6e6c3..48ee13c 100644 --- a/src/WallpaperChangeScheduler.cs +++ b/src/WallpaperChangeScheduler.cs @@ -13,8 +13,8 @@ class WallpaperChangeScheduler { private int[] dayImages; private int[] nightImages; - private bool isSunUp; + private bool isSunUp; private string lastDate = "yyyy-MM-dd"; private int lastImageId = -1; private int lastImageNumber = -1; @@ -24,12 +24,14 @@ class WallpaperChangeScheduler private WeatherData tomorrowsData; public bool enableTransitions = true; - public Timer wallpaperTimer; + public Timer wallpaperTimer = new Timer(); public WallpaperChangeScheduler() { dayImages = JsonConfig.imageSettings.dayImageList; nightImages = JsonConfig.imageSettings.nightImageList; + + wallpaperTimer.Tick += new EventHandler(OnWallpaperTimerTick); } private string GetDateString(int todayDelta = 0) @@ -94,6 +96,7 @@ public void StartScheduler(bool forceRefresh = false) } string currentDate = GetDateString(); + if (currentDate != lastDate || forceRefresh) { todaysData = GetWeatherData(currentDate); @@ -105,26 +108,23 @@ public void StartScheduler(bool forceRefresh = false) // Before sunrise yesterdaysData = GetWeatherData(GetDateString(-1)); tomorrowsData = null; - isSunUp = false; } else if (DateTime.Now > todaysData.SunsetTime) { // After sunset yesterdaysData = null; tomorrowsData = GetWeatherData(GetDateString(1)); - isSunUp = false; } else { // Between sunrise and sunset yesterdaysData = null; tomorrowsData = null; - isSunUp = true; } lastImageId = -1; - if (isSunUp) + if (yesterdaysData == null && tomorrowsData == null) { StartDaySchedule(); } @@ -136,6 +136,8 @@ public void StartScheduler(bool forceRefresh = false) private void StartDaySchedule() { + isSunUp = true; + TimeSpan dayTime = todaysData.SunsetTime - todaysData.SunriseTime; TimeSpan timerLength = new TimeSpan(dayTime.Ticks / dayImages.Length); @@ -143,9 +145,7 @@ private void StartDaySchedule() TimeSpan interval = new TimeSpan(todaysData.SunriseTime.Ticks + timerLength.Ticks * (imageNumber + 1) - DateTime.Now.Ticks); - wallpaperTimer = new Timer(); wallpaperTimer.Interval = (int)interval.TotalMilliseconds; - wallpaperTimer.Tick += new EventHandler(OnWallpaperTimerTick); wallpaperTimer.Start(); if (dayImages[imageNumber] != lastImageId) @@ -183,6 +183,8 @@ private void SwitchToNight() private void StartNightSchedule() { + isSunUp = false; + WeatherData day1Data = (yesterdaysData == null) ? todaysData : yesterdaysData; WeatherData day2Data = (yesterdaysData == null) ? tomorrowsData : todaysData; @@ -193,9 +195,7 @@ private void StartNightSchedule() TimeSpan interval = new TimeSpan(day1Data.SunsetTime.Ticks + timerLength.Ticks * (imageNumber + 1) - DateTime.Now.Ticks); - wallpaperTimer = new Timer(); wallpaperTimer.Interval = (int)interval.TotalMilliseconds; - wallpaperTimer.Tick += new EventHandler(OnWallpaperTimerTick); wallpaperTimer.Start(); if (nightImages[imageNumber] != lastImageId) @@ -225,7 +225,7 @@ private void NextNightImage() lastImageNumber++; SetWallpaper(nightImages[lastImageNumber]); - if (DateTime.Now.Hour == 0 && tomorrowsData != null) + if (DateTime.Now.Hour >= 0 && DateTime.Now.Hour < 12 && tomorrowsData != null) { yesterdaysData = todaysData; todaysData = tomorrowsData; @@ -244,6 +244,8 @@ private void SwitchToDay() private void OnWallpaperTimerTick(object sender, EventArgs e) { + wallpaperTimer.Stop(); + if (isSunUp) { NextDayImage();