Click here to Skip to main content
15,895,746 members
Articles / Mobile Apps

Netflix Browser for Windows Phone 7 - Part 1

Rate me:
Please Sign up or sign in to vote.
4.95/5 (58 votes)
17 Mar 2011CPOL15 min read 219.6K   1.1K   92  
Learn how to use the Pivot and Panorama controls, page navigation, OData and more!
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Services.Client;
using System.Diagnostics;
using System.Linq;
using System.Net;

using NetflixModel;

namespace NetflixBrowser
{
	public class GenreDvdsViewModel : INotifyPropertyChanged
	{	
		bool loaded;
		public bool Loaded
		{
			get
			{
				return loaded;
			}
			set
			{
				loaded = value;
				OnPropertyChanged("Loaded");
			}
		}
		
		bool busy;
		public bool Busy
		{
			get
			{
				return busy;
			}
			set
			{
				busy = value;
				OnPropertyChanged("Busy");
			}
		}

		String selectedGenreName;
		public String SelectedGenreName
		{
			get
			{
				return selectedGenreName;
			}
			set
			{
				selectedGenreName = value;
				OnPropertyChanged("SelectedGenreName");
			}
		}

		ObservableCollection<Title> genreTitlesByYear;
		public ObservableCollection<Title> GenreTitlesByYear
		{
			get
			{
				return genreTitlesByYear;
			}
			set
			{
				genreTitlesByYear = value;
				OnPropertyChanged("GenreTitlesByYear");
			}
		}

		ObservableCollection<Title> genreTitlesByRating; 
		public ObservableCollection<Title> GenreTitlesByRating
		{
			get
				{
				return genreTitlesByRating;
			}
			set
			{
				genreTitlesByRating = value;
				OnPropertyChanged("GenreTitlesByRating");
			}
		}

		DataServiceCollection<Title> genreTitles = new DataServiceCollection<Title>();
		public DataServiceCollection<Title> GenreTitles
		{
			get
			{
				return genreTitles;
			}
			
		}

		public void LoadGenreTitles(string genreName)
		{
			string genreId = HttpUtility.UrlEncode(genreName);
			genreTitles = DataLayer.GetTitlesForGenre(genreId);
			SelectedGenreName = genreName;
			Loaded = false;
			Busy = true;
			
			if (genreTitles.Count() == 0)
			{
				// Load related titles.
				genreTitles.LoadAsync();
			}
			else
			{
				Loaded = true;
				Busy = false;
				GetTitlesByYear(genreTitles);
				GetTitlesByRating(genreTitles);
			}

			genreTitles.LoadCompleted += new EventHandler<LoadCompletedEventArgs>((sender, args) =>
			{
				// Odata has some limitations. Loading the Titles always raises the below exception, but Titles get loaded. 
				if (args.Error != null && !args.Error.Message.Contains("Wrong number of arguments specified"))
				{
					Debug.WriteLine(
						"Requesting titles failed. " + args.Error.Message);
				}

				Loaded = true;
				Busy = false;
				GetTitlesByYear(genreTitles);
				GetTitlesByRating(genreTitles);
			});
		}

		public void GetTitlesByYear(DataServiceCollection<Title> titles)
		{
			//if (GenreTitlesByYear !=null && GenreTitlesByYear.Count() != 0)
			//{
			//    return;
			//}
			genreTitlesByYear = new ObservableCollection<Title>();
			var query  = (from t in titles
						  orderby t.ReleaseYear descending
						select t).Take(10);

			foreach (Title title in query)
			{
				GenreTitlesByYear.Add(title);
			}
		}


		public void GetTitlesByRating(DataServiceCollection<Title> titles)
		{
			//if (genreTitlesByRating != null && GenreTitlesByRating.Count() != 0)
			//{
			//    return;
			//}
			genreTitlesByRating = new ObservableCollection<Title>();
			var query = (from t in titles
						 orderby t.AverageRating descending 
						 select t).Take(10);

			foreach (Title title in query)
			{
				GenreTitlesByRating.Add(title);
			}
		}

		public event PropertyChangedEventHandler PropertyChanged;	

		protected void OnPropertyChanged(string propertyName)
		{
			if (PropertyChanged != null)
			{
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Product Manager Outcoder
United States United States
Katka has several years of experience working in software development in the areas of market research and e-commerce. She has wide ranging experience in developing Java, ASP.Net MVC, ASP.Net, WPF, Silverlight, and Windows Phone applications.

Company: Outcoder.com
Group: XAML Experts
Proud co-creator of: Surfy browser, Airlock Browser

Comments and Discussions