Ejemplo n.º 1
0
        public async Task LoadAllItems(BlogGroup group)
        {
            var cachedItems = await LoadCachedItems(group);

            group.ItemCount    = cachedItems.Count;
            group.NewItemCount = 0;
            var newItems = await LoadLiveItems(group);

            foreach (var item in newItems.Where(i => !cachedItems.Contains(i, new BaseItemComparer())))
            {
                MessageDialog dialog = null;
                try
                {
                    var client = GetClient();
                    var page   = await client.GetStringAsync(item.PageUri);

                    item.ImageUriList = new ObservableCollection <Uri>(BlogUtility.ExtractImagesFromPage(page));
                    foreach (var image in item.ImageUriList)
                    {
                        ImageUriManager.AddImage(item.Id, image);
                    }
                }
                catch (Exception ex)
                {
                    dialog = new MessageDialog(ex.Message);
                }

                if (dialog != null)
                {
                    await dialog.ShowAsync();
                }
                cachedItems.Add(item);
                group.NewItemCount++;
                await StorageUtility.SaveItem(group.Id.GetHashCode().ToString(), item);
            }

            foreach (var item in cachedItems.OrderByDescending(i => i.PostDate))
            {
                group.Items.Add(item);
            }
        }
Ejemplo n.º 2
0
        private static async Task <List <BlogItem> > LoadLiveItems(BlogGroup group)
        {
            var info = NetworkInformation.GetInternetConnectionProfile();

            if (info == null || info.GetNetworkConnectivityLevel() != NetworkConnectivityLevel.InternetAccess)
            {
                return(new List <BlogItem>());
            }

            MessageDialog dialog = null;
            var           retVal = new List <BlogItem>();

            try
            {
                var client = GetSyndicationClient();
                var feed   = await client.RetrieveFeedAsync(group.RssUri);

                foreach (var item in feed.Items.OrderBy(i => i.PublishedDate))
                {
                    MessageDialog innerDialog = null;
                    try
                    {
                        var blogItem = new BlogItem {
                            Group = @group
                        };
                        var uri = string.Empty;

                        if (item.Links.Count > 0)
                        {
                            var link =
                                item.Links.FirstOrDefault(
                                    l => l.Relationship.Equals("alternate", StringComparison.OrdinalIgnoreCase)) ??
                                item.Links[0];
                            uri = link.Uri.AbsoluteUri;
                        }

                        blogItem.Id = uri;

                        blogItem.PageUri = new Uri(uri, UriKind.Absolute);
                        blogItem.Title   = item.Title != null ? item.Title.Text : "(no title)";

                        blogItem.PostDate = item.PublishedDate.LocalDateTime;

                        var content = "(no content)";

                        if (item.Content != null)
                        {
                            content = BlogUtility.ParseHtml(item.Content.Text);
                        }
                        else if (item.Summary != null)
                        {
                            content = BlogUtility.ParseHtml(item.Summary.Text);
                        }

                        blogItem.Description = content;

                        retVal.Add(blogItem);
                    }
                    catch (Exception ex)
                    {
                        innerDialog = new MessageDialog(ex.Message);
                    }

                    if (innerDialog != null)
                    {
                        await innerDialog.ShowAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                dialog = new MessageDialog(ex.Message);
            }

            if (dialog != null)
            {
                await dialog.ShowAsync();
            }
            return(retVal);
        }