/// <summary> /// Load a model to edit. /// </summary> /// <param name="path">path of the file to load</param> public void Load(string path) { this.Observations.Reset(); try { string filename = Path.GetFileName(path); string year = Path.GetFileName(Path.GetDirectoryName(path)); RawObservations observations = XmlFileIo.ReadXml <RawObservations>( path); this.Observations.LoadObservations( observations, filename, year); } catch (Exception ex) { string errorDescription = $"Error loading {this.Observations.Filename}"; AppStatusMessage message = new AppStatusMessage( errorDescription); Messenger.Default.Send(message); this.logger.WriteLine( $"Event Entry Save : {errorDescription}: {ex}"); } }
/// <summary> /// Get a new date in the model. /// </summary> /// <returns>model date</returns> public DateTime GetDate() { bool conversionIsFaulty = false; int day = 0; int month = 0; int year = 0; DateTime returnDate; string[] dateArray = this.observations.Date.Split('/'); if (dateArray.Length != 3) { conversionIsFaulty = true; } else { if (!int.TryParse(dateArray[0], out day)) { conversionIsFaulty = true; } if (!int.TryParse(dateArray[1], out month)) { conversionIsFaulty = true; } if (!int.TryParse(dateArray[2], out year)) { conversionIsFaulty = true; } } if (conversionIsFaulty) { AppStatusMessage message = new AppStatusMessage( $"Date {this.observations.Date} is invalid. Using Now."); Messenger.Default.Send(message); returnDate = DateTime.Now; } else { returnDate = new DateTime( year, month, day); } return(returnDate); }
/// <summary> /// Save the current model. /// </summary> /// <returns>success flag</returns> public bool Save() { if (string.IsNullOrWhiteSpace(this.Observations.GetLocation())) { string faultString = "Can't save, no location provided."; logger.WriteLine($"Event Entry Save: {faultString}"); AppStatusMessage message = new AppStatusMessage( faultString); Messenger.Default.Send(message); return(false); } string path = $"{DataPath.RawDataPath}\\{this.Observations.Year}"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } try { XmlFileIo.WriteXml( this.Observations.GetObservations(), $"{path}\\{this.Observations.Filename}"); } catch (Exception ex) { string errorDescription = $"Error saving {this.Observations.Filename}"; AppStatusMessage message = new AppStatusMessage( errorDescription); Messenger.Default.Send(message); this.logger.WriteLine( $"Event Entry Save : {errorDescription}: {ex}"); } this.Observations.Reset(); return(true); }
/// <summary> /// Select a new page for the view. /// </summary> /// <param name="newPageName"> /// Name of the page to display. /// </param> private void NewPage(string newPageName) { this.SetMonth(newPageName); List <string> eventPaths = this.FindEventPaths(); this.Events.Clear(); foreach (string eventPath in eventPaths) { try { RawObservations observations = XmlFileIo.ReadXml <RawObservations>( eventPath); ICalendarItem calendarItem = new CalendarItem( observations.Date.Substring(0, 2), observations.Location, observations.Intensity, eventPath, this.openEventCommand); this.Events.Add(calendarItem); } catch (Exception ex) { string errorDescription = $"Error loading {eventPath}"; AppStatusMessage message = new AppStatusMessage( errorDescription); Messenger.Default.Send(message); this.logger.WriteLine( $"Calendar view model : {errorDescription}: {ex}"); } } this.RaisePropertyChangedEvent(nameof(this.Events)); }
/// <summary> /// Load a file and store it in the Data Manager /// </summary> /// <param name="path">path of the file to load</param> private void LoadFile(string path) { try { Beastie beastie = XmlFileIo.ReadXml <Beastie>( path); this.dataManager.AddBeastie(beastie); } catch (Exception ex) { AppStatusMessage message = new AppStatusMessage( $"Error loading {path}"); Messenger.Default.Send(message); this.logger.WriteLine( $"Error loading {path}: {ex}"); } }
/// <summary> /// Update the status with the contents of the latest message. /// </summary> /// <param name="message"> /// Message received via the MVVM messenger service. /// </param> private void NewStatus(AppStatusMessage message) { this.Status = message.Status; }