2023-06-05 00:35:57 +02:00
|
|
|
|
using Logging;
|
2023-06-06 20:54:21 +02:00
|
|
|
|
using Newtonsoft.Json;
|
2023-06-05 00:35:57 +02:00
|
|
|
|
|
|
|
|
|
namespace Tranga.TrangaTasks;
|
|
|
|
|
|
|
|
|
|
public class DownloadChapterTask : TrangaTask
|
|
|
|
|
{
|
|
|
|
|
public string connectorName { get; }
|
|
|
|
|
public Publication publication { get; }
|
|
|
|
|
public string language { get; }
|
|
|
|
|
public Chapter chapter { get; }
|
2023-06-07 00:24:58 +02:00
|
|
|
|
[JsonIgnore]public DownloadNewChaptersTask? parentTask { get; init; }
|
2023-06-06 20:54:21 +02:00
|
|
|
|
|
|
|
|
|
public DownloadChapterTask(Task task, string connectorName, Publication publication, Chapter chapter, string language = "en", DownloadNewChaptersTask? parentTask = null) : base(task, TimeSpan.Zero)
|
2023-06-05 00:35:57 +02:00
|
|
|
|
{
|
|
|
|
|
this.chapter = chapter;
|
|
|
|
|
this.connectorName = connectorName;
|
|
|
|
|
this.publication = publication;
|
|
|
|
|
this.language = language;
|
2023-06-06 20:54:21 +02:00
|
|
|
|
this.parentTask = parentTask;
|
2023-06-05 00:35:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-10 14:27:09 +02:00
|
|
|
|
protected override void ExecuteTask(TaskManager taskManager, Logger? logger, CancellationToken? cancellationToken = null)
|
2023-06-05 00:35:57 +02:00
|
|
|
|
{
|
2023-06-10 14:27:09 +02:00
|
|
|
|
if (cancellationToken?.IsCancellationRequested??false)
|
|
|
|
|
return;
|
2023-06-10 15:04:37 +02:00
|
|
|
|
if(this.parentTask is not null)
|
|
|
|
|
this.parentTask.state = ExecutionState.Running;
|
2023-06-05 00:35:57 +02:00
|
|
|
|
Connector connector = taskManager.GetConnector(this.connectorName);
|
2023-06-10 14:27:09 +02:00
|
|
|
|
connector.DownloadChapter(this.publication, this.chapter, this, cancellationToken);
|
2023-06-10 15:04:37 +02:00
|
|
|
|
if(this.parentTask is not null)
|
|
|
|
|
this.parentTask.state = ExecutionState.Waiting;
|
2023-06-05 00:35:57 +02:00
|
|
|
|
taskManager.DeleteTask(this);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-06 20:54:21 +02:00
|
|
|
|
public new float IncrementProgress(float amount)
|
|
|
|
|
{
|
|
|
|
|
this.progress += amount;
|
2023-06-07 00:27:53 +02:00
|
|
|
|
this.lastChange = DateTime.Now;
|
2023-06-06 20:54:21 +02:00
|
|
|
|
parentTask?.IncrementProgress(amount);
|
|
|
|
|
return this.progress;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-05 00:35:57 +02:00
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return $"{base.ToString()}, {connectorName}, {publication.sortName} {publication.internalId}, Vol.{chapter.volumeNumber} Ch.{chapter.chapterNumber}";
|
|
|
|
|
}
|
|
|
|
|
}
|