Beispiel #1
0
        public async Task <FeedItem> RefreshFeedItem(FeedItem feedItem)
        {
            try
            {
                if (feedItem != null)
                {
                    //get new episodes from saved URI in feedItem
                    var doc = await RssFeedHelper.GetXDocFromLinkAsync(feedItem.Link);

                    //update ImageLink and ImageFileName for FeedItem
                    var feedItemFromXDoc = FeedItemManager.GetFeedItemFromXDoc(doc, feedItem.Link);
                    feedItem.ImageLink     = feedItemFromXDoc.ImageLink; //new image on FeedItem for new episode
                    feedItem.ImageFileName = feedItemFromXDoc.ImageFileName;
                    feedItem.LastCheck     = DateTime.Now;               //save LastCheck
                    var result = await DataStore.SaveFeedItemAsync(feedItem);

                    if (result == 0)
                    {
                        Debug.WriteLine("Could Not update FeedItem with new LastCheck date");
                    }

                    //get new episodes from saved URI in feedItem
                    var episodes = RssEpisodeManager.GetRssEpisodesFromXDoc(doc, feedItem.Id.Value);

                    //save new episodes to database
                    result = await DataStore.AddEpisodesToFeed(episodes); //returns the number of items added

                    if (result == 0)
                    {
                        Debug.WriteLine("FeedItemManager.RefreshFeedItem Result from AddEpisodesToFeed was 0 ");
                    }

                    //check existing episodes for undownloaded images
                    var existingEpisodes = await DataStore.GetAllEpisodeItemsByFeedIdAsync((int)feedItem.Id);

                    foreach (var episode in existingEpisodes)
                    {
                        if (DownloadService.CanDownloadImage(episode))                  //check if image is already downloaded
                        {
                            await DownloadService.Instance.DownloadImageAsync(episode); //queue to download
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //could not refresh item
                Debug.WriteLine("FeedItemManager.RefreshFeedItem Error " + ex.Message);
            }
            return(feedItem);
        }
        public async Task ExecuteNewFeedItemCommand()
        {
            IsBusy = true;

            try
            {
                var doc = await RssFeedHelper.GetXDocFromLinkAsync(Link);

                if (doc != null)
                {
                    var newFeedItem = FeedItemManager.GetFeedItemFromXDoc(doc, Link);
                    if (newFeedItem != null)
                    {
                        var successFeedImage = await FileHelper.DownloadImageFile(newFeedItem.ImageLink);

                        if (!successFeedImage)
                        {
                            Debug.WriteLine("Could not download feed image file " + newFeedItem.ImageLink);
                        }

                        var result = await DataStore.SaveFeedItemAsync(newFeedItem);

                        if (newFeedItem.Id != null)
                        {
                            //defensive programming, delete any existing records for this new FeedItem.Id
                            //this is because Ids get reused and not all the episodes in and Id might get deleted
                            try
                            {
                                result = await DataStore.DeleteAllEpisodesByFeedIdAsync(newFeedItem.Id); //don't care about the result
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine("Info Error Deleting Episodes before adding new Feed Item " + ex.Message);
                            }
                            var episodeList = RssEpisodeManager.GetRssEpisodesFromXDoc(doc, newFeedItem.Id.Value); //get the episodes
                            result = await DataStore.SaveEpisodeListAsync(episodeList);                            //save the episodes

                            //get the images
                            var count         = 0;
                            var lastImageLink = "";
                            foreach (var episode in episodeList)
                            {
                                if (lastImageLink != episode.ImageLink)
                                {   //don't try to download when the image is the same from episode to episode
                                    lastImageLink = episode.ImageLink;
                                    await DownloadService.Instance.DownloadImageAsync(episode);
                                }
                                count++;
                                //if (count > 20) break; //only get 20, not like 1000
                            }
                            MessagingCenter.Send(newFeedItem, "AddFeedItem");
                        }
                        else
                        {
                            //newFeedItem.Id was null
                            Debug.WriteLine("newFeedItem.Id was null");
                        }
                    }
                    else
                    {
                        //newFeedItem was null
                        Debug.WriteLine("newFeedItem was null");
                    }
                }
                else
                {
                    //problem getting feed
                    await Application.Current.MainPage.DisplayAlert("Alert", "Could not get RSS Feed from Link", "OK");
                }
            }
            catch (Exception ex)
            {
                //could not delete item
                Debug.WriteLine("Could not Add Podcast Feed Item " + ex.Message);
            }
            finally
            {
                IsBusy = false;
            }
        }