public AsyncActionsCreator <State> NewsFetchActionAsync_1(TopStoriesCategory category) { return(async(dispatch, getState) => { System.Diagnostics.Debug.WriteLine("NewsFetchAction..."); dispatch(new NewsFetchAction { Category = category }); await Task.Delay(1000); System.Diagnostics.Debug.WriteLine("NewsFetchSuccessAction..."); dispatch(new NewsFetchSuccessAction { Articles = new List <Article> { new Article() { Url = "1" }, new Article() { Url = "2" }, new Article() { Url = "3" } } }); }); }
public async Task <List <Article> > TopStories(TopStoriesCategory category) { var response = await this.RequestTopStories(category); List <Article> articles = response != null ? response.Results : new List <Article> (); return(articles); }
public List <Article> TopStoriesSync(TopStoriesCategory category) { var assembly = this.GetType().GetTypeInfo().Assembly; var textStreamReader = new StreamReader(assembly.GetManifestResourceStream("StartingPCL.Resources.top-stories.json")); var json = textStreamReader.ReadToEnd(); var list = JsonConvert.DeserializeObject <List <Article> >(json); return(list); }
private void OnLoadMore() { var nextCategory = Enum.GetValues(typeof(TopStoriesCategory)) .Cast <TopStoriesCategory> () .SkipWhile(e => e != this.Category) .Skip(1) .FirstOrDefault(); this.Category = nextCategory; this.ActionsFactory.NewsFetchActionAsync(this.Category).DispatchAsync(); }
public Task <List <Article> > TopStories(TopStoriesCategory category) { // var _assembly = Assembly.GetExecutingAssembly(); var assembly = this.GetType().GetTypeInfo().Assembly; // _imageStream = assembly.GetManifestResourceStream("MyNamespace.MyImage.bmp"); var textStreamReader = new StreamReader(assembly.GetManifestResourceStream("StartingPCL.Resources.top-stories.json")); var json = textStreamReader.ReadToEnd(); var list = Task.Factory.StartNew(() => JsonConvert.DeserializeObject <List <Article> >(json)); // var list = JsonConvert.DeserializeObject<List<Article>>(json); return(list); }
async Task <TopStoriesResponse> RequestTopStories(TopStoriesCategory category) { Log.Info("Request top stories: {0}", category.ToString()); // TopStoriesResponse res = await this.RestService.TopStories (TopStoriesApiKey, category); // return res; TopStoriesResponse res = null; try { res = await this.RestService.TopStories(TopStoriesApiKey, category); return(res); } catch (Exception ex) { // TODO: Do not handle all exceptions // TODO: Implement error handling Debug.WriteLine($"Loading error: {ex}"); } return(res); }
// async Task FetchAllArticlesAsync () // { // await this.FetchArticlesAsync (TopStoriesCategory.automobiles); // await this.FetchArticlesAsync (TopStoriesCategory.travel); // await this.FetchArticlesAsync (TopStoriesCategory.food); // await this.FetchArticlesAsync (TopStoriesCategory.sports); // await this.FetchArticlesAsync (TopStoriesCategory.arts); // await this.FetchArticlesAsync (TopStoriesCategory.technology); // await this.FetchArticlesAsync (TopStoriesCategory.business); // await this.FetchArticlesAsync (TopStoriesCategory.magazine); // await this.FetchArticlesAsync (TopStoriesCategory.realestate); // await this.FetchArticlesAsync (TopStoriesCategory.obituaries); // await this.FetchArticlesAsync (TopStoriesCategory.fashion); // } // async Task FetchArticlesAsync(TopStoriesCategory category) { if (this.IsBusy) { return; } this.IsBusy = true; try { Debug.WriteLine($"[NewsListViewModel] FetchArticles {category.ToString()}..."); var loadTask = this.NewsService.TopStories(category); var viewModels = await loadTask.ContinueWith(t => { // Debug.WriteLine ($"AppBase.IsMainThread = {AppBase.IsMainThread}"); var objs = t.Result; // create view models in background var vms = from article in objs select this.ViewModelsFactory.ArticleViewModel(article); return(vms); }); // TaskScheduler.FromCurrentSynchronizationContext() this.Articles.AddRange(viewModels); var count = this.Articles.Count; this.Title = $"NY Times ({count} articles)"; this.IsBusy = false; } catch (Exception ex) { Debug.WriteLine(ex.ToString()); await this.Router.DisplayAlert("FetchArticlesAsync", ex.Message, "OK"); } finally { this.IsBusy = false; } }
public AsyncActionsCreator <State> NewsFetchActionAsync(TopStoriesCategory category) { return(async(dispatch, getState) => { Log.Info("NewsFetchAction..."); dispatch(new NewsFetchAction { Category = category }); try { var models = await this.newsService.TopStories(category); Log.Info($"NewsFetchSuccessAction... {models.Count}"); dispatch(new NewsFetchSuccessAction { Articles = models }); } catch (Exception ex) { dispatch(new NewsFetchFailureAction { Error = ex }); } }); }
public IAction CreateNewsFetchAction(TopStoriesCategory category) { return(new NewsFetchAction { Category = category }); }