Skip to content

Commit

Permalink
Merge pull request #90 from t1m0thyj/feature-4segment
Browse files Browse the repository at this point in the history
Feature 4segment
  • Loading branch information
t1m0thyj authored Jan 9, 2019
2 parents 99b20f5 + c85cdfc commit beda1c8
Show file tree
Hide file tree
Showing 22 changed files with 543 additions and 346 deletions.
128 changes: 128 additions & 0 deletions src/Compatibility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;

namespace WinDynamicDesktop
{
class Compatibility
{
public static void CompatibilizeThemes()
{
if (Directory.Exists("images"))
{
UpdateThemeFileStructure();
}

UpdateThemeImageLists();
}

// TODO Remove after 2.3.0
private static void UpdateThemeFileStructure()
{
List<string> filePaths = Directory.GetFiles("themes", "*.json").ToList();
filePaths.AddRange(ThemeManager.defaultThemes.Select(
themeId => Path.Combine("themes", themeId + ".json")));

foreach (string filePath in filePaths)
{
string themeId = Path.GetFileNameWithoutExtension(filePath);
Directory.CreateDirectory(Path.Combine("themes", themeId));

if (File.Exists(filePath))
{
File.Move(filePath, Path.Combine("themes", themeId, "theme.json"));
}

ThemeConfig theme = JsonConfig.LoadTheme(themeId);
foreach (string imagePath in Directory.GetFiles("images", theme.imageFilename))
{
File.Move(imagePath,
Path.Combine("themes", themeId, Path.GetFileName(imagePath)));
}
}

if (Directory.GetFiles("images").Length == 0 &&
Directory.GetDirectories("images").Length == 0)
{
Directory.Delete("images", false);
}
}

// TODO Remove after 3.0
private static void UpdateThemeImageLists()
{
List<string> upgradeIds = new List<string>() { "BitDay", "Earth_View", "Firewatch",
"New_York", "San_Francisco", "High_Sierra"};
foreach (string filePath in Directory.EnumerateFiles("themes", "*.json",
SearchOption.AllDirectories))
{
string themeId = Path.GetFileName(Path.GetDirectoryName(filePath));

if (!upgradeIds.Contains(themeId))
{
continue;
}

string jsonText = File.ReadAllText(filePath);

if (!jsonText.Contains("sunriseImageList") ||
!jsonText.Contains("sunsetImageList"))
{
jsonText = jsonText.Replace("}",
",\"sunriseImageList\":[],\"sunsetImageList\":[]}");
ThemeConfig theme = JsonConvert.DeserializeObject<ThemeConfig>(jsonText);

if (themeId == "BitDay")
{
theme.sunriseImageList = new int[] { 12, 1 };
theme.dayImageList = new int[] { 2, 3, 4, 5, 6 };
theme.sunsetImageList = new int[] { 7, 8 };
theme.nightImageList = new int[] { 9, 10, 11 };
}
else if (themeId == "Earth_View")
{
theme.sunriseImageList = new int[] { 4, 5, 6 };
theme.dayImageList = new int[] { 7, 8, 9, 10, 11 };
theme.sunsetImageList = new int[] { 12, 13, 14 };
theme.nightImageList = new int[] { 15, 16, 1, 2, 3 };
}
else if (themeId == "Firewatch")
{
theme.sunriseImageList = new int[] { 1, 2 };
theme.dayImageList = new int[] { 3, 4, 5 };
theme.sunsetImageList = new int[] { 6, 7 };
theme.nightImageList = new int[] { 8 };
}
else if (themeId == "New_York")
{
theme.sunriseImageList = new int[] { 1, 2, 3 };
theme.dayImageList = new int[] { 4, 5, 6, 7, 8 };
theme.sunsetImageList = new int[] { 9, 10, 11, 12 };
theme.nightImageList = new int[] { 13, 14, 15, 16 };
}
else if (themeId == "San_Francisco")
{
theme.sunriseImageList = new int[] { 2, 3, 4 };
theme.dayImageList = new int[] { 5, 6, 7, 8, 9 };
theme.sunsetImageList = new int[] { 10, 16, 11, 12 };
theme.nightImageList = new int[] { 13, 14, 15, 1 };
}
else if (themeId == "High_Sierra")
{
theme.sunriseImageList = new int[] { 3, 4, 5 };
theme.dayImageList = new int[] { 6, 7, 8, 9, 10 };
theme.sunsetImageList = new int[] { 9, 1, 12, 13 };
theme.nightImageList = new int[] { 14, 15, 16, 2 };
}

string newJson = JsonConvert.SerializeObject(theme);
File.WriteAllText(filePath, newJson);
}
}
}
}
}
6 changes: 2 additions & 4 deletions src/DesktopHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DesktopHelper : PlatformHelper

public override string GetCurrentDirectory()
{
return Path.GetDirectoryName(Application.ExecutablePath);
return Application.StartupPath;
}

public override void CheckStartOnBoot()
Expand All @@ -37,9 +37,7 @@ public override void ToggleStartOnBoot()

if (!startOnBoot)
{
string exePath = Path.Combine(Directory.GetCurrentDirectory(),
Environment.GetCommandLineArgs()[0]);
startupKey.SetValue("WinDynamicDesktop", exePath);
startupKey.SetValue("WinDynamicDesktop", Application.ExecutablePath);
startOnBoot = true;
}
else
Expand Down
8 changes: 4 additions & 4 deletions src/InputDialog.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions src/InputDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ private async void okButton_Click(object sender, EventArgs e)
JsonConfig.settings.latitude = data.lat;
JsonConfig.settings.longitude = data.lon;

this.Hide();

if (ThemeManager.isReady)
{
AppContext.wcsService.RunScheduler();
Expand All @@ -106,7 +104,6 @@ private async void okButton_Click(object sender, EventArgs e)
}
else
{
this.Hide();
bool locationUpdated = await UwpLocation.UpdateGeoposition();

if (locationUpdated)
Expand All @@ -122,8 +119,6 @@ private async void okButton_Click(object sender, EventArgs e)
{
MessageBox.Show("Failed to get location from Windows location service.",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);

this.Show();
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/JsonConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ public class AppConfig

public class ThemeConfig
{
public string themeName { get; set; }
public string themeId { get; set; }
public string displayName { get; set; }
public string imagesZipUri { get; set; }
public string imageFilename { get; set; }
public string imageCredits { get; set; }
public int[] sunriseImageList { get; set; }
public int[] dayImageList { get; set; }
public int[] sunsetImageList { get; set; }
public int[] nightImageList { get; set; }
}

Expand Down Expand Up @@ -75,7 +78,7 @@ public static ThemeConfig LoadTheme(string name)
}

ThemeConfig theme = JsonConvert.DeserializeObject<ThemeConfig>(themeJson);
theme.themeName = name;
theme.themeId = name;

return theme;
}
Expand Down
4 changes: 2 additions & 2 deletions src/LocationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public static void Initialize()
}
else
{
UpdateLocation();
ChangeLocation();
}
}

public static void UpdateLocation()
public static void ChangeLocation()
{
if (locationDialog == null)
{
Expand Down
7 changes: 3 additions & 4 deletions src/MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private static List<ToolStripItem> GetMenuItems()
new ToolStripMenuItem("WinDynamicDesktop"),
new ToolStripSeparator(),
themeItem,
new ToolStripMenuItem("&Update Location...", null, OnLocationItemClick)
new ToolStripMenuItem("&Change Location...", null, OnLocationItemClick)
});
items[0].Enabled = false;

Expand Down Expand Up @@ -79,7 +79,6 @@ private static void ToggleDarkMode()
JsonConfig.settings.darkMode = isEnabled;
darkModeItem.Checked = isEnabled;

AppContext.wcsService.LoadImageLists();
AppContext.wcsService.RunScheduler();
}

Expand All @@ -90,12 +89,12 @@ private static void OnThemeItemClick(object sender, EventArgs e)

private static void OnLocationItemClick(object sender, EventArgs e)
{
LocationManager.UpdateLocation();
LocationManager.ChangeLocation();
}

private static void OnRefreshItemClick(object sender, EventArgs e)
{
AppContext.wcsService.RunScheduler();
AppContext.wcsService.RunScheduler(true);
}

private static void OnDarkModeClick(object sender, EventArgs e)
Expand Down
4 changes: 2 additions & 2 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static class Program
[STAThread]
static void Main()
{
Environment.CurrentDirectory = UwpDesktop.GetHelper().GetCurrentDirectory();
Directory.SetCurrentDirectory(UwpDesktop.GetHelper().GetCurrentDirectory());
Application.ThreadException += OnThreadException;
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;

Expand All @@ -39,7 +39,7 @@ static void LogError(Exception exc)
{
string errorMessage = exc.ToString() + "\n";
string logFilename = Path.Combine(Directory.GetCurrentDirectory(),
Environment.GetCommandLineArgs()[0] + ".log");
Path.GetFileName(Environment.GetCommandLineArgs()[0]) + ".log");

try
{
Expand Down
6 changes: 3 additions & 3 deletions src/ProgressDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void DownloadNext()
{
List<string> imagesZipUris = theme.imagesZipUri.Split('|').ToList();
client.DownloadFileAsync(new Uri(imagesZipUris.First()),
theme.themeName + "_images.zip", imagesZipUris.Skip(1).ToList());
theme.themeId + "_images.zip", imagesZipUris.Skip(1).ToList());
}
}
else
Expand Down Expand Up @@ -77,7 +77,7 @@ public async void OnDownloadFileCompleted(object sender, AsyncCompletedEventArgs
{
ThemeConfig theme = downloadQueue.Peek();
client.DownloadFileAsync(new Uri(imagesZipUris.First()),
theme.themeName + "_images.zip", imagesZipUris.Skip(1).ToList());
theme.themeId + "_images.zip", imagesZipUris.Skip(1).ToList());
}
else
{
Expand All @@ -86,7 +86,7 @@ public async void OnDownloadFileCompleted(object sender, AsyncCompletedEventArgs
if (e.Error == null)
{
await Task.Run(() => ThemeManager.ExtractTheme(
theme.themeName + "_images.zip", theme.themeName, true));
theme.themeId + "_images.zip", theme.themeId, true));
}

DownloadNext();
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("2.3.0")]
[assembly: AssemblyVersion("3.0.0")]
//[assembly: AssemblyFileVersion("1.0.0.0")]
43 changes: 30 additions & 13 deletions src/SunriseSunset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,51 @@
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
using Innovative.SolarCalculator;
using SunCalcNet.Model;

namespace WinDynamicDesktop
{
public class SolarData
{
public DateTime SunriseTime { get; set; }
public DateTime SunsetTime { get; set; }
public DateTime sunriseTime { get; set; }
public DateTime sunsetTime { get; set; }
public DateTime[] solarTimes { get; set; }
}

class SunriseSunsetService
{
public static SolarData GetSolarData(DateTime date)
private static Dictionary<string,DateTime> GetSunPhases(DateTime date, string lat,
string lng)
{
return GetSolarData(JsonConfig.settings.latitude, JsonConfig.settings.longitude, date);
double latitude = double.Parse(lat, CultureInfo.InvariantCulture);
double longitude = double.Parse(lng, CultureInfo.InvariantCulture);
var sunPhases = new Dictionary<string, DateTime>();

foreach (SunPhase phase in SunCalcNet.SunCalc.GetSunPhases(date, latitude, longitude))
{
sunPhases.Add(phase.Name.Value, phase.PhaseTime);
}

return sunPhases;
}

public static SolarData GetSolarData(string lat, string lon, DateTime date)
public static SolarData GetSolarData(DateTime date)
{
double latitude = double.Parse(lat, CultureInfo.InvariantCulture);
double longitude = double.Parse(lon, CultureInfo.InvariantCulture);

SolarTimes solarTimes = new SolarTimes(date, latitude, longitude);
var sunPhases = GetSunPhases(date.AddHours(12).ToUniversalTime(),
JsonConfig.settings.latitude, JsonConfig.settings.longitude);

SolarData data = new SolarData();
data.SunriseTime = solarTimes.Sunrise;
data.SunsetTime = solarTimes.Sunset;
SolarData data = new SolarData
{
sunriseTime = sunPhases[SunPhaseName.Sunrise.Value].ToLocalTime(),
sunsetTime = sunPhases[SunPhaseName.Sunset.Value].ToLocalTime(),
solarTimes = new DateTime[4]
};

data.solarTimes[0] = sunPhases[SunPhaseName.NauticalDawn.Value].ToLocalTime();
data.solarTimes[1] = sunPhases[SunPhaseName.GoldenHourEnd.Value].ToLocalTime();
data.solarTimes[2] = sunPhases[SunPhaseName.GoldenHour.Value].ToLocalTime();
data.solarTimes[3] = sunPhases[SunPhaseName.NauticalDusk.Value].ToLocalTime();

return data;
}
}
Expand Down
Loading

0 comments on commit beda1c8

Please sign in to comment.