public async Task Setup(int index) { //Split the tasks HtmlWeb web = new HtmlWeb(); HtmlDocument doc = web.Load(URL); //Get Chapters ObservableCollection <ChapterInfo> chapters = new ObservableCollection <ChapterInfo>(); IEnumerable <HtmlNode> chapterCollection = doc.DocumentNode.SelectNodes("//div[@class='chapter-list']").Descendants("div"); ChapterInfo newChapter; foreach (var node in chapterCollection) { newChapter = new ChapterInfo(); HtmlNode link = node.Descendants("span").First().Descendants("a").First(); newChapter.href = link.Attributes["href"].Value; newChapter.Name = link.InnerText; chapters.Add(newChapter); } chapters = new ObservableCollection <ChapterInfo>(chapters.Reverse()); while (currentDownloadTask != null) { await Task.Delay(1000); } //Call and Wait till main thread updates collection await Application.Current.Dispatcher.Invoke(async() => { await SetChapterData(chapters); //Clear Collection _pages.Clear(); //Set Buttons CalculateChapterInfo(); //Start download of chapter by setting chapter index SelectedChapterIndex = index; }); }
public async Task SetChapterData(ObservableCollection <ChapterInfo> infoList) { ChapterNames = new ObservableCollection <string>(); _chapters = new List <ChapterHolder>(); for (int i = 0; i < infoList.Count; i++) { ChapterInfo info = infoList[i]; //Add to chapter list Combobox ChapterNames.Add(info.Name); //Add to chapter holder _chapters.Add( new ChapterHolder { url = info.href }); //await Task.Delay((int)(0.001 * 1000)); } }
public async Task LoadData() { #region Get Data HtmlWeb web = new HtmlWeb(); HtmlDocument doc = web.Load(url); IEnumerable <HtmlNode> infonodes = doc.DocumentNode.SelectNodes("//ul[contains(@class, 'manga-info-text')]").Descendants("li"); //Get Name string name = infonodes.ElementAt(0).Descendants("h1").FirstOrDefault().InnerText; //get Image URL HtmlNode img = doc.DocumentNode.SelectSingleNode("//div[@class='manga-info-pic']").Descendants("img").First(); string imageSource = img.Attributes["src"].Value; //Get Status string status = infonodes.ElementAt(2).InnerText.Substring(9); //Get Authors ObservableCollection <string> authors = new ObservableCollection <string>(); IEnumerable <HtmlNode> authorNodes = infonodes.ElementAt(1).Descendants("a"); for (int i = 0; i < authorNodes.Count(); i++) { authors.Add(authorNodes.ElementAt(i).InnerText); } //Get Genres ObservableCollection <string> genres = new ObservableCollection <string>(); IEnumerable <HtmlNode> genreNodes = infonodes.ElementAt(6).Descendants("a"); for (int i = 0; i < genreNodes.Count(); i++) { genres.Add(genreNodes.ElementAt(i).InnerText); } //Get Description HtmlNodeCollection descriptionNodes = doc.DocumentNode.SelectNodes("//div[@id='noidungm']/text()"); StringBuilder sb = new StringBuilder(); foreach (var node in descriptionNodes) { sb.Append(node.InnerText); } string description = Regex.Replace(sb.ToString(), @"\n|'|"", ""); description = Regex.Replace(description, @"'|’", "'"); //Get Chapter List ObservableCollection <ChapterInfo> chapters = new ObservableCollection <ChapterInfo>(); IEnumerable <HtmlNode> chapterCollection = doc.DocumentNode.SelectNodes("//div[@class='chapter-list']").Descendants("div"); ChapterInfo newChapter; foreach (var node in chapterCollection) { newChapter = new ChapterInfo(); HtmlNode link = node.Descendants("span").First().Descendants("a").First(); newChapter.href = link.Attributes["href"].Value; newChapter.Name = link.InnerText; chapters.Add(newChapter); } chapters.Reverse(); #endregion #region Save Data _name = name; _status = status; _image = imageSource.ToFreezedBitmapImage(); _description = description; _authorList = authors; _genreList = new ObservableCollection <GenreItemViewModel>(); foreach (var genre in genres) { _genreList.Add(new GenreItemViewModel { Text = genre }); } _chapterList = new ObservableCollection <ChapterListItemViewModel>(); for (int i = 0; i < chapters.Count; i++) { _chapterList.Add( new ChapterListItemViewModel(MangaView, i) { Name = chapters[i].Name, URL = chapters[i].href, IsNotDownloaded = true } ); } #endregion }