Skip to content

Commit ff87740

Browse files
committed
Fixed logic for switching between day and night and ensuring only one timer runs at a time
1 parent 613d1a0 commit ff87740

File tree

6 files changed

+19
-17
lines changed

6 files changed

+19
-17
lines changed

src/DesktopHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static string GetCurrentDirectory()
1717
}
1818
}
1919

20-
class DesktopStartupManager : IStartupManager
20+
class DesktopStartupManager : StartupManager
2121
{
2222
private bool startOnBoot;
2323
private string registryStartupLocation = @"Software\Microsoft\Windows\CurrentVersion\Run";

src/FormWrapper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class FormWrapper : ApplicationContext
1616
private InputDialog locationDialog;
1717
private NotifyIcon notifyIcon;
1818

19-
public IStartupManager _startupManager;
19+
public StartupManager _startupManager;
2020
public WallpaperChangeScheduler _wcsService;
2121

2222
public FormWrapper()

src/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.2.2")]
35+
[assembly: AssemblyVersion("1.2.3")]
3636
//[assembly: AssemblyFileVersion("1.0.0.0")]

src/UwpDesktop.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace WinDynamicDesktop
1010
{
11-
abstract class IStartupManager
11+
abstract class StartupManager
1212
{
1313
public abstract void ToggleStartOnBoot();
1414
}
@@ -34,7 +34,7 @@ public static string GetCurrentDirectory()
3434
}
3535
}
3636

37-
public static IStartupManager GetStartupManager(MenuItem startupMenuItem)
37+
public static StartupManager GetStartupManager(MenuItem startupMenuItem)
3838
{
3939
if (!IsRunningAsUwp())
4040
{

src/UwpHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static string GetCurrentDirectory()
1515
}
1616
}
1717

18-
class UwpStartupManager : IStartupManager
18+
class UwpStartupManager : StartupManager
1919
{
2020
private bool startOnBoot;
2121

src/WallpaperChangeScheduler.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class WallpaperChangeScheduler
1313
{
1414
private int[] dayImages;
1515
private int[] nightImages;
16-
private bool isSunUp;
1716

17+
private bool isSunUp;
1818
private string lastDate = "yyyy-MM-dd";
1919
private int lastImageId = -1;
2020
private int lastImageNumber = -1;
@@ -24,12 +24,14 @@ class WallpaperChangeScheduler
2424
private WeatherData tomorrowsData;
2525

2626
public bool enableTransitions = true;
27-
public Timer wallpaperTimer;
27+
public Timer wallpaperTimer = new Timer();
2828

2929
public WallpaperChangeScheduler()
3030
{
3131
dayImages = JsonConfig.imageSettings.dayImageList;
3232
nightImages = JsonConfig.imageSettings.nightImageList;
33+
34+
wallpaperTimer.Tick += new EventHandler(OnWallpaperTimerTick);
3335
}
3436

3537
private string GetDateString(int todayDelta = 0)
@@ -94,6 +96,7 @@ public void StartScheduler(bool forceRefresh = false)
9496
}
9597

9698
string currentDate = GetDateString();
99+
97100
if (currentDate != lastDate || forceRefresh)
98101
{
99102
todaysData = GetWeatherData(currentDate);
@@ -105,26 +108,23 @@ public void StartScheduler(bool forceRefresh = false)
105108
// Before sunrise
106109
yesterdaysData = GetWeatherData(GetDateString(-1));
107110
tomorrowsData = null;
108-
isSunUp = false;
109111
}
110112
else if (DateTime.Now > todaysData.SunsetTime)
111113
{
112114
// After sunset
113115
yesterdaysData = null;
114116
tomorrowsData = GetWeatherData(GetDateString(1));
115-
isSunUp = false;
116117
}
117118
else
118119
{
119120
// Between sunrise and sunset
120121
yesterdaysData = null;
121122
tomorrowsData = null;
122-
isSunUp = true;
123123
}
124124

125125
lastImageId = -1;
126126

127-
if (isSunUp)
127+
if (yesterdaysData == null && tomorrowsData == null)
128128
{
129129
StartDaySchedule();
130130
}
@@ -136,16 +136,16 @@ public void StartScheduler(bool forceRefresh = false)
136136

137137
private void StartDaySchedule()
138138
{
139+
isSunUp = true;
140+
139141
TimeSpan dayTime = todaysData.SunsetTime - todaysData.SunriseTime;
140142
TimeSpan timerLength = new TimeSpan(dayTime.Ticks / dayImages.Length);
141143

142144
int imageNumber = GetImageNumber(todaysData.SunriseTime, timerLength, dayImages.Length);
143145
TimeSpan interval = new TimeSpan(todaysData.SunriseTime.Ticks + timerLength.Ticks *
144146
(imageNumber + 1) - DateTime.Now.Ticks);
145147

146-
wallpaperTimer = new Timer();
147148
wallpaperTimer.Interval = (int)interval.TotalMilliseconds;
148-
wallpaperTimer.Tick += new EventHandler(OnWallpaperTimerTick);
149149
wallpaperTimer.Start();
150150

151151
if (dayImages[imageNumber] != lastImageId)
@@ -183,6 +183,8 @@ private void SwitchToNight()
183183

184184
private void StartNightSchedule()
185185
{
186+
isSunUp = false;
187+
186188
WeatherData day1Data = (yesterdaysData == null) ? todaysData : yesterdaysData;
187189
WeatherData day2Data = (yesterdaysData == null) ? tomorrowsData : todaysData;
188190

@@ -193,9 +195,7 @@ private void StartNightSchedule()
193195
TimeSpan interval = new TimeSpan(day1Data.SunsetTime.Ticks + timerLength.Ticks *
194196
(imageNumber + 1) - DateTime.Now.Ticks);
195197

196-
wallpaperTimer = new Timer();
197198
wallpaperTimer.Interval = (int)interval.TotalMilliseconds;
198-
wallpaperTimer.Tick += new EventHandler(OnWallpaperTimerTick);
199199
wallpaperTimer.Start();
200200

201201
if (nightImages[imageNumber] != lastImageId)
@@ -225,7 +225,7 @@ private void NextNightImage()
225225
lastImageNumber++;
226226
SetWallpaper(nightImages[lastImageNumber]);
227227

228-
if (DateTime.Now.Hour == 0 && tomorrowsData != null)
228+
if (DateTime.Now.Hour >= 0 && DateTime.Now.Hour < 12 && tomorrowsData != null)
229229
{
230230
yesterdaysData = todaysData;
231231
todaysData = tomorrowsData;
@@ -244,6 +244,8 @@ private void SwitchToDay()
244244

245245
private void OnWallpaperTimerTick(object sender, EventArgs e)
246246
{
247+
wallpaperTimer.Stop();
248+
247249
if (isSunUp)
248250
{
249251
NextDayImage();

0 commit comments

Comments
 (0)