예제 #1
0
        /// <summary>
        /// Saves the favorites feed (the first feed of the feeds list) to local storage.
        /// </summary>
        public static async Task SaveFavoritesAsync(this FeedViewModel favorites)
        {
            //List<FeedViewModel> fav = new List<FeedViewModel>();
            //fav.Add(favorites);
            //favoriteData.Index(fav);
            var file = await ApplicationData.Current.LocalFolder
                       .CreateFileAsync("favorites.dat", CreationCollisionOption.ReplaceExisting);

            byte[] array = Serializer.Serialize(favorites);
            await FileIO.WriteBytesAsync(file, array);
        }
예제 #2
0
        /// <summary>
        /// Retrieves feed data from the server and updates the appropriate FeedViewModel properties.
        /// </summary>
        /*private*/
        public static async Task <bool> TryGetFeedAsync(FeedViewModel feedViewModel, CancellationToken?cancellationToken = null)
        {
            try
            {
                var feed = await new SyndicationClient().RetrieveFeedAsync(feedViewModel.Link);

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

                feedViewModel.LastSyncDateTime = DateTime.Now;
                feedViewModel.Name             = String.IsNullOrEmpty(feedViewModel.Name) ? feed.Title.Text : feedViewModel.Name;
                feedViewModel.Description      = feed.Subtitle?.Text ?? feed.Title.Text;

                feed.Items.Select(item => new BE.ArticleViewModelBase
                {
                    Title   = item.Title.Text,
                    Summary = item.Summary == null ? string.Empty :
                              item.Summary.Text.RegexRemove("\\&.{0,4}\\;").RegexRemove("<.*?>"),
                    Author        = item.Authors.Select(a => a.NodeValue).FirstOrDefault(),
                    Link          = item.ItemUri ?? item.Links.Select(l => l.Uri).FirstOrDefault(),
                    PublishedDate = item.PublishedDate
                })
                .ToList().ForEach(article =>
                {
                    var favorites    = AppShell.Current.ViewModel.FavoritesFeed;
                    var existingCopy = favorites.Articles.FirstOrDefault(a => a.Equals(article));
                    article          = existingCopy ?? article;
                    if (!feedViewModel.Articles.Contains(article))
                    {
                        feedViewModel.Articles.Add(article);
                    }
                });
                feedViewModel.IsInError    = false;
                feedViewModel.ErrorMessage = null;
                return(true);
            }
            catch (Exception)
            {
                if (!cancellationToken.HasValue || !cancellationToken.Value.IsCancellationRequested)
                {
                    feedViewModel.IsInError    = true;
                    feedViewModel.ErrorMessage = feedViewModel.Articles.Count == 0 ? BAD_URL_MESSAGE : NO_REFRESH_MESSAGE;
                }
                return(false);
            }
        }
예제 #3
0
        /// <summary>
        /// Attempts to update the feed with new data from the server.
        /// </summary>
        public static async Task RefreshAsync(this FeedViewModel feedViewModel, CancellationToken?cancellationToken = null)
        {
            if (feedViewModel.Link.Host == "localhost" ||
                (feedViewModel.Link.Scheme != "http" && feedViewModel.Link.Scheme != "https"))
            {
                return;
            }

            feedViewModel.IsLoading = true;

            int  numberOfAttempts = 5;
            bool success          = false;

            do
            {
                success = await TryGetFeedAsync(feedViewModel, cancellationToken);
            }while (!success && numberOfAttempts-- > 0 &&
                    (!cancellationToken.HasValue || !cancellationToken.Value.IsCancellationRequested));

            feedViewModel.IsLoading = false;
        }