Skip to content

Instantly share code, notes, and snippets.

@anuith
Created June 26, 2013 18:13
Show Gist options
  • Save anuith/5869871 to your computer and use it in GitHub Desktop.
Save anuith/5869871 to your computer and use it in GitHub Desktop.
additional functions to asynchronously load data MSP Boot Camp 2013
public static async Task LoadLocalDataAsync()
{
// Retrieve recipe data from Recipes.txt
var file = await Package.Current.InstalledLocation.GetFileAsync("Assets\\movies.json");
var result = await FileIO.ReadTextAsync(file);
// Parse the JSON recipe data
var movies = JsonArray.Parse(result);
// Convert the JSON objects into RecipeDataItems and RecipeDataGroups
CreateMoviesDataAndGroups(movies);
}
private static void CreateMoviesDataAndGroups(JsonArray array)
{
foreach (var item in array)
{
var obj = item.GetObject();
MovieDataItem movie = null;
MovieDataYear group = null;
string uniqueId, title, description, contentRating, posterImagePath;
uniqueId = title = description = contentRating = posterImagePath = string.Empty;
DateTime datePublished = new DateTime();
int duration = 0;
string[] genres = new string[0];
foreach (var key in obj.Keys)
{
IJsonValue val;
if (!obj.TryGetValue(key, out val))
continue;
switch (key)
{
case "name":
title = val.GetString();
break;
case "description":
description = val.GetString();
break;
case "contentRating":
contentRating = val.GetString();
break;
case "duration":
JsonValueType type = val.ValueType;
if(type != JsonValueType.Number){
duration = 0;
}
break;
case "genres":
string genresValue = val.GetString();
genres = genresValue.Split();
break;
case "datePublished":
string dateValue = val.GetString();
DateTime date = DateTime.Parse(dateValue);
datePublished = date;
break;
case "poster":
posterImagePath = val.GetString();
break;
}
}
uniqueId = "Movie-" + title.Replace(" ", "-").Trim();
var groupKey = datePublished.Year;
group = _sampleDataSource.AllGroups.FirstOrDefault(c => c.Value.Equals(groupKey));
if (group == null)
{
group = new MovieDataYear("Year-" + groupKey, groupKey.ToString(), groupKey);
_sampleDataSource.AllGroups.Add(group);
}
movie = new MovieDataItem(uniqueId, title, description, genres,
contentRating, datePublished, group,
new TimeSpan(duration), posterImagePath);
group.Items.Add(movie);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment