static void data_SectionStoriesDownloadCompleted(object sender, DownloadStringCompletedEventArgs downloadCompletedEvent, ObservableCollection<Story> storyCollection, Section section, InsertStoriesAt insertStoriesAt) { if (downloadCompletedEvent.Error != null) { DownloadFailed(sender, downloadCompletedEvent); return; } SunApiAdapter.handleJsonWithSanitization( (json) => { var stories = SunApiAdapter.StoriesOfApiResponse(json); var existingNids = storyCollection.Select(s => s.Nid); int countStoriesAdded = insertStoriesAt == InsertStoriesAt.Beginning ? 0 : storyCollection.Count; foreach (Story story in stories.Where(article => !existingNids.Contains(article.Nid))) { story.Vid = section.Vid; // cached stories will already be present // so we want to add the new stuff to the beginning // Do we need to sort anyway, or will this always work? storyCollection.Insert(countStoriesAdded++, story); } }, () => { Debug.Assert(false, "Why couldn't the JSON be parsed?"); DownloadFailed(sender, downloadCompletedEvent); }, downloadCompletedEvent.Result ); }
internal static ObservableCollection<Story> GetStories(Section section, InsertStoriesAt insertStoriesAt) { if (!_downloadedSectionStories.Contains(section) && !_currentlyDownloadingSectionStories.Contains(section)) { _currentlyDownloadingSectionStories.Add(section); downloadData(SunApiAdapter.StoriesUrlOfSection(section), (sender, e) => { _downloadedSectionStories.Add(section); _currentlyDownloadingSectionStories.Remove(section); data_SectionStoriesDownloadCompleted(sender, e, getSectionStories()[section], section, insertStoriesAt); section.LoadedPage++; }); } return getSectionStories()[section]; }