Skip to content

Instantly share code, notes, and snippets.

@anuith
Created June 26, 2013 18:11
Show Gist options
  • Save anuith/5869848 to your computer and use it in GitHub Desktop.
Save anuith/5869848 to your computer and use it in GitHub Desktop.
MoviesDataSource.cs - MSP Boot Camp 2013
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Windows.ApplicationModel.Resources.Core;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using System.Collections.Specialized;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.Storage;
using Windows.Data.Json;
// The data model defined by this file serves as a representative example of a strongly-typed
// model that supports notification when members are added, removed, or modified. The property
// names chosen coincide with data bindings in the standard item templates.
//
// Applications may use this model as a starting point and build on it, or discard it entirely and
// replace it with something appropriate to their needs.
namespace KRATIBMoviesSample.Data
{
/// <summary>
/// Base class for <see cref="SampleDataItem"/> and <see cref="SampleDataGroup"/> that
/// defines properties common to both.
/// </summary>
[Windows.Foundation.Metadata.WebHostHidden]
public abstract class MovieDataCommon : Common.BindableBase
{
protected static Uri _baseUri = new Uri("ms-appx:///");
public MovieDataCommon(String uniqueId, String title)
{
this._uniqueId = uniqueId;
this._title = title;
}
private string _uniqueId = string.Empty;
public string UniqueId
{
get { return this._uniqueId; }
set { this.SetProperty(ref this._uniqueId, value); }
}
private string _title = string.Empty;
public string Title
{
get { return this._title; }
set { this.SetProperty(ref this._title, value); }
}
public override string ToString()
{
return this.Title;
}
}
/// <summary>
/// Generic item data model.
/// </summary>
public class MovieDataItem : MovieDataCommon
{
public MovieDataItem(String uniqueId, String title, String description, IEnumerable<string> genres, String rating,
DateTime datePublished, MovieDataYear yearGroup, TimeSpan duration, String posterImagePath)
: base(uniqueId, title)
{
this._description = description;
this._genres = genres;
this._datePublished = datePublished;
this._yearGroup = yearGroup;
this._duration = duration;
this._posterImagePath = posterImagePath;
}
private string _description = string.Empty;
public string Description
{
get { return this._description; }
set { this.SetProperty(ref this._description, value); }
}
private string _contentRating = string.Empty;
public string ContentRating
{
get { return this._contentRating; }
set { this.SetProperty(ref this._contentRating, value); }
}
private DateTime _datePublished;
public DateTime DatePublished
{
get { return this._datePublished; }
set { this.SetProperty(ref this._datePublished, value); }
}
public int YearPublished
{
get { return this._datePublished.Year; }
}
private MovieDataYear _yearGroup;
public MovieDataYear YearGroup
{
get { return this._yearGroup; }
set { this.SetProperty(ref this._yearGroup, value); }
}
private TimeSpan _duration;
public TimeSpan Duration
{
get { return this._duration; }
set { this.SetProperty(ref this._duration, value); }
}
private ImageSource _posterImage = null;
private String _posterImagePath = null;
public ImageSource PosterImage
{
get
{
if (this._posterImage == null && this._posterImagePath != null)
{
this._posterImage = new BitmapImage(new Uri(MovieDataCommon._baseUri, this._posterImagePath));
}
return this._posterImage;
}
set
{
this._posterImagePath = null;
this.SetProperty(ref this._posterImage, value);
}
}
private IEnumerable<string> _genres;
public IEnumerable<string> Genres
{
get { return this._genres; }
}
}
/// <summary>
/// Generic group data model.
/// </summary>
public class MovieDataYear : MovieDataCommon
{
public MovieDataYear(String uniqueId, String title, int year)
: base(uniqueId, title)
{
this._value = year;
Items.CollectionChanged += ItemsCollectionChanged;
}
private int _value;
public int Value
{
get { return this._value; }
}
private void ItemsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
// Provides a subset of the full items collection to bind to from a GroupedItemsPage
// for two reasons: GridView will not virtualize large items collections, and it
// improves the user experience when browsing through groups with large numbers of
// items.
//
// A maximum of 12 items are displayed because it results in filled grid columns
// whether there are 1, 2, 3, 4, or 6 rows displayed
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
if (e.NewStartingIndex < 12)
{
TopItems.Insert(e.NewStartingIndex, Items[e.NewStartingIndex]);
if (TopItems.Count > 12)
{
TopItems.RemoveAt(12);
}
}
break;
case NotifyCollectionChangedAction.Move:
if (e.OldStartingIndex < 12 && e.NewStartingIndex < 12)
{
TopItems.Move(e.OldStartingIndex, e.NewStartingIndex);
}
else if (e.OldStartingIndex < 12)
{
TopItems.RemoveAt(e.OldStartingIndex);
TopItems.Add(Items[11]);
}
else if (e.NewStartingIndex < 12)
{
TopItems.Insert(e.NewStartingIndex, Items[e.NewStartingIndex]);
TopItems.RemoveAt(12);
}
break;
case NotifyCollectionChangedAction.Remove:
if (e.OldStartingIndex < 12)
{
TopItems.RemoveAt(e.OldStartingIndex);
if (Items.Count >= 12)
{
TopItems.Add(Items[11]);
}
}
break;
case NotifyCollectionChangedAction.Replace:
if (e.OldStartingIndex < 12)
{
TopItems[e.OldStartingIndex] = Items[e.OldStartingIndex];
}
break;
case NotifyCollectionChangedAction.Reset:
TopItems.Clear();
while (TopItems.Count < Items.Count && TopItems.Count < 12)
{
TopItems.Add(Items[TopItems.Count]);
}
break;
}
}
private ObservableCollection<MovieDataItem> _items = new ObservableCollection<MovieDataItem>();
public ObservableCollection<MovieDataItem> Items
{
get { return this._items; }
}
private ObservableCollection<MovieDataItem> _topItem = new ObservableCollection<MovieDataItem>();
public ObservableCollection<MovieDataItem> TopItems
{
get { return this._topItem; }
}
}
/// <summary>
/// Creates a collection of groups and items with hard-coded content.
///
/// SampleDataSource initializes with placeholder data rather than live production
/// data so that sample data is provided at both design-time and run-time.
/// </summary>
public sealed class MoviesDataSource
{
private static MoviesDataSource _sampleDataSource = new MoviesDataSource();
private ObservableCollection<MovieDataYear> _allGroups = new ObservableCollection<MovieDataYear>();
public ObservableCollection<MovieDataYear> AllGroups
{
get { return this._allGroups; }
}
public static IEnumerable<MovieDataYear> GetGroups(string uniqueId)
{
if (!uniqueId.Equals("AllGroups")) throw new ArgumentException("Only 'AllGroups' is supported as a collection of groups");
return _sampleDataSource.AllGroups;
}
public static MovieDataYear GetGroup(string uniqueId)
{
// Simple linear search is acceptable for small data sets
var matches = _sampleDataSource.AllGroups.Where((group) => group.UniqueId.Equals(uniqueId));
if (matches.Count() == 1) return matches.First();
return null;
}
public static MovieDataItem GetItem(string uniqueId)
{
// Simple linear search is acceptable for small data sets
var matches = _sampleDataSource.AllGroups.SelectMany(group => group.Items).Where((item) => item.UniqueId.Equals(uniqueId));
if (matches.Count() == 1) return matches.First();
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment