Click here to Skip to main content
15,885,244 members
Articles / Mobile Apps / Windows Phone 7

Netflix Browser for Windows Phone 7 - Part 2

Rate me:
Please Sign up or sign in to vote.
4.99/5 (35 votes)
9 Nov 2010CPOL17 min read 152.7K   1.5K   50  
Learn how to use the Pivot and Panorama controls, page navigation, OData and more!
using System;
using System.ComponentModel;
using System.Data.Services.Client;
using System.Diagnostics;

using NetflixCatalog.Model;
using alias=NetflixCatalog.Model;



namespace NetflixBrowser
{
	public class MainViewModel : INotifyPropertyChanged
	{
		readonly alias.NetflixCatalog context;
		DataServiceCollection<Genre> genres;
		DataServiceCollection<Title> newTitles;
		DataServiceCollection<Title> topTitles;
		Title selectedTitle = new Title();

		public MainViewModel()
		{
			// Instantiate the context.
			context = 
				new alias.NetflixCatalog(new Uri("http://odata.netflix.com/Catalog/", UriKind.Absolute));
		}

		public Title SelectedTitle
		{
			get
			{
				return selectedTitle;
			}
			private set
			{
				selectedTitle = value;
				OnPropertyChanged("SelectedTitle");
			}
		}

		public DataServiceCollection<Genre> Genres
		{
			get
			{
				return genres;
			}
			private set
			{
				genres = value;
				OnPropertyChanged("Genres");
			}
		}

		public DataServiceCollection<Title> TopTitles
		{
			get
			{
				return topTitles;
			}
			private set
			{
				topTitles = value;
				OnPropertyChanged("TopTitles");
			}
		}

		public DataServiceCollection<Title> NewTitles
		{
			get
			{
				return newTitles;
			}
			private set
			{
				newTitles = value;
				OnPropertyChanged("NewTitles");
			}
		}

		public bool IsDataLoaded { get; private set; }

		#region INotifyPropertyChanged Members

		public event PropertyChangedEventHandler PropertyChanged;

		#endregion

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

		public void LoadNewTitles()
		{
			//Instantiate newTitles.
			newTitles = new DataServiceCollection<Title>(context);
			
			/* Using variable in linq expression raises an exception. 
			 * It appears to be an issue with the OData library. 
			 * For this demo the year has been hardcoded.*/
			int currentYear = DateTime.Now.Year;

			//IOrderedQueryable<Title> query =
			//                from t in context.Titles.AddQueryOption("$top", 10).Expand("Genres").Expand("Languages")
			//                where t.Type == "Movie" && t.ReleaseYear == 2010 && t.Dvd.Available
			//                orderby t.Dvd.AvailableFrom descending
							//select t;

			Uri uriQuery = new Uri("/Titles()?$filter=((Type eq 'Movie') and (ReleaseYear eq 2010)) and Dvd/Available&$orderby=Dvd/AvailableFrom desc&$expand=Genres,Languages&$top=10", UriKind.Relative);
			
			// Asynchronously load the result of the query.
			newTitles.LoadAsync(uriQuery);

			newTitles.LoadCompleted += (sender, args) =>
			                           	{
			                           		if (args.Error != null)
			                           		{
			                           			Debug.WriteLine("Requesting NewTitles failed. " + args.Error.Message);
			                           		}
			                           		else
			                           		{
			                           			IsDataLoaded = true;
			                           		}
			                           	};
		}

		public void LoadTopTitles()
		{
			//Instantiate topTitles.
			topTitles = new DataServiceCollection<Title>(context);

			//IOrderedQueryable<Title> query = from t in context.Titles
			//                                    .AddQueryOption("$top", 20).Expand("Genres").Expand("Languages")
			//                                 where t.Type == "Movie" && t.ReleaseYear == 2010 && t.Dvd.Available
			//                                 orderby t.AverageRating descending
			//                                 select t;

			Uri uriQuery = new Uri("/Titles()?$filter=((Type eq 'Movie') and (ReleaseYear eq 2010)) and Dvd/Available&$orderby=AverageRating desc&$expand=Genres,Languages&$top=20", UriKind.Relative);
			
			// Asynchronously load the result of the query.
			topTitles.LoadAsync(uriQuery);

			topTitles.LoadCompleted += (sender, args) =>
			                           	{
			                           		if (args.Error != null)
			                           		{
			                           			Debug.WriteLine("Requesting TopTitles failed. " + args.Error.Message);
			                           		}
			                           		else
			                           		{
			                           			IsDataLoaded = true;
			                           		}
			                           	};
		}

		public void LoadGenres()
		{
			//Instantiate genres.
			genres = new DataServiceCollection<Genre>(context);
			
			//IQueryable<Genre> query = from g in context.Genres
			//                          select g;

			Uri uriQuery = new Uri("/Genres()", UriKind.Relative);
			
			// Asynchronously load the result of the query.
			genres.LoadAsync(uriQuery);

			genres.LoadCompleted += (sender, args) =>
			                        	{
			                        		if (args.Error != null)
			                        		{
			                        			Debug.WriteLine("Requesting titles failed. " + args.Error.Message);
			                        		}
			                        		else
			                        		{
			                        			IsDataLoaded = true;
			                        		}
			                        	};
		}


		public void LoadData()
		{
			LoadTopTitles();
			LoadNewTitles();
			LoadGenres();
		}


	}
}

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