public void Location_Click() { // Show window to change location ChangeLocationWindow w = new ChangeLocationWindow(Location.Latitude, Location.Longitude); w.Owner = Application.Current.MainWindow; w.ShowDialog(); // If user clicked Ok button (and input was validated)... if (w.OkClicked) { // Verify user input won't crash app by checking if given location has all SunPhase entries var phases = SunCalcHelper.GetSunPhases(DateTime.Now, w.Latitude, w.Longitude); const int EXPECTED_NUM_SUNPHASES = 14; if (phases.Count() != EXPECTED_NUM_SUNPHASES) { // User input an invalid location. Notify user MessageBox.Show($"There's a problem with\n{w.Latitude}\u00B0N, {w.Longitude}\u00B0E"); return; } // No problem with user input // Persist location values in settings Properties.Settings.Default.Latitude = w.Latitude; Properties.Settings.Default.Longitude = w.Longitude; Properties.Settings.Default.Save(); // Set Location property (which updates _scheduler) Location = new Location(w.Latitude, w.Longitude); } }
// Constructor public SunInfoWindowViewModel() { // Initialize properties Date = DateTime.Now; _lat = Properties.Settings.Default.Latitude; _lng = Properties.Settings.Default.Longitude; Phases = SunCalcHelper.GetSunPhases(Date, _lat, _lng); AverageProgresses = CalculateAverageProgresses(); }
// Private methods /// <summary> /// Returns a Dictionary containing the average sun progress values for each SunPhase in the SunCalc library for the next year /// </summary> /// <returns></returns> private Dictionary <string, double> CalculateAverageProgresses() { // Build a Dictionary that will contain progress totals for each SunPhase Dictionary <string, double> progressTotals = new Dictionary <string, double>(); progressTotals.Add(SunCalcHelper.PHASE_NAME_SUNRISE, 0); progressTotals.Add(SunCalcHelper.PHASE_NAME_SUNRISE_END, 0); progressTotals.Add(SunCalcHelper.PHASE_NAME_GOLDEN_HOUR_END, 0); progressTotals.Add(SunCalcHelper.PHASE_NAME_SOLAR_NOON, 0); progressTotals.Add(SunCalcHelper.PHASE_NAME_GOLDEN_HOUR, 0); progressTotals.Add(SunCalcHelper.PHASE_NAME_SUNSET_START, 0); progressTotals.Add(SunCalcHelper.PHASE_NAME_SUNSET, 0); progressTotals.Add(SunCalcHelper.PHASE_NAME_DUSK, 0); progressTotals.Add(SunCalcHelper.PHASE_NAME_NAUTICAL_DUSK, 0); progressTotals.Add(SunCalcHelper.PHASE_NAME_NIGHT, 0); progressTotals.Add(SunCalcHelper.PHASE_NAME_NADIR, 0); progressTotals.Add(SunCalcHelper.PHASE_NAME_NIGHT_END, 0); progressTotals.Add(SunCalcHelper.PHASE_NAME_NAUTICAL_DAWN, 0); progressTotals.Add(SunCalcHelper.PHASE_NAME_DAWN, 0); // Build a List of DateTimes containing one DateTime per week for the next 52 weeks List <DateTime> days = new List <DateTime>(); for (int week = 0; week < 52; week++) { days.Add(Date.AddDays(7 * week)); } // Sum progresses for each Sunphase across all days foreach (var day in days) { var phases = SunCalcHelper.GetSunPhases(day, _lat, _lng); foreach (var phase in phases) { progressTotals[phase.Name.Value] += SunCalcHelper.GetSunProgress(phase.PhaseTime, _lat, _lng); } } // Build a Dictionary of average progresses by dividing each entry in progressTotals by the number of elements in days Dictionary <string, double> result = new Dictionary <string, double>(); foreach (KeyValuePair <string, double> entry in progressTotals) { double avg = progressTotals[entry.Key] / days.Count; result.Add(entry.Key, avg); } return(result); }