示例#1
0
        public async void GetPostsAsync(PostCategory postCategory)
        {
            switch (postCategory)
            {
            case PostCategory.Hot:
                StartWorkIndication(ViewModelConstants.LOADING_HOT_MESSAGE);
                break;

            case PostCategory.Trending:
                StartWorkIndication(ViewModelConstants.LOADING_TRENDING_MESSAGE);
                break;

            case PostCategory.Vote:
                StartWorkIndication(ViewModelConstants.LOADING_FRESH_MESSAGE);
                break;
            }

            ArePostsLoading = true;

            await Task.Run(async() =>
            {
                var requestStatus = await _clientService.GetPostsAsync(postCategory, NUMBER_OF_POSTS);

                if (requestStatus != null && requestStatus.IsSuccessful)
                {
                    _currentCategory = postCategory;

                    Device.BeginInvokeOnMainThread(() =>
                    {
                        Posts.Clear();
                        Posts = new ObservableCollection <PostView>();

                        _clientService.Posts.ToList().ForEach(post =>
                                                              Posts.Add(PostViewFactory.CreatePostViewFromPost(post)));

                        CurrentPost = Posts.FirstOrDefault();
                    });

                    await Task.Delay(ViewModelConstants.GET_POSTS_DELAY);
                }
                else
                {
                    StopWorkIndication();
                    string message = requestStatus == null ? ViewModelConstants.REQUEST_FAILED_MESSAGE : requestStatus.Message;
                    await ShowMessage(message);
                }
            });

            ArePostsLoading = false;

            StopWorkIndication();
        }
示例#2
0
        public async void GetMorePostsAsync()
        {
            Debug.WriteLine("Getting more posts");

            if (Posts.Count < 1)
            {
                await ShowMessage(ViewModelConstants.EMPTY_POST_LIST_MESSAGE);

                return;
            }

            ArePostsLoading = true;

            await Task.Run(async() =>
            {
                var requestStatus = await _clientService.GetPostsAsync(_currentCategory, NUMBER_OF_POSTS, Posts.Last().Id);

                if (requestStatus != null && requestStatus.IsSuccessful)
                {
                    Device.BeginInvokeOnMainThread(() =>
                                                   _clientService.Posts.ToList().ForEach(post =>
                                                                                         Posts.Add(PostViewFactory.CreatePostViewFromPost(post))));
                }
                else
                {
                    string message = requestStatus == null ?
                                     ViewModelConstants.REQUEST_FAILED_MESSAGE :
                                     requestStatus.Message;

                    await ShowMessage(message);
                }
            });

            ArePostsLoading = false;
        }