Ejemplo n.º 1
0
        private async void CheckUpdate()
        {
            if (IsChecking || IsLoading || IsUpdating)
            {
                return;
            }
            DispatcherHelper.CheckBeginInvokeOnUI(() =>
            {
                IsChecking = true;
            });
            if (this.LocalBookList.Count < 1)
            {
                return;
            }

            Task[] tasks = new Task[this.LocalBookList.Count];

            try
            {
                int i = 0;
                foreach (var item in LocalBookList)
                {
                    tasks[i] = Task.Run(async() =>
                    {
                        try
                        {
                            if (!string.IsNullOrEmpty(item.CatalogListUrl))
                            {
                                var result = await AnalysisBookCatalogList.GetCatalogList(item.CatalogListUrl, item.BookID, new HttpHelper());
                                if (!IsChecking)
                                {
                                    return;
                                }

                                if (result.Item1 != null)
                                {
                                    var list = result.Item1;
                                    var last = DBBookCatalog.SelectBookCatalogs(AppDataPath.GetBookDBPath(item.BookID), item.BookID);


                                    var undownLoad = list.FindAll(p => last?.FirstOrDefault(m => m.CatalogUrl == p.CatalogUrl) == null);

                                    if (undownLoad != null && undownLoad.Count > 0)
                                    {
                                        item.UnDownloadCatalogList = new ObservableCollection <BookCatalog>();
                                        undownLoad.ForEach(p => item.UnDownloadCatalogList.Add(p));
                                        DispatcherHelper.CheckBeginInvokeOnUI(() =>
                                        {
                                            item.UnReadCountData = "  " + undownLoad.Count.ToString();
                                        });
                                    }
                                    else
                                    {
                                        DispatcherHelper.CheckBeginInvokeOnUI(() =>
                                        {
                                            item.UnReadCountData = "";
                                        });
                                    }
                                }
                            }
                        }
                        catch (Exception)
                        {
                        }
                    });

                    i++;
                }

                await Task.Factory.ContinueWhenAll(tasks, (obj) =>
                {
                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                    {
                        IsChecking = false;
                    });
                });
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);

                DispatcherHelper.CheckBeginInvokeOnUI(() =>
                {
                    IsChecking = false;
                });
            }
        }
Ejemplo n.º 2
0
        public void GetLocalBook()
        {
            if (IsLoading || IsChecking || IsUpdating)
            {
                if (IsLoading)
                {
                    ToastHeplper.ShowMessage("正在刷新数据,请稍后");
                    return;
                }

                if (IsChecking)
                {
                    ToastHeplper.ShowMessage("正在检测更新,请稍后");
                    return;
                }

                if (IsUpdating)
                {
                    ToastHeplper.ShowMessage("正在更新本地数据,请稍后");
                    return;
                }
            }

            IsLoading = true;
            this.LocalBookList.Clear();
            Task.Run(() =>
            {
                try
                {
                    var result = DBLocalBook.GetAllLocalBookList(AppDataPath.GetLocalBookDBPath());
                    if (result != null)
                    {
                        foreach (var item in result)
                        {
                            string path = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, AppDataPath.LocalBookFolderName, item.BookID + ".db");

                            if (File.Exists(path))
                            {
                                var list = DBBookCatalog.SelectBookCatalogs(AppDataPath.GetBookDBPath(item.BookID), item.BookID);
                                if (list != null && list.Count > 0)
                                {
                                    foreach (var catalog in list)
                                    {
                                        if (item.CatalogList == null)
                                        {
                                            item.CatalogList = new ObservableCollection <BookCatalog>();
                                        }
                                        item.CatalogList.Add(catalog);
                                    }
                                    item.NewestChapterName = list.LastOrDefault().CatalogName;
                                    item.NewestChapterUrl  = list.LastOrDefault().CatalogUrl;
                                }
                                else
                                {
                                    item.NewestChapterName = null;
                                    item.NewestChapterUrl  = null;
                                }
                                DispatcherHelper.CheckBeginInvokeOnUI(() =>
                                {
                                    item.IsLocal = true;
                                    this.LocalBookList.Add(item);
                                });
                            }
                            else
                            {
                                DBLocalBook.DeleteLocalBookByBookID(AppDataPath.GetLocalBookDBPath(), item.BookID);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
                finally
                {
                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                    {
                        IsLoading = false;
                    });
                }
            }).ContinueWith(obj =>
            {
                CheckUpdate();
            });
        }