/DataBoundApp1/ViewModels/MainViewModel.cs
C# | 151 lines | 129 code | 16 blank | 6 comment | 8 complexity | 9c9f1e8ddca932bca9648565e415ca6c MD5 | raw file
- 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));
- }
- }
- }
- }