PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/DataBoundApp1/ViewModels/MainViewModel.cs

https://bitbucket.org/JeffF/wp7codemashsmackdown2012
C# | 151 lines | 129 code | 16 blank | 6 comment | 8 complexity | 9c9f1e8ddca932bca9648565e415ca6c MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Collections.ObjectModel;
  5. using System.IO;
  6. using System.IO.IsolatedStorage;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Xml.Linq;
  10. namespace DataBoundApp1.ViewModels
  11. {
  12. public class MainViewModel : INotifyPropertyChanged
  13. {
  14. public MainViewModel()
  15. {
  16. this.Sessions = new ObservableCollection<SessionViewModel>();
  17. this.Favorites = new ObservableCollection<SessionViewModel>();
  18. this.AllSessions = new List<SessionViewModel>();
  19. }
  20. /// <summary>
  21. /// A collection for SessionViewModel objects.
  22. /// </summary>
  23. public ObservableCollection<SessionViewModel> Sessions { get; private set; }
  24. public ObservableCollection<SessionViewModel> Favorites{ get; private set; }
  25. public List<SessionViewModel> AllSessions{ get; private set; }
  26. public bool IsDataLoaded
  27. {
  28. get;
  29. private set;
  30. }
  31. /// <summary>
  32. /// Creates and adds a few SessionViewModel objects into the Sessions collection.
  33. /// </summary>
  34. public void LoadData()
  35. {
  36. using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
  37. {
  38. if (iso.FileExists("sessions.xml"))
  39. {
  40. using (var file = new IsolatedStorageFileStream("sessions.xml", FileMode.Open, iso))
  41. {
  42. var sessionXml = XElement.Load(file);
  43. ParseXml(sessionXml);
  44. }
  45. }
  46. else
  47. {
  48. RefreshData();
  49. }
  50. }
  51. LoadFavorites();
  52. }
  53. private void LoadFavorites()
  54. {
  55. this.Favorites.Clear();
  56. if (IsolatedStorageSettings.ApplicationSettings.Contains("fav"))
  57. {
  58. var loadedFav = (ObservableCollection<SessionViewModel>) IsolatedStorageSettings.ApplicationSettings["fav"];
  59. foreach (var sessionViewModel in loadedFav)
  60. {
  61. this.Favorites.Add(sessionViewModel);
  62. }
  63. }
  64. }
  65. private void ParseXml(XElement sessionXml)
  66. {
  67. this.AllSessions = sessionXml.Descendants("Session")
  68. .Select(s => new SessionViewModel()
  69. {
  70. Title = s.Element("Title").Value,
  71. Abstract = s.Element("Abstract").Value,
  72. Difficulty = s.Element("Difficulty").Value,
  73. Room = s.Element("Room").Value,
  74. SpeakerName = s.Element("SpeakerName").Value,
  75. Technology = s.Element("Technology").Value,
  76. Uri = s.Element("URI").Value,
  77. Start = DateTime.Parse(s.Element("Start").Value)
  78. })
  79. .OrderBy(s => s.Start)
  80. .ToList();
  81. foreach (var sessionViewModel in this.AllSessions)
  82. {
  83. this.Sessions.Add(sessionViewModel);
  84. }
  85. this.IsDataLoaded = true;
  86. }
  87. public void FilterData(string searchTerm)
  88. {
  89. this.Sessions.Clear();
  90. if (searchTerm.Length == 0)
  91. {
  92. foreach (var sessionViewModel in AllSessions)
  93. {
  94. this.Sessions.Add(sessionViewModel);
  95. }
  96. }
  97. else
  98. {
  99. foreach (var sessionViewModel in AllSessions.Where(s => s.Title.ToLower().Contains(searchTerm.ToLower())))
  100. {
  101. this.Sessions.Add(sessionViewModel);
  102. }
  103. }
  104. }
  105. private void RefreshData()
  106. {
  107. var webClient = new WebClient();
  108. webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadComplete);
  109. webClient.DownloadStringAsync(new Uri("http://www.codemash.org/rest/sessions.xml"));
  110. }
  111. private void DownloadComplete(object sender, DownloadStringCompletedEventArgs e)
  112. {
  113. if (e.Error != null)
  114. return;
  115. var sessionXml = XElement.Parse(e.Result);
  116. ParseXml(sessionXml);
  117. using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
  118. {
  119. using (var file = new IsolatedStorageFileStream("sessions.xml", FileMode.Create, iso))
  120. {
  121. sessionXml.Save(file);
  122. }
  123. }
  124. }
  125. public event PropertyChangedEventHandler PropertyChanged;
  126. private void NotifyPropertyChanged(String propertyName)
  127. {
  128. PropertyChangedEventHandler handler = PropertyChanged;
  129. if (null != handler)
  130. {
  131. handler(this, new PropertyChangedEventArgs(propertyName));
  132. }
  133. }
  134. }
  135. }