Example #1
0
        /// <summary>
        /// Try to Get The Favicon From the homepage Server
        /// Use Google tool, So please ensure your computer unblocked  Google
        /// </summary>
        ///

        public static async Task  TryUpdateIconSource(this FeedViewModel feedViewModel, string HomePageUrl)
        {
            if (feedViewModel.IconSrc.ToString() != string.Empty)
            {
                return;
            }
            if (HomePageUrl == string.Empty)
            {
                return;
            }

            //一般这个不会发生
            var feedId = feedViewModel.Id ?? -1;

            int  numberOfAttempts = 2;
            bool success          = false;

            // 尝试下载2次
            do
            {
                success = await WebIconDownloadTool.DownLoadIconFrom_IconUri(HomePageUrl, feedId.ToString());
            } while (!success && numberOfAttempts-- > 0);


            // 如果成功,更新IconSrc, iconsrc为下载的图片文件的文件名,否则IconSrc还会是默认的那个图标路径
            if (success)
            {
                feedViewModel.IconSrc = feedId.ToString() + ".png";
                await SQLiteService.UpdateFeedInfoAsync(feedViewModel.AbstractInfo());
            }
        }
Example #2
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);
            }
        }
Example #3
0
 public static async Task RemoveAFeedAsync(this FeedViewModel feedViewModel)
 {
     await SQLiteService.RemoveAFeedAsync(feedViewModel.AbstractInfo());
 }