public async Task <Boolean> CacheCategories(HttpClient client, Boolean forceReload = false) { //If we've already loaded categories, don't bother again (TODO: refresh strategy) if (this._categoryList != null && !forceReload) { return(true); } StorageFolder tempFolder = Windows.Storage.ApplicationData.Current.TemporaryFolder; String filename = "categories.json"; String category_response = null; //Attempt to load categories from the saved file if (!forceReload) { try { StorageFile file = await tempFolder.GetFileAsync(filename); category_response = await FileIO.ReadTextAsync(file); } catch (Exception) { } } //Failing that, go load them Uri category_index_uri = new Uri(App.BLOG_URL + "?json=get_category_index"); try { //If category_response is still null, we are either forcing reload or had an error if (category_response == null) { category_response = await client.GetStringAsync(category_index_uri); } this._categoryList = await JsonConvert.DeserializeObjectAsync <CategoryListObject>(category_response); StorageFile file = await tempFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting); await FileIO.WriteTextAsync(file, category_response); return(true); } catch (Exception) { return(false); } }
public async Task<Boolean> CacheCategories(HttpClient client, Boolean forceReload = false) { //If we've already loaded categories, don't bother again (TODO: refresh strategy) if (this._categoryList != null && !forceReload) { return true; } StorageFolder tempFolder = Windows.Storage.ApplicationData.Current.TemporaryFolder; String filename = "categories.json"; String category_response = null; //Attempt to load categories from the saved file if (!forceReload) { try { StorageFile file = await tempFolder.GetFileAsync(filename); category_response = await FileIO.ReadTextAsync(file); } catch (Exception) { } } //Failing that, go load them Uri category_index_uri = new Uri(App.BLOG_URL + "?json=get_category_index"); try { //If category_response is still null, we are either forcing reload or had an error if (category_response == null) { category_response = await client.GetStringAsync(category_index_uri); } this._categoryList = await JsonConvert.DeserializeObjectAsync<CategoryListObject>(category_response); StorageFile file = await tempFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting); await FileIO.WriteTextAsync(file, category_response); return true; } catch (Exception) { return false; } }