Skip to content

Commit 675a278

Browse files
committed
Greatly simplify and hopefully improve WallpaperChangeScheduler code
1 parent 3c797bd commit 675a278

File tree

3 files changed

+25
-118
lines changed

3 files changed

+25
-118
lines changed

src/FormWrapper.cs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Net;
88
using System.Threading;
99
using System.Windows.Forms;
10-
using Microsoft.Win32;
1110

1211
namespace WinDynamicDesktop
1312
{
@@ -25,8 +24,7 @@ public FormWrapper()
2524
{
2625
Application.ApplicationExit += new EventHandler(OnApplicationExit);
2726
EnforceSingleInstance();
28-
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(OnPowerModeChanged);
29-
27+
3028
JsonConfig.LoadConfig();
3129
_wcsService = new WallpaperChangeScheduler();
3230

@@ -43,7 +41,7 @@ public FormWrapper()
4341
}
4442
else
4543
{
46-
_wcsService.StartScheduler();
44+
_wcsService.RunScheduler();
4745
}
4846
}
4947

@@ -97,7 +95,7 @@ private void OnLocationItemClick(object sender, EventArgs e)
9795

9896
private void OnRefreshItemClick(object sender, EventArgs e)
9997
{
100-
_wcsService.StartScheduler(true);
98+
_wcsService.RunScheduler(true);
10199
}
102100

103101
private void OnDarkItemClick(object sender, EventArgs e)
@@ -186,7 +184,7 @@ private void OnDownloadDialogClosed(object sender, EventArgs e)
186184
{
187185
if (JsonConfig.settings.location != null)
188186
{
189-
_wcsService.StartScheduler(true);
187+
_wcsService.RunScheduler();
190188
}
191189

192190
if (JsonConfig.firstRun && locationDialog == null)
@@ -237,28 +235,6 @@ private void BackgroundNotify()
237235
JsonConfig.firstRun = false; // Don't show this message again
238236
}
239237

240-
private void OnPowerModeChanged(object sender, PowerModeChangedEventArgs e)
241-
{
242-
if (e.Mode == PowerModes.Suspend)
243-
{
244-
if (_wcsService.wallpaperTimer != null)
245-
{
246-
_wcsService.wallpaperTimer.Stop();
247-
}
248-
249-
_wcsService.enableTransitions = false;
250-
}
251-
else if (e.Mode == PowerModes.Resume)
252-
{
253-
if (JsonConfig.settings.location != null)
254-
{
255-
_wcsService.StartScheduler();
256-
}
257-
258-
_wcsService.enableTransitions = true;
259-
}
260-
}
261-
262238
private void OnApplicationExit(object sender, EventArgs e)
263239
{
264240
if (downloadDialog != null)

src/InputDialog.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private void okButton_Click(object sender, EventArgs e)
5151

5252
if (Directory.Exists("images"))
5353
{
54-
_wcsService.StartScheduler(true);
54+
_wcsService.RunScheduler(true);
5555
}
5656

5757
MessageBox.Show("Location set successfully to: " + data.display_name +

src/WallpaperChangeScheduler.cs

Lines changed: 20 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,13 @@ class WallpaperChangeScheduler
1313
{
1414
private int[] dayImages;
1515
private int[] nightImages;
16-
17-
private bool isSunUp;
1816
private string lastDate = "yyyy-MM-dd";
1917
private int lastImageId = -1;
20-
private int lastImageNumber = -1;
2118

2219
private WeatherData yesterdaysData;
2320
private WeatherData todaysData;
2421
private WeatherData tomorrowsData;
2522

26-
public bool enableTransitions = true;
2723
public Timer wallpaperTimer = new Timer();
2824

2925
public WallpaperChangeScheduler()
@@ -82,26 +78,23 @@ private void SetWallpaper(int imageId)
8278
string imageFilename = String.Format(JsonConfig.imageSettings.imageFilename, imageId);
8379
string imagePath = Path.Combine(Directory.GetCurrentDirectory(), "images", imageFilename);
8480

85-
if (enableTransitions)
86-
{
87-
WallpaperChanger.EnableTransitions();
88-
}
89-
81+
WallpaperChanger.EnableTransitions();
9082
WallpaperChanger.SetWallpaper(imagePath);
9183

9284
lastImageId = imageId;
9385
}
9486

95-
public void StartScheduler(bool forceRefresh = false)
87+
public void RunScheduler(bool forceRefresh = false)
9688
{
9789
if (wallpaperTimer != null)
9890
{
9991
wallpaperTimer.Stop();
10092
}
10193

10294
string currentDate = GetDateString();
95+
bool shouldRefresh = currentDate != lastDate || forceRefresh;
10396

104-
if (currentDate != lastDate || forceRefresh)
97+
if (shouldRefresh)
10598
{
10699
todaysData = GetWeatherData(currentDate);
107100
lastDate = currentDate;
@@ -110,14 +103,22 @@ public void StartScheduler(bool forceRefresh = false)
110103
if (DateTime.Now < todaysData.SunriseTime)
111104
{
112105
// Before sunrise
113-
yesterdaysData = GetWeatherData(GetDateString(-1));
106+
if (shouldRefresh || yesterdaysData == null)
107+
{
108+
yesterdaysData = GetWeatherData(GetDateString(-1));
109+
}
110+
114111
tomorrowsData = null;
115112
}
116113
else if (DateTime.Now > todaysData.SunsetTime)
117114
{
118115
// After sunset
119116
yesterdaysData = null;
120-
tomorrowsData = GetWeatherData(GetDateString(1));
117+
118+
if (shouldRefresh || tomorrowsData == null)
119+
{
120+
tomorrowsData = GetWeatherData(GetDateString(1));
121+
}
121122
}
122123
else
123124
{
@@ -130,18 +131,16 @@ public void StartScheduler(bool forceRefresh = false)
130131

131132
if (yesterdaysData == null && tomorrowsData == null)
132133
{
133-
StartDaySchedule();
134+
UpdateDayImage();
134135
}
135136
else
136137
{
137-
StartNightSchedule();
138+
UpdateNightImage();
138139
}
139140
}
140141

141-
private void StartDaySchedule()
142+
private void UpdateDayImage()
142143
{
143-
isSunUp = true;
144-
145144
TimeSpan dayTime = todaysData.SunsetTime - todaysData.SunriseTime;
146145
TimeSpan timerLength = new TimeSpan(dayTime.Ticks / dayImages.Length);
147146
int imageNumber = GetImageNumber(todaysData.SunriseTime, timerLength);
@@ -152,37 +151,11 @@ private void StartDaySchedule()
152151
if (dayImages[imageNumber] != lastImageId)
153152
{
154153
SetWallpaper(dayImages[imageNumber]);
155-
lastImageNumber = imageNumber;
156-
}
157-
}
158-
159-
private void NextDayImage()
160-
{
161-
if (lastImageNumber == dayImages.Length - 1)
162-
{
163-
SwitchToNight();
164-
return;
165154
}
166-
167-
TimeSpan dayTime = todaysData.SunsetTime - todaysData.SunriseTime;
168-
StartTimer(dayTime.Ticks / dayImages.Length);
169-
170-
lastImageNumber++;
171-
SetWallpaper(dayImages[lastImageNumber]);
172155
}
173156

174-
private void SwitchToNight()
157+
private void UpdateNightImage()
175158
{
176-
tomorrowsData = GetWeatherData(GetDateString(1));
177-
lastImageNumber = -1;
178-
179-
StartNightSchedule();
180-
}
181-
182-
private void StartNightSchedule()
183-
{
184-
isSunUp = false;
185-
186159
WeatherData day1Data = (yesterdaysData == null) ? todaysData : yesterdaysData;
187160
WeatherData day2Data = (yesterdaysData == null) ? tomorrowsData : todaysData;
188161

@@ -196,44 +169,9 @@ private void StartNightSchedule()
196169
if (nightImages[imageNumber] != lastImageId)
197170
{
198171
SetWallpaper(nightImages[imageNumber]);
199-
lastImageNumber = imageNumber;
200-
}
201-
}
202-
203-
private void NextNightImage()
204-
{
205-
if (lastImageNumber == nightImages.Length - 1)
206-
{
207-
SwitchToDay();
208-
return;
209-
}
210-
211-
WeatherData day1Data = (yesterdaysData == null) ? todaysData : yesterdaysData;
212-
WeatherData day2Data = (yesterdaysData == null) ? tomorrowsData : todaysData;
213-
214-
TimeSpan nightTime = day2Data.SunriseTime - day1Data.SunsetTime;
215-
StartTimer(nightTime.Ticks / nightImages.Length);
216-
217-
lastImageNumber++;
218-
SetWallpaper(nightImages[lastImageNumber]);
219-
220-
if (DateTime.Now.Hour >= 0 && DateTime.Now.Hour < 12 && tomorrowsData != null)
221-
{
222-
yesterdaysData = todaysData;
223-
todaysData = tomorrowsData;
224-
tomorrowsData = null;
225-
lastDate = GetDateString();
226172
}
227173
}
228174

229-
private void SwitchToDay()
230-
{
231-
yesterdaysData = null;
232-
lastImageNumber = -1;
233-
234-
StartDaySchedule();
235-
}
236-
237175
public void ToggleDarkMode()
238176
{
239177
if (!JsonConfig.settings.darkMode)
@@ -245,22 +183,15 @@ public void ToggleDarkMode()
245183
dayImages = JsonConfig.imageSettings.dayImageList;
246184
}
247185

248-
StartScheduler();
186+
RunScheduler();
249187
JsonConfig.settings.darkMode ^= true;
250188
}
251189

252190
private void OnWallpaperTimerTick(object sender, EventArgs e)
253191
{
254192
wallpaperTimer.Stop();
255193

256-
if (isSunUp)
257-
{
258-
NextDayImage();
259-
}
260-
else
261-
{
262-
NextNightImage();
263-
}
194+
RunScheduler();
264195
}
265196
}
266197
}

0 commit comments

Comments
 (0)