private void ApplyTheme() { UpdateAppSettings("SelectedTheme", SelectedTheme); this.WindowState = System.Windows.WindowState.Minimized; Hide(); if (currentWeather != null) { Console.WriteLine(themes[SelectedTheme]["Path"] + "\\" + themes[SelectedTheme][currentWeather]); WallpaperApi.SetWallpaper(themes[SelectedTheme]["Path"] + "\\" + themes[SelectedTheme][currentWeather]); } dispatcherTimer.Stop(); dispatcherTimer.Tick += dispatcherTimer_Tick; dispatcherTimer.Interval = new TimeSpan(0, 0, 5); WallpaperApi.EnableTransitions(); dispatcherTimer.Start(); }
private void dispatcherTimer_Tick(object sender, EventArgs e) { if (api.key == "") { api_change_click(null, null); return; } HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response; string j = string.Empty; dynamic json; try { response = (HttpWebResponse)request.GetResponse(); Console.WriteLine(response.IsFromCache); using (Stream stream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(stream)) { j = reader.ReadToEnd(); json = JsonSerializer.Deserialize <dynamic>(j); } string weather_name = json.GetProperty("weather")[0].GetProperty("main").ToString(); Int32 time = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; Int32 sunrise = json.GetProperty("sys").GetProperty("sunrise").GetInt32(); Int32 sunset = json.GetProperty("sys").GetProperty("sunset").GetInt32(); string period; if (600 >= Math.Abs(time - sunrise) || 600 >= Math.Abs(time - sunset)) { period = "Sunset"; } else if ((sunrise + 600) < time && time < (sunset - 600)) { period = "Day"; } else { period = "Night"; } if (weather_name == "Drizzle") { weather_name = "Rain"; } else if (weather_name == "Mist" || weather_name == "Smoke" || weather_name == "Haze" || weather_name == "Dust" || weather_name == "Sand" || weather_name == "Ash" || weather_name == "Squall" || weather_name == "Tornado") { weather_name = "Fog"; } currentWeather = priority[Math.Min(Array.IndexOf(priority, period), Array.IndexOf(priority, weather_name))]; if (currentWeather != currentWallpaper) { currentWallpaper = currentWeather; Console.WriteLine(themes[SelectedTheme]["Path"] + "\\" + themes[SelectedTheme][currentWeather]); WallpaperApi.SetWallpaper(themes[SelectedTheme]["Path"] + "\\" + themes[SelectedTheme][currentWeather]); } } catch (Exception ex) { Console.WriteLine(ex); new LogWriter(ex.ToString()); MessageBox.Show(ex.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error ); UpdateAppSettings("Location", "Chicago, US"); LocationText.Text = "Chicago, US"; UpdateAppSettings("apiKey", ""); api.key = ""; System.Windows.Application.Current.Shutdown(); return; } }
private void dispatcherTimer_Tick(object sender, EventArgs e) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response; string j = string.Empty; dynamic json; try { response = (HttpWebResponse)request.GetResponse(); } catch { MessageBox.Show("The location you entered was not found. Maybe check your spelling?", "Error", MessageBoxButton.OK, MessageBoxImage.Error ); UpdateAppSettings("Location", "Chicago, US"); city = ConfigurationManager.AppSettings.Get("Location"); LocationText.Text = "Location:\n" + city; url = "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=metric&APPID=" + apiKey; request = (HttpWebRequest)WebRequest.Create(url); response = (HttpWebResponse)request.GetResponse(); } using (Stream stream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(stream)) { j = reader.ReadToEnd(); json = JsonSerializer.Deserialize <dynamic>(j); } string weather_name = json.GetProperty("weather")[0].GetProperty("main").ToString(); Int32 time = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; Int32 sunrise = json.GetProperty("sys").GetProperty("sunrise").GetInt32(); Int32 sunset = json.GetProperty("sys").GetProperty("sunset").GetInt32(); string period; if (600 >= Math.Abs(time - sunrise) || 600 >= Math.Abs(time - sunset)) { period = "Sunset"; } else if ((sunrise + 600) < time && time < (sunset - 600)) { period = "Day"; } else { period = "Night"; } if (weather_name == "Drizzle") { weather_name = "Rain"; } else if (weather_name == "Mist" || weather_name == "Smoke" || weather_name == "Haze" || weather_name == "Dust" || weather_name == "Sand" || weather_name == "Ash" || weather_name == "Squall" || weather_name == "Tornado") { weather_name = "Fog"; } string chosen = priority[Math.Min(Array.IndexOf(priority, period), Array.IndexOf(priority, weather_name))]; if (chosen != currentWallpaper) { currentWallpaper = chosen; Console.WriteLine(themes[SelectedTheme]["Path"] + "\\" + themes[SelectedTheme][chosen]); WallpaperApi.SetWallpaper(themes[SelectedTheme]["Path"] + "\\" + themes[SelectedTheme][chosen]); } }