Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Commit

Permalink
完成DownloadManager
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiichiamane committed Jun 16, 2020
1 parent f35e5d8 commit f99d38c
Showing 1 changed file with 52 additions and 6 deletions.
58 changes: 52 additions & 6 deletions PixivFSUWP/Data/DownloadManager.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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; }
Expand Down Expand Up @@ -91,7 +102,42 @@ public void Cancel()
public static class DownloadManager
{
//下载任务列表
static List<DownloadJob> downloadJobs = new List<DownloadJob>();
public static ObservableCollection<DownloadJob> DownloadJobs = new ObservableCollection<DownloadJob>();

//添加下载任务
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<string, bool> 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);
}
}
}

0 comments on commit f99d38c

Please sign in to comment.