@@ -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