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