From f99d38c146f82a8574bd3230ba8fe38a210bf84b Mon Sep 17 00:00:00 2001 From: tobiichiamane Date: Tue, 16 Jun 2020 14:07:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90DownloadManager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PixivFSUWP/Data/DownloadManager.cs | 58 ++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 6 deletions(-) diff --git a/PixivFSUWP/Data/DownloadManager.cs b/PixivFSUWP/Data/DownloadManager.cs index 171e887..9e04ae7 100644 --- a/PixivFSUWP/Data/DownloadManager.cs +++ b/PixivFSUWP/Data/DownloadManager.cs @@ -1,5 +1,7 @@ -using System; +using Lumia.Imaging.Compositing; +using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Text; @@ -17,10 +19,19 @@ public class DownloadCompletedEventArgs : EventArgs public class DownloadJob { - public string Title { get; set; } - public string Uri { get; set; } - public string FilePath { get; set; } - public int Progress { get; set; } + public string Title { get; } + public string Uri { get; } + public string FilePath { get; } + public int Progress { get; private set; } + + public DownloadJob(string Title, string Uri, string FilePath) + { + this.Title = Title; + this.Uri = Uri; + this.FilePath = FilePath; + Progress = 0; + Downloading = false; + } //下载状态 public bool Downloading { get; private set; } @@ -91,7 +102,42 @@ public void Cancel() public static class DownloadManager { //下载任务列表 - static List downloadJobs = new List(); + public static ObservableCollection DownloadJobs = new ObservableCollection(); + + //添加下载任务 + public static void NewJob(string Title, string Uri, string FilePath) + { + var job = new DownloadJob(Title, Uri, FilePath); + job.DownloadCompleted += Job_DownloadCompleted; + DownloadJobs.Add(job); + _ = job.Download(); + } + //有任务下载完成时的事件 + public static event Action DownloadCompleted; + + //下载完成时 + private static void Job_DownloadCompleted(DownloadJob source, DownloadCompletedEventArgs args) + { + DownloadJobs.Remove(source); + DownloadCompleted?.Invoke(source.Title, args.HasError); + } + + //移除下载任务 + public static void RemoveJob(int Index) + { + var job = DownloadJobs[Index]; + job.DownloadCompleted -= Job_DownloadCompleted; + job.Cancel(); + DownloadJobs.Remove(job); + } + + //移除下载任务 + public static void RemoveJob(DownloadJob Job) + { + Job.DownloadCompleted -= Job_DownloadCompleted; + Job.Cancel(); + DownloadJobs.Remove(Job); + } } }