示例#1
0
        private async void DeleteDownLoadItem(DowmLoadEntity entity)
        {
            entity.IsPause = true;
            await Task.Delay(800);

            if (entity.IsCompleted)
            {
                ToastHeplper.ShowMessage(entity.Entity.BookName + "已下载完毕,正在处理数据,无法取消");
                return;
            }
            this.DownLoadList.Remove(entity);
            try
            {
                string path = Path.Combine(AppDataPath.GetLocalBookFolderPath(), entity.Entity.BookID + ".db");
                if (File.Exists(path))
                {
                    System.IO.File.Delete(AppDataPath.GetBookDBPath(entity.Entity.BookID));
                }
            }
            catch (Exception)
            {
            }
            if (this.DownLoadList.Count == 0)
            {
                IsDownLoading = false;
            }
        }
示例#2
0
        private void OnPauseCommand(object obj)
        {
            DowmLoadEntity entity = obj as DowmLoadEntity;

            if (entity == null)
            {
                return;
            }
            if (!entity.IsPause)
            {
                entity.IsPause     = true;
                this.IsDownLoading = false;
                foreach (var item in DownLoadList)
                {
                    if (entity.IsPause == false)
                    {
                        this.IsDownLoading = true;
                        break;
                    }
                }
            }
            else
            {
                entity.IsPause = false;

                StartNewCommonDownLoadInstance(entity);
            }
        }
示例#3
0
        public async void AddNewDownloadItem(BookEntity entity)
        {
            if (this.DownLoadList.ToList().FirstOrDefault(p => p.Entity.BookID == entity.BookID) != null)
            {
                ToastHeplper.ShowMessage("已经在下载队列中");
                return;
            }

            if (this.DownLoadList.Count == maxDownloadCount)
            {
                ToastHeplper.ShowMessage("最多同时下载" + maxDownloadCount + "本");
                return;
            }

            DowmLoadEntity temp = new DowmLoadEntity();

            temp.Entity = entity.Clone();

            //无网络
            if (NetworkHelper.Current.Network == 4)
            {
                ToastHeplper.ShowMessage("无网络连接,请检查网络后重试");
                return;
            }
            //wifi
            else if (NetworkHelper.Current.Network == 3)
            {
                StartNew(temp);
            }
            //流量
            else
            {
                if (!ViewModelInstance.Instance.SettingPageViewModelInstance.IfDownloadInWAAN)
                {
                    var msgDialog = new Windows.UI.Popups.MessageDialog("你现在使用的是手机流量,确定下载?\n你可以在设置中取消此提示。")
                    {
                        Title = "使用流量下载"
                    };
                    msgDialog.Commands.Add(new Windows.UI.Popups.UICommand("我是土豪", uiCommand =>
                    {
                        StartNew(temp);
                    }));
                    msgDialog.Commands.Add(new Windows.UI.Popups.UICommand("还是算了吧", uiCommand =>
                    {
                        return;
                    }));
                    await msgDialog.ShowAsync();
                }
                else
                {
                    StartNew(temp);
                }
            }
        }
示例#4
0
        private async void OnDeleteCommand(object obj)
        {
            DowmLoadEntity entity = obj as DowmLoadEntity;

            if (entity == null)
            {
                return;
            }

            var msgDialog = new Windows.UI.Popups.MessageDialog("\n确定取消下载 " + entity.Entity.BookName + "?")
            {
                Title = "取消下载"
            };

            msgDialog.Commands.Add(new Windows.UI.Popups.UICommand("确定", uiCommand =>
            {
                DeleteDownLoadItem(entity);
            }));
            msgDialog.Commands.Add(new Windows.UI.Popups.UICommand("取消", uiCommand =>
            {
                return;
            }));
            await msgDialog.ShowAsync();
        }
示例#5
0
        public async void StartNewFastDownLoadInstance(DowmLoadEntity temp)
        {
            Stopwatch watch = new Stopwatch();

            watch.Start();
            IsDownLoading = true;

            int count = 10;

            temp.IsFast = true;

            if (temp.Entity.CatalogList == null || temp.Entity.CatalogList.Count == 0)
            {
                return;
            }
            temp.ContentList = new List <BookCatalogContent>();

            count = temp.Entity.CatalogList.Count >= count ? count : temp.Entity.CatalogList.Count;
            Task[] tasks = new Task[count];

            var groups = Split <BookCatalog>(temp.Entity.CatalogList, count);
            int index  = 0;

            if (!string.IsNullOrEmpty(temp.Entity.Cover))
            {
                try
                {
                    StorageFolder storageFolder = await StorageFolder.GetFolderFromPathAsync(AppDataPath.GetLocalBookCoverFolderPath());

                    StorageFile file     = null;
                    string      filename = temp.Entity.BookID + ".jpg";
                    if (!File.Exists(filename))
                    {
                        file = await storageFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
                    }
                    file = await storageFolder.GetFileAsync(filename);

                    using (var stream = await file.OpenReadAsync())
                    {
                        if (stream.Size <= 0)
                        {
                            var tmpfile = await storageFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);

                            var http = new HttpClient();
                            var data = await http.GetByteArrayAsync(temp.Entity.Cover);

                            await FileIO.WriteBytesAsync(tmpfile, data);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(temp.Entity.BookName + "  下载封面失败");
                }
            }

            try
            {
                foreach (var list in groups)
                {
                    tasks[index] = await Task.Factory.StartNew(async() =>
                    {
                        HttpHelper http = new HttpHelper();
                        foreach (var catalog in list)
                        {
                            if (temp.IsPause)
                            {
                                return;
                            }
                            if (string.IsNullOrEmpty(catalog.CatalogUrl))
                            {
                                continue;
                            }
                            try
                            {
                                string html = await GetHtmlData(catalog.CatalogUrl, http);

                                if (string.IsNullOrEmpty(html))
                                {
                                    html = GetHtmlData(catalog.CatalogUrl, http).Result;
                                }
                                if (string.IsNullOrEmpty(html))
                                {
                                    Debug.WriteLine("*******下载失败****** :" + temp.Entity.BookName + "  " + catalog.CatalogName + "   " + catalog.CatalogUrl);
                                }
                                else
                                {
                                    Debug.WriteLine("下载完成 :" + temp.Entity.BookName + "  " + catalog.CatalogName + "   " + catalog.CatalogUrl);
                                }

                                BookCatalogContent content = new BookCatalogContent()
                                {
                                    BookID     = catalog.BookID,
                                    CatalogUrl = catalog.CatalogUrl,
                                    Content    = html,
                                };
                                temp.ContentList.Add(content);


                                if (IsFrameContent)
                                {
                                    DispatcherHelper.CheckBeginInvokeOnUI(temp.SetProcessValue);

                                    //await Task.Factory.StartNew((obj) =>
                                    //{
                                    //    temp.SetProcessValue();
                                    //}, null, new CancellationTokenSource().Token, TaskCreationOptions.None, _syncContextTaskScheduler);
                                }
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine(ex.Message);
                                continue;
                            }
                        }
                    });

                    index++;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                temp.IsPause = true;
            }


            await Task.Factory.ContinueWhenAll(tasks, (obj) =>
            {
                if (temp.IsPause)
                {
                    if (DownLoadList.Contains(temp))
                    {
                        DispatcherHelper.CheckBeginInvokeOnUI(() =>
                        {
                            this.DownLoadList.Remove(temp);
                        });
                    }

                    if (File.Exists(AppDataPath.GetLocalBookCoverPath(temp.Entity.BookID)))
                    {
                        File.Delete(AppDataPath.GetLocalBookCoverPath(temp.Entity.BookID));
                    }
                    return;
                }
                DispatcherHelper.CheckBeginInvokeOnUI(() =>
                {
                    temp.SetProcessValue();
                    temp.Entity.LastReadChapterName = null;
                    temp.Entity.LastReadChapterUrl  = null;
                    temp.Entity.IsLocal             = true;

                    var catalog = temp.Entity.CatalogList.OrderBy(p => p.Index).ToList().LastOrDefault();
                    if (catalog != null)
                    {
                        temp.Entity.NewestChapterName = catalog.CatalogName;
                        temp.Entity.NewestChapterUrl  = catalog.CatalogUrl;
                    }
                });

                DBBookCatalogContent.InsertOrUpdateBookCatalogContents(AppDataPath.GetBookDBPath(temp.Entity.BookID), temp.ContentList);
                DBBookCatalog.InsertOrUpdateBookCatalogs(AppDataPath.GetBookDBPath(temp.Entity.BookID), temp.Entity.CatalogList.ToList());
                lock (isAdd1)
                {
                    DBLocalBook.InsertOrUpdateBookEntity(AppDataPath.GetLocalBookDBPath(), temp.Entity);
                }

                try
                {
                    DispatcherHelper.CheckBeginInvokeOnUI(() =>
                    {
                        this.DownLoadList.Remove(temp);
                        ViewModelInstance.Instance.LocalBookPage.LocalBookList.Add(temp.Entity);
                        ToastHeplper.ShowMessage(temp.Entity.BookName + "下载完毕,点击“本地图书”查看");
                    });
                }
                catch (Exception ex)
                {
                    this.DownLoadList.Remove(temp);
                    ToastHeplper.ShowMessage(temp.Entity.BookName + "下载失败");
                    Debug.WriteLine(ex.Message);
                }
                finally
                {
                    if (this.DownLoadList.Count == 0)
                    {
                        this.IsDownLoading = false;
                    }
                }

                watch.Stop();
                Debug.WriteLine("共用时:" + watch.Elapsed.TotalSeconds);
            });
        }
示例#6
0
        public void StartNewCommonDownLoadInstance(DowmLoadEntity temp)
        {
            bool result = false;

            IsDownLoading = true;
            temp.IsFast   = false;

            Task task = Task.Run(async() =>
            {
                try
                {
                    int startIndex = 0;
                    //适用于暂停然后重新开始
                    if (temp.CurrentCatalog != null)
                    {
                        if (temp.Entity.CatalogList != null && temp.Entity.CatalogList.Count > 0)
                        {
                            var catalog = temp.Entity.CatalogList.FirstOrDefault(p => p.CatalogUrl == temp.CurrentCatalog.CatalogUrl);
                            if (catalog != null)
                            {
                                startIndex = temp.Entity.CatalogList.IndexOf(catalog);
                            }
                        }
                    }

                    BookEntity entity = temp.Entity;
                    int count         = temp.Entity.CatalogList.Count;
                    HttpHelper http   = new HttpHelper();
                    for (int i = startIndex; i < count; i++)
                    {
                        if (temp.IsPause)
                        {
                            // break;
                            return;
                        }
                        try
                        {
                            var item = temp.Entity.CatalogList[i];
                            DispatcherHelper.CheckBeginInvokeOnUI(() =>
                            {
                                temp.CurrentCatalog = item;
                                temp.CurrentIndex   = i + 1;
                                temp.ProgressValue  = Math.Round(((double)item.Index / (double)temp.Entity.CatalogList.Count), 3) * 100;
                            });

                            string html = await GetHtmlData(item.CatalogUrl, http);

                            BookCatalogContent content = new BookCatalogContent()
                            {
                                BookID     = item.BookID,
                                CatalogUrl = item.CatalogUrl,
                                Content    = html
                            };

                            lock (isAdd1)
                            {
                                if (!string.IsNullOrEmpty(item.CatalogUrl))
                                {
                                    DBBookCatalog.InsertOrUpdateBookCatalog(AppDataPath.GetBookDBPath(temp.Entity.BookID), item);
                                }
                                if (!string.IsNullOrEmpty(content.CatalogUrl))
                                {
                                    DBBookCatalogContent.InsertOrUpdateBookCatalogContent(AppDataPath.GetBookDBPath(temp.Entity.BookID), content);
                                }
                            }

                            DispatcherHelper.CheckBeginInvokeOnUI(() =>
                            {
                                temp.Entity.NewestChapterName = item.CatalogName;
                                temp.Entity.NewestChapterUrl  = item.CatalogUrl;
                            });
                        }
                        catch (Exception)
                        {
                            continue;
                        }
                    }
                    result = true;
                }
                catch (Exception)
                {
                    result = false;
                }
                finally
                {
                    if (result)
                    {
                        DispatcherHelper.CheckBeginInvokeOnUI(() =>
                        {
                            lock (isAdd2)
                            {
                                temp.Entity.LastReadChapterName = null;
                                temp.Entity.LastReadChapterUrl  = null;
                                DBLocalBook.InsertOrUpdateBookEntity(AppDataPath.GetLocalBookDBPath(), temp.Entity);
                            }
                            this.DownLoadList.Remove(temp);
                            if (this.DownLoadList.Count == 0)
                            {
                                this.IsDownLoading = false;
                            }
                            temp.Entity.IsLocal = true;
                            ViewModelInstance.Instance.LocalBookPage.LocalBookList.Add(temp.Entity);
                            ToastHeplper.ShowMessage(temp.Entity.BookName + "下载完毕,点击“本地图书”查看");
                        });
                    }
                    else
                    {
                        DispatcherHelper.CheckBeginInvokeOnUI(() =>
                        {
                            if (!temp.IsPause)
                            {
                                this.DownLoadList.Remove(temp);
                                ToastHeplper.ShowMessage(temp.Entity.BookName + "下载失败");
                            }
                            else
                            {
                                //  Services.ToastHeplper.ShowMessage(temp.Entity.BookName + "下载已暂停");
                            }
                        });
                    }
                }
            });
        }
示例#7
0
 private void StartNew(DowmLoadEntity temp)
 {
     ToastHeplper.ShowMessage("开始下载图书,请耐心等待。");
     this.DownLoadList.Add(temp);
     StartNewFastDownLoadInstance(temp);
 }