PageRenderTime 53ms CodeModel.GetById 17ms app.highlight 3ms RepoModel.GetById 30ms app.codeStats 0ms

/DataBoundApp1/ViewModels/MainViewModel.cs

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