wp7codemashsmackdown2012 /DataBoundApp1/ViewModels/MainViewModel.cs

Language C# Lines 151
MD5 Hash 9c9f1e8ddca932bca9648565e415ca6c Estimated Cost $3,146 (why?)
Repository https://bitbucket.org/JeffF/wp7codemashsmackdown2012 View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Xml.Linq;

namespace DataBoundApp1.ViewModels
{
	public class MainViewModel : INotifyPropertyChanged
	{
		public MainViewModel()
		{
			this.Sessions = new ObservableCollection<SessionViewModel>();
			this.Favorites = new ObservableCollection<SessionViewModel>();
			this.AllSessions = new List<SessionViewModel>();
		}

		/// <summary>
		/// A collection for SessionViewModel objects.
		/// </summary>
		public ObservableCollection<SessionViewModel> Sessions { get; private set; }
		public ObservableCollection<SessionViewModel> Favorites{ get; private set; }
		public List<SessionViewModel> AllSessions{ get; private set; }		

		public bool IsDataLoaded
		{
			get;
			private set;
		}

		/// <summary>
		/// Creates and adds a few SessionViewModel objects into the Sessions collection.
		/// </summary>
		public void LoadData()
		{
			using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
			{
				if (iso.FileExists("sessions.xml"))
				{
					using (var file = new IsolatedStorageFileStream("sessions.xml", FileMode.Open, iso))
					{
						var sessionXml = XElement.Load(file);
						ParseXml(sessionXml);
					}
				}
				else
				{
					RefreshData();
				}
			}

			LoadFavorites();
		}

		private void LoadFavorites()
		{
			this.Favorites.Clear();

			if (IsolatedStorageSettings.ApplicationSettings.Contains("fav"))
			{
				var loadedFav = (ObservableCollection<SessionViewModel>) IsolatedStorageSettings.ApplicationSettings["fav"];
				foreach (var sessionViewModel in loadedFav)
				{
					this.Favorites.Add(sessionViewModel);
				}
			}
		}

		private void ParseXml(XElement sessionXml)
		{
			this.AllSessions = sessionXml.Descendants("Session")
				.Select(s => new SessionViewModel()
				             	{
				             		Title = s.Element("Title").Value,
				             		Abstract = s.Element("Abstract").Value,
				             		Difficulty = s.Element("Difficulty").Value,
				             		Room = s.Element("Room").Value,
				             		SpeakerName = s.Element("SpeakerName").Value,
				             		Technology = s.Element("Technology").Value,
				             		Uri = s.Element("URI").Value,
				             		Start = DateTime.Parse(s.Element("Start").Value)
				             	})
				.OrderBy(s => s.Start)
				.ToList();

			foreach (var sessionViewModel in this.AllSessions)
			{
				this.Sessions.Add(sessionViewModel);
			}
			this.IsDataLoaded = true;
		}

		public void FilterData(string searchTerm)
		{
			this.Sessions.Clear();

			if (searchTerm.Length == 0)
			{
				foreach (var sessionViewModel in AllSessions)
				{
					this.Sessions.Add(sessionViewModel);
				}	
			}
			else
			{
				foreach (var sessionViewModel in AllSessions.Where(s => s.Title.ToLower().Contains(searchTerm.ToLower())))
				{
					this.Sessions.Add(sessionViewModel);
				}
			}
		}

		private void RefreshData()
		{
			var webClient = new WebClient();
			webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadComplete);
			webClient.DownloadStringAsync(new Uri("http://www.codemash.org/rest/sessions.xml"));
		}

		private void DownloadComplete(object sender, DownloadStringCompletedEventArgs e)
		{
			if (e.Error != null)
				return;

			var sessionXml = XElement.Parse(e.Result);
			ParseXml(sessionXml);

			using (var iso = IsolatedStorageFile.GetUserStoreForApplication())
			{
				using (var file = new IsolatedStorageFileStream("sessions.xml", FileMode.Create, iso))
				{
					sessionXml.Save(file);
				}
			}
		}

		public event PropertyChangedEventHandler PropertyChanged;
		private void NotifyPropertyChanged(String propertyName)
		{
			PropertyChangedEventHandler handler = PropertyChanged;
			if (null != handler)
			{
				handler(this, new PropertyChangedEventArgs(propertyName));
			}
		}
	}
}
Back to Top