示例#1
0
        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"
                        }
                    }
                });
            });
        }
示例#2
0
        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);
        }
示例#3
0
        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);
        }
示例#4
0
        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();
        }
示例#5
0
        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);
        }
示例#6
0
        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);
        }
示例#7
0
        //		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;
            }
        }
示例#8
0
        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
                    });
                }
            });
        }
示例#9
0
 public IAction CreateNewsFetchAction(TopStoriesCategory category)
 {
     return(new NewsFetchAction {
         Category = category
     });
 }