//Set current date for API - create URL and get json private void setAPIDate(APOD apod, DateTime datetime) { try { apod.setAPIDate(datetime); } catch (Exception e) { statusBar.Text = e.Message; apod.media_type = null; } }
//Fill combo box with current date and 7 days back and ahead private void fillDatesCombo(DateTime datetime) { APOD _apod = new APOD(); //use API locally to get dates & descriptions //Up to 7 days back //(we should be safe with just checking the date&media returned by API) comboDates.Items.Clear(); for (int i = -7; i < 0; i++) { _apod.setAPIDate(datetime.AddDays(i)); if (_apod.date != null && _apod.media_type == "image") { comboDates.Items.Add(_apod.apiDate.ToShortDateString() + " " + _apod.title); } } //Current - same here, no extra checking _apod.setAPIDate(datetime); if (_apod.date != null && _apod.media_type == "image") { comboDates.Items.Add(_apod.apiDate.ToShortDateString() + " " + _apod.title); } //comboDates.SelectedIndex = comboDates.Items.Count - 1; //set focus to current date item //Up to 7 days ahead //Extra check if the date is ahead of today - no need to call API in this case for (int i = 1; i <= 7; i++) { if (datetime.AddDays(i) <= DateTime.Today) //don't call API if date later than today { _apod.setAPIDate(datetime.AddDays(i)); if (_apod.date != null && _apod.media_type == "image") { comboDates.Items.Add(_apod.apiDate.ToShortDateString() + " " + _apod.title); } } } }