Exemplo n.º 1
0
        private async Task <string> GetHtmlData(BookCatalog catalog, HttpHelper http)
        {
            string resultHtml = null;

            if (catalog == null)
            {
                return(null);
            }
            try
            {
                if (this.BookEntity.IsLocal)
                {
                    resultHtml = await GetCatafromDatabase(catalog);

                    if (!string.IsNullOrEmpty(resultHtml))
                    {
                        return(resultHtml);
                    }
                }
                resultHtml = await AnalysisContentService.GetHtmlContent(http, catalog.CatalogUrl);
            }
            catch (Exception)
            {
                resultHtml = null;
            }
            return(resultHtml);
        }
Exemplo n.º 2
0
        private async Task <string> GetHtmlData(string catalogUrl, HttpHelper http)
        {
            string html = null;

            try
            {
                html = await AnalysisContentService.GetHtmlContent(http, catalogUrl);
            }
            catch (Exception)
            {
                html = null;
            }
            return(html);
        }
Exemplo n.º 3
0
        private async void OnUpdateCommand()
        {
            var reslut = LocalBookList.FirstOrDefault(p => !string.IsNullOrEmpty(p.UnReadCountData));

            if (reslut != null)
            {
                ToastHeplper.ShowMessage("开始更新。");
            }
            else
            {
                ToastHeplper.ShowMessage("无更新,请稍后再试。");
                IsUpdating = false;
                return;
            }

            IsUpdating = true;
            Task[] tasks = new Task[LocalBookList.Count];

            for (int j = 0; j < LocalBookList.Count; j++)
            {
                var item = LocalBookList[j];
                tasks[j] = Task.Run((Action)(async() =>
                {
                    try
                    {
                        if (item.UnDownloadCatalogList == null || item.UnDownloadCatalogList.Count == 0)
                        {
                            DispatcherHelper.CheckBeginInvokeOnUI(() =>
                            {
                                item.UnReadCountData = "";
                            });
                            return;
                        }
                        var list = new List <BookCatalog>();
                        item.UnDownloadCatalogList.ToList().ForEach(p => list.Add(p));
                        for (int i = 0; i < list.Count; i++)
                        {
                            if (!IsUpdating)
                            {
                                DispatcherHelper.CheckBeginInvokeOnUI(() =>
                                {
                                    item.UnReadCountData = "  " + item.UnDownloadCatalogList.Count.ToString();
                                });
                                break;
                            }

                            DispatcherHelper.CheckBeginInvokeOnUI(() =>
                            {
                                item.UnReadCountData = "正在更新(" + "剩余" + (list.Count - i).ToString() + ")";
                            });

                            string html = await AnalysisContentService.GetHtmlContent(new HttpHelper(), list[i].CatalogUrl);
                            if (!string.IsNullOrEmpty(html))
                            {
                                BookCatalogContent content = new BookCatalogContent()
                                {
                                    BookID = item.BookID,
                                    CatalogUrl = list[i].CatalogUrl,
                                    Content = html
                                };
                                DispatcherHelper.CheckBeginInvokeOnUI(() =>
                                {
                                    item.NewestChapterName = list[i].CatalogName;
                                    item.NewestChapterUrl = list[i].CatalogUrl;
                                    item.UpdateTime = DateTime.Now.ToString();

                                    if (item.CatalogList == null)
                                    {
                                        item.CatalogList = new ObservableCollection <BookCatalog>();
                                    }
                                    item.CatalogList.Add(list[i]);
                                    item.UnDownloadCatalogList.Remove(list[i]);

                                    if (i == list.Count - 1)
                                    {
                                        item.UnReadCountData = null;
                                    }
                                });

                                lock (obj)
                                {
                                    DBBookCatalog.InsertOrUpdateBookCatalog(AppDataPath.GetBookDBPath(item.BookID), list[i]);
                                    DBBookCatalogContent.InsertOrUpdateBookCatalogContent(AppDataPath.GetBookDBPath(item.BookID), content);
                                    DBLocalBook.InsertOrUpdateBookEntity(AppDataPath.GetLocalBookDBPath(), item);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                    }
                }));
            }

            await Task.Factory.ContinueWhenAll(tasks, completedTasks =>
            {
                DispatcherHelper.CheckBeginInvokeOnUI(() =>
                {
                    IsUpdating = false;
                });
            });
        }