Example #1
0
        private async Task <int> GetAllPostsCount(int countPerTime, int offset)
        {
            return(await Task.Run(async() =>
            {
                int countNew = 0;

                while (true)
                {
                    var result = await PostApi.GetByUserId(
                        ViewModel.UserId,
                        countPerTime, offset)
                                 .ConfigureAwait(false);

                    if (result?.IsError != false)
                    {
                        continue;
                    }

                    if (result.Data == null)
                    {
                        result.Data = new List <PostSchema>();
                    }

                    if (result.Data.Count == 0)
                    {
                        break;
                    }

                    countNew += result.Data.Count;
                    offset += countPerTime;
                }

                return countNew;
            }).ConfigureAwait(true));
        }
Example #2
0
        public async Task LoadMorePosts(int count, int offset)
        {
            ShowLoadingMoreGrid(true);

            var result = await PostApi.GetByUserId(
                ViewModel.UserId,
                count, offset)
                         .ConfigureAwait(true);

            if (result == null)
            {
                return;
            }

            if (result.Data == null)
            {
                result.Data = new List <PostSchema>();
            }

            await AddMorePosts(result.Data)
            .ConfigureAwait(true);

            ShowLoadingMoreGrid(false);
        }
Example #3
0
        public async Task <int> GetNewPostsCount(int countPerTime, int offset)
        {
            int postsCount = 0;

            Dispatcher.Invoke(() =>
            {
                postsCount = lstPosts.Children.Count;
            });

            if (postsCount == 0)
            {
                return(await GetAllPostsCount(countPerTime, offset)
                       .ConfigureAwait(true));
            }

            int headOldId = -1;

            Dispatcher.Invoke(() =>
            {
                headOldId = ViewModel.LastNewHeadPostId;
            });

            if (headOldId == -1)
            {
                return(0);
            }

            return(await Task.Run(async() =>
            {
                int countNew = 0;
                int headNewId = -1;
                bool headOldIsFound = false;
                bool isFirstRequest = true;

                while (!headOldIsFound)
                {
                    var result = await PostApi.GetByUserId(
                        ViewModel.UserId,
                        countPerTime, offset)
                                 .ConfigureAwait(false);

                    if (result?.IsError != false)
                    {
                        continue;
                    }

                    if (result.Data == null)
                    {
                        result.Data = new List <PostSchema>();
                    }

                    if (result.Data.Count == 0)
                    {
                        await Dispatcher.Invoke(() =>
                        {
                            return UpdatePosts();
                        }).ConfigureAwait(true);

                        return 0;
                    }

                    if (isFirstRequest)
                    {
                        headNewId = result.Data[0].Id;
                        isFirstRequest = false;
                    }

                    foreach (var post in result.Data)
                    {
                        if (post.Id == headOldId)
                        {
                            headOldIsFound = true;
                            break;
                        }

                        ++countNew;
                    }

                    if (countNew >= 500)
                    {
                        await Dispatcher.Invoke(() =>
                        {
                            return UpdatePosts();
                        }).ConfigureAwait(true);

                        return 0;
                    }

                    offset += countPerTime;
                }

                Dispatcher.Invoke(() =>
                {
                    ViewModel.LastNewHeadPostId = headNewId;
                });

                return countNew;
            }).ConfigureAwait(true));
        }