Ejemplo n.º 1
0
        /// <summary>
        /// Retrieves feed data from the server and updates the appropriate FeedViewModel properties.
        /// There may be two condition, One is for the Have ReFreshbuild and the orther is not.
        /// </summary>
        /// 已经修改了变量名使之可以编译,还没有全体check
        private static async Task <bool> TryGetFeedFromServerAsync(FeedViewModel feedViewModel, CancellationToken?cancellationToken = null)
        {
            var originalArticleNum = feedViewModel.Articles.Count;

            try
            {
                var feed = await new SyndicationClient().RetrieveFeedAsync(feedViewModel.Source);

                //if (cancellationToken.HasValue && cancellationToken.Value.IsCancellationRequested) return false;

                // If it is a new feed without in the database, we need to get the default basic info
                bool isHaveNewArticles = false;

                if (feedViewModel.Id == null)
                {
                    // 第一次添加新的feed
                    feedViewModel.Name            = string.IsNullOrEmpty(feedViewModel.Name) ? feed.Title.Text : feedViewModel.Name;
                    feedViewModel.Description     = feed.Subtitle?.Text ?? feed.Title.Text;
                    feedViewModel.IconSrc         = string.Empty;
                    feedViewModel.LastBuildedTime = feed.LastUpdatedTime.ToString();
                    feedViewModel.Id = await SQLiteService.InsertOrReplaceFeedAsync(feedViewModel.AbstractInfo());

                    var homePageLinkString = (feed.IconUri == null) ? feed.Links.Select(l => l.Uri).FirstOrDefault().ToString() : feed.IconUri.ToString();
                    await feedViewModel.TryUpdateIconSource(homePageLinkString);

                    // 现在UI上添加,缩短响应时间
                    MainPage.Current.ViewModel.Feeds.Add(feedViewModel);
                    isHaveNewArticles = true;
                }

                if (isHaveNewArticles || DateTimeOffset.Parse(feedViewModel.LastBuildedTime) < feed.LastUpdatedTime)
                {
                    feedViewModel.LastBuildedTime = feed.LastUpdatedTime.ToString();
                    isHaveNewArticles             = true;
                }

                if (isHaveNewArticles || feedViewModel.Articles.Count() == 0)
                {
                    var articleInfoList = new List <ArticleInfo>();
                    // Get Article from the newly getted feed And sync
                    foreach (var item in feed.Items)
                    {
                        var newArticle = new ArticleModel(new ArticleInfo()
                        {
                            FeedId  = (int)feedViewModel.Id,
                            Title   = item.Title.Text,
                            PubDate = item.PublishedDate.ToString(),
                            // 借鉴了官方的获取本篇文章source的方法
                            Source      = (item.ItemUri ?? item.Links.Select(l => l.Uri).FirstOrDefault()).ToString(),
                            Description = item.Summary == null ? "The article doesn't have description!" : item.Summary.Text,
                            Unread      = true,
                            Isstarred   = false
                        });

                        newArticle.Id = await SQLiteService.InsertOrReplaceArticleAsync(newArticle.AbstractInfo());

                        // 初始化那些不存在数据库里面的用于绑定的属性
                        newArticle.InitialOnlyBindingProperty(feedViewModel);
                        feedViewModel.Articles.Insert(0, newArticle);
                    }
                }
                else
                {
                    Debug.Write("The feed is already the newest.");
                }
                feedViewModel.IsInError    = false;
                feedViewModel.ErrorMessage = null;
                return(true);
            }
            catch (Exception)
            {
                if (!cancellationToken.HasValue || !cancellationToken.Value.IsCancellationRequested)
                {
                    feedViewModel.IsInError    = true;
                    feedViewModel.ErrorMessage = feedViewModel.Articles.Count == originalArticleNum ? BAD_URL_MESSAGE : NO_REFRESH_MESSAGE;
                }
                return(false);
            }
        }