//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; } }
//--- Main class methods ----------------------------------------------------------------- //Default constructor - will create program window and set everything up public MainWindow() { InitializeComponent(); //read anything from INI file to check if it was already created //if not, write initial zero number of runs var lastDate = iniFile.Read("lastDate"); if (lastDate == String.Empty) //write default values to INI file { iniFile.Write("lastDate", DateTime.Today.ToString()); iniFile.Write("saveToDisk", checkSaveToDisk.Checked.ToString()); iniFile.Write("pathToSave", string.Empty); } //GUI items labelImageDesc.Text = string.Empty; myIconMenu = new ContextMenu(); myIconMenu.MenuItems.Add("Previous", buttonPrev_Click); myIconMenu.MenuItems.Add("Next", buttonNext_Click); myIconMenu.MenuItems.Add("-"); myIconMenu.MenuItems.Add("Refresh", OnMenuRefresh); myIconMenu.MenuItems.Add("Exit", OnMenuExit); myIcon.Icon = SystemIcons.Asterisk; myIcon.ContextMenu = myIconMenu; pathToSave = iniFile.Read("pathToSave"); textPath.Text = pathToSave; if (iniFile.Read("saveToDIsk") == "True") { checkSaveToDisk.Checked = true; } else { checkSaveToDisk.Checked = false; } statusBar.Text = "Ready!"; //Subscribe for events pictureBox.LoadProgressChanged += PictureBox_LoadProgressChanged; pictureBox.LoadCompleted += PictureBox_LoadCompleted; //Create 'photo of the day' object and for starters set API date to today apod = new APOD(); setAPIDate(apod, DateTime.Parse(iniFile.Read("lastDate"))); //Get the image on startup getNASAApod(); }
//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); } } } }