diff --git a/Tranga/Connector.cs b/Tranga/Connector.cs index 006597c..3888cf3 100644 --- a/Tranga/Connector.cs +++ b/Tranga/Connector.cs @@ -183,7 +183,7 @@ public abstract class Connector string extension = split[^1]; logger?.WriteLine("Connector", $"Downloading Image {chapter + 1:000}/{imageUrls.Length:000} {parentTask.publication.sortName} {parentTask.publication.internalId} Vol.{parentTask.chapter.volumeNumber} Ch.{parentTask.chapter.chapterNumber} {parentTask.progress:P2}"); DownloadImage(imageUrl, Path.Join(tempFolder, $"{chapter++}.{extension}"), requestType, referrer); - parentTask.progress += 1f / imageUrls.Length; + parentTask.IncrementProgress(1f / imageUrls.Length); } if(comicInfoPath is not null) diff --git a/Tranga/TrangaTask.cs b/Tranga/TrangaTask.cs index 4ccd895..5350b40 100644 --- a/Tranga/TrangaTask.cs +++ b/Tranga/TrangaTask.cs @@ -21,7 +21,7 @@ public abstract class TrangaTask public DateTime lastExecuted { get; set; } public Task task { get; } [Newtonsoft.Json.JsonIgnore]public ExecutionState state { get; set; } - [Newtonsoft.Json.JsonIgnore]public float progress { get; set; } + [Newtonsoft.Json.JsonIgnore]public float progress { get; protected set; } [Newtonsoft.Json.JsonIgnore]public DateTime nextExecution => lastExecuted.Add(reoccurrence); public enum ExecutionState @@ -38,6 +38,12 @@ public abstract class TrangaTask this.task = task; this.progress = 0f; } + + public float IncrementProgress(float amount) + { + this.progress += amount; + return this.progress; + } /// /// BL for concrete Tasks diff --git a/Tranga/TrangaTasks/DownloadChapterTask.cs b/Tranga/TrangaTasks/DownloadChapterTask.cs index 38584c7..30133bb 100644 --- a/Tranga/TrangaTasks/DownloadChapterTask.cs +++ b/Tranga/TrangaTasks/DownloadChapterTask.cs @@ -1,4 +1,5 @@ using Logging; +using Newtonsoft.Json; namespace Tranga.TrangaTasks; @@ -8,12 +9,15 @@ public class DownloadChapterTask : TrangaTask public Publication publication { get; } public string language { get; } public Chapter chapter { get; } - public DownloadChapterTask(Task task, string connectorName, Publication publication, Chapter chapter, string language = "en") : base(task, TimeSpan.Zero) + [JsonIgnore]private DownloadNewChaptersTask? parentTask { get; init; } + + public DownloadChapterTask(Task task, string connectorName, Publication publication, Chapter chapter, string language = "en", DownloadNewChaptersTask? parentTask = null) : base(task, TimeSpan.Zero) { this.chapter = chapter; this.connectorName = connectorName; this.publication = publication; this.language = language; + this.parentTask = parentTask; } protected override void ExecuteTask(TaskManager taskManager, Logger? logger) @@ -24,6 +28,13 @@ public class DownloadChapterTask : TrangaTask taskManager.DeleteTask(this); } + public new float IncrementProgress(float amount) + { + this.progress += amount; + parentTask?.IncrementProgress(amount); + return this.progress; + } + public override string ToString() { return $"{base.ToString()}, {connectorName}, {publication.sortName} {publication.internalId}, Vol.{chapter.volumeNumber} Ch.{chapter.chapterNumber}"; diff --git a/Tranga/TrangaTasks/DownloadNewChaptersTask.cs b/Tranga/TrangaTasks/DownloadNewChaptersTask.cs index ec1e15a..2f5f2d5 100644 --- a/Tranga/TrangaTasks/DownloadNewChaptersTask.cs +++ b/Tranga/TrangaTasks/DownloadNewChaptersTask.cs @@ -1,4 +1,5 @@ using Logging; +using Newtonsoft.Json; namespace Tranga.TrangaTasks; @@ -7,34 +8,38 @@ public class DownloadNewChaptersTask : TrangaTask public string connectorName { get; } public Publication publication { get; } public string language { get; } + [JsonIgnore]private int childTaskAmount { get; set; } + public DownloadNewChaptersTask(Task task, string connectorName, Publication publication, TimeSpan reoccurrence, string language = "en") : base(task, reoccurrence) { this.connectorName = connectorName; this.publication = publication; this.language = language; + childTaskAmount = 0; + } + + public new float IncrementProgress(float amount) + { + this.progress += amount / this.childTaskAmount; + return this.progress; } protected override void ExecuteTask(TaskManager taskManager, Logger? logger) { Publication pub = publication!; Connector connector = taskManager.GetConnector(this.connectorName); - this.progress = 0.1f; //Check if Publication already has a Folder pub.CreatePublicationFolder(taskManager.settings.downloadLocation); - this.progress = 0.2f; List newChapters = GetNewChaptersList(connector, pub, language!, ref taskManager.chapterCollection); - this.progress = 0.6f; + this.childTaskAmount = newChapters.Count; connector.CopyCoverFromCacheToDownloadLocation(pub, taskManager.settings); - this.progress = 0.7f; pub.SaveSeriesInfoJson(connector.downloadLocation); - this.progress = 0.8f; foreach (Chapter newChapter in newChapters) - taskManager.AddTask(new DownloadChapterTask(Task.DownloadChapter, this.connectorName!, pub, newChapter, this.language)); - this.progress = 1f; + taskManager.AddTask(new DownloadChapterTask(Task.DownloadChapter, this.connectorName!, pub, newChapter, this.language, this)); } ///