Skip to content

Instantly share code, notes, and snippets.

@markdstafford
Last active December 16, 2015 15:28
Show Gist options
  • Save markdstafford/5455674 to your computer and use it in GitHub Desktop.
Save markdstafford/5455674 to your computer and use it in GitHub Desktop.
Code for OData 101: Building our first OData-based Windows Store app (Part 2)
public static async Task<IEnumerable<T>> ExecuteAsync<T>(this DataServiceQuery<T> query)
{
return await Task.Factory.FromAsync<IEnumerable<T>>(query.BeginExecute(null, null), query.EndExecute);
}
public static IEnumerable<SampleDataItem> Search(string searchString)
{
var regex = new Regex(searchString,
RegexOptions.CultureInvariant | RegexOptions.IgnoreCase |
RegexOptions.IgnorePatternWhitespace);
return
Instance.AllGroups.SelectMany(g => g.Items).Where(
m => regex.IsMatch(m.Title) || regex.IsMatch(m.Subtitle)).Distinct(new SampleDataItemComparer());
}
public static async void LoadMovies()
{
IEnumerable<Title> titles =
await
((DataServiceQuery<Title>)
Context.Titles.Expand("Genres,AudioFormats,AudioFormats/Language,Awards,Cast").Where(
t => t.Rating == "PG").OrderByDescending(t => t.ReleaseYear).Take(300)).ExecuteAsync();
foreach (Title title in titles)
{
foreach (Genre netflixGenre in title.Genres)
{
SampleDataGroup genre = GetGroup(netflixGenre.Name);
if (genre == null)
{
genre = new SampleDataGroup(netflixGenre.Name, netflixGenre.Name, String.Empty,
title.BoxArt.LargeUrl,
String.Empty);
Instance.AllGroups.Add(genre);
}
var content = new StringBuilder();
// Write additional things to content here if you want them to display in the item detail.
genre.Items.Add(new SampleDataItem(title.Id, title.Name,
String.Format("{0}\r\n\r\n{1} ({2})", title.Synopsis,
title.Rating,
title.ReleaseYear),
title.BoxArt.HighDefinitionUrl ?? title.BoxArt.LargeUrl,
"Description",
content.ToString()));
}
}
}
this.DefaultViewModel["QueryText"] = queryText;
void Filter_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Determine what filter was selected
var selectedFilter = e.AddedItems.FirstOrDefault() as Filter;
if (selectedFilter != null)
{
// Mirror the results into the corresponding Filter object to allow the
// RadioButton representation used when not snapped to reflect the change
selectedFilter.Active = true;
// TODO: Respond to the change in active filter by setting this.DefaultViewModel["Results"]
// to a collection of items with bindable Image, Title, Subtitle, and Description properties
var searchValue = (string)this.DefaultViewModel["QueryText"];
this.DefaultViewModel["Results"] = new List<SampleDataItem>(SampleDataSource.Search(searchValue));
// Ensure results are found
object results;
ICollection resultsCollection;
if (this.DefaultViewModel.TryGetValue("Results", out results) &&
(resultsCollection = results as ICollection) != null &&
resultsCollection.Count != 0)
{
VisualStateManager.GoToState(this, "ResultsFound", true);
return;
}
}
// Display informational text when there are no search results.
VisualStateManager.GoToState(this, "NoResultsFound", true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment