Skip to content

Commit

Permalink
Fixed logic for switching between day and night and ensuring only one…
Browse files Browse the repository at this point in the history
… timer runs at a time
  • Loading branch information
t1m0thyj committed Jun 24, 2018
1 parent 613d1a0 commit ff87740
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/DesktopHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion src/FormWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class FormWrapper : ApplicationContext
private InputDialog locationDialog;
private NotifyIcon notifyIcon;

public IStartupManager _startupManager;
public StartupManager _startupManager;
public WallpaperChangeScheduler _wcsService;

public FormWrapper()
Expand Down
2 changes: 1 addition & 1 deletion src/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
4 changes: 2 additions & 2 deletions src/UwpDesktop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace WinDynamicDesktop
{
abstract class IStartupManager
abstract class StartupManager
{
public abstract void ToggleStartOnBoot();
}
Expand All @@ -34,7 +34,7 @@ public static string GetCurrentDirectory()
}
}

public static IStartupManager GetStartupManager(MenuItem startupMenuItem)
public static StartupManager GetStartupManager(MenuItem startupMenuItem)
{
if (!IsRunningAsUwp())
{
Expand Down
2 changes: 1 addition & 1 deletion src/UwpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static string GetCurrentDirectory()
}
}

class UwpStartupManager : IStartupManager
class UwpStartupManager : StartupManager
{
private bool startOnBoot;

Expand Down
24 changes: 13 additions & 11 deletions src/WallpaperChangeScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -94,6 +96,7 @@ public void StartScheduler(bool forceRefresh = false)
}

string currentDate = GetDateString();

if (currentDate != lastDate || forceRefresh)
{
todaysData = GetWeatherData(currentDate);
Expand All @@ -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();
}
Expand All @@ -136,16 +136,16 @@ 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);

int imageNumber = GetImageNumber(todaysData.SunriseTime, timerLength, dayImages.Length);
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)
Expand Down Expand Up @@ -183,6 +183,8 @@ private void SwitchToNight()

private void StartNightSchedule()
{
isSunUp = false;

WeatherData day1Data = (yesterdaysData == null) ? todaysData : yesterdaysData;
WeatherData day2Data = (yesterdaysData == null) ? tomorrowsData : todaysData;

Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -244,6 +244,8 @@ private void SwitchToDay()

private void OnWallpaperTimerTick(object sender, EventArgs e)
{
wallpaperTimer.Stop();

if (isSunUp)
{
NextDayImage();
Expand Down

0 comments on commit ff87740

Please sign in to comment.