From 391efcb9bc04b3e4f6abe94428a2ef57b2349eb1 Mon Sep 17 00:00:00 2001 From: glax Date: Thu, 2 Nov 2023 15:18:41 +0100 Subject: [PATCH] Add Field jobType to Job --- Tranga/Jobs/DownloadChapter.cs | 4 ++-- Tranga/Jobs/DownloadNewChapters.cs | 4 ++-- Tranga/Jobs/Job.cs | 9 +++++++-- Tranga/Jobs/JobJsonConverter.cs | 19 +++++++++++++++---- Tranga/Jobs/UpdateMetadata.cs | 1 + 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/Tranga/Jobs/DownloadChapter.cs b/Tranga/Jobs/DownloadChapter.cs index b13503c..703ca03 100644 --- a/Tranga/Jobs/DownloadChapter.cs +++ b/Tranga/Jobs/DownloadChapter.cs @@ -7,12 +7,12 @@ public class DownloadChapter : Job { public Chapter chapter { get; init; } - public DownloadChapter(GlobalBase clone, MangaConnector connector, Chapter chapter, DateTime lastExecution, string? parentJobId = null) : base(clone, connector, lastExecution, parentJobId: parentJobId) + public DownloadChapter(GlobalBase clone, MangaConnector connector, Chapter chapter, DateTime lastExecution, string? parentJobId = null) : base(clone, JobType.DownloadChapterJob, connector, lastExecution, parentJobId: parentJobId) { this.chapter = chapter; } - public DownloadChapter(GlobalBase clone, MangaConnector connector, Chapter chapter, string? parentJobId = null) : base(clone, connector, parentJobId: parentJobId) + public DownloadChapter(GlobalBase clone, MangaConnector connector, Chapter chapter, string? parentJobId = null) : base(clone, JobType.DownloadChapterJob, connector, parentJobId: parentJobId) { this.chapter = chapter; } diff --git a/Tranga/Jobs/DownloadNewChapters.cs b/Tranga/Jobs/DownloadNewChapters.cs index 8f02c69..8544fa5 100644 --- a/Tranga/Jobs/DownloadNewChapters.cs +++ b/Tranga/Jobs/DownloadNewChapters.cs @@ -8,14 +8,14 @@ public class DownloadNewChapters : Job public string translatedLanguage { get; init; } public DownloadNewChapters(GlobalBase clone, MangaConnector connector, Manga manga, DateTime lastExecution, - bool recurring = false, TimeSpan? recurrence = null, string? parentJobId = null, string translatedLanguage = "en") : base(clone, connector, lastExecution, recurring, + bool recurring = false, TimeSpan? recurrence = null, string? parentJobId = null, string translatedLanguage = "en") : base(clone, JobType.DownloadNewChaptersJob, connector, lastExecution, recurring, recurrence, parentJobId) { this.manga = manga; this.translatedLanguage = translatedLanguage; } - public DownloadNewChapters(GlobalBase clone, MangaConnector connector, Manga manga, bool recurring = false, TimeSpan? recurrence = null, string? parentJobId = null, string translatedLanguage = "en") : base (clone, connector, recurring, recurrence, parentJobId) + public DownloadNewChapters(GlobalBase clone, MangaConnector connector, Manga manga, bool recurring = false, TimeSpan? recurrence = null, string? parentJobId = null, string translatedLanguage = "en") : base (clone, JobType.DownloadNewChaptersJob, connector, recurring, recurrence, parentJobId) { this.manga = manga; this.translatedLanguage = translatedLanguage; diff --git a/Tranga/Jobs/Job.cs b/Tranga/Jobs/Job.cs index 0f0fc5d..d476d98 100644 --- a/Tranga/Jobs/Job.cs +++ b/Tranga/Jobs/Job.cs @@ -13,9 +13,13 @@ public abstract class Job : GlobalBase public string id => GetId(); internal IEnumerable? subJobs { get; private set; } public string? parentJobId { get; init; } + public enum JobType : byte { DownloadChapterJob, DownloadNewChaptersJob, UpdateMetaDataJob } - internal Job(GlobalBase clone, MangaConnector connector, bool recurring = false, TimeSpan? recurrenceTime = null, string? parentJobId = null) : base(clone) + public JobType jobType; + + internal Job(GlobalBase clone, JobType jobType, MangaConnector connector, bool recurring = false, TimeSpan? recurrenceTime = null, string? parentJobId = null) : base(clone) { + this.jobType = jobType; this.mangaConnector = connector; this.progressToken = new ProgressToken(0); this.recurring = recurring; @@ -27,9 +31,10 @@ public abstract class Job : GlobalBase this.parentJobId = parentJobId; } - internal Job(GlobalBase clone, MangaConnector connector, DateTime lastExecution, bool recurring = false, + internal Job(GlobalBase clone, JobType jobType, MangaConnector connector, DateTime lastExecution, bool recurring = false, TimeSpan? recurrenceTime = null, string? parentJobId = null) : base(clone) { + this.jobType = jobType; this.mangaConnector = connector; this.progressToken = new ProgressToken(0); this.recurring = recurring; diff --git a/Tranga/Jobs/JobJsonConverter.cs b/Tranga/Jobs/JobJsonConverter.cs index 794075e..eaca679 100644 --- a/Tranga/Jobs/JobJsonConverter.cs +++ b/Tranga/Jobs/JobJsonConverter.cs @@ -23,7 +23,20 @@ public class JobJsonConverter : JsonConverter public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) { JObject jo = JObject.Load(reader); - if (jo.ContainsKey("manga"))//DownloadNewChapters + + if (jo.ContainsKey("jobType") && jo["jobType"]!.Value() == (byte)Job.JobType.UpdateMetaDataJob) + { + return new UpdateMetadata(this._clone, + jo.GetValue("mangaConnector")!.ToObject(JsonSerializer.Create(new JsonSerializerSettings() + { + Converters = + { + this._mangaConnectorJsonConverter + } + }))!, + jo.GetValue("manga")!.ToObject(), + jo.GetValue("parentJobId")!.Value()); + }else if ((jo.ContainsKey("jobType") && jo["jobType"]!.Value() == (byte)Job.JobType.DownloadNewChaptersJob) || jo.ContainsKey("translatedLanguage"))//TODO change to jobType { return new DownloadNewChapters(this._clone, jo.GetValue("mangaConnector")!.ToObject(JsonSerializer.Create(new JsonSerializerSettings() @@ -38,9 +51,7 @@ public class JobJsonConverter : JsonConverter jo.GetValue("recurring")!.Value(), jo.GetValue("recurrenceTime")!.ToObject(), jo.GetValue("parentJobId")!.Value()); - } - - if (jo.ContainsKey("chapter"))//DownloadChapter + }else if ((jo.ContainsKey("jobType") && jo["jobType"]!.Value() == (byte)Job.JobType.DownloadChapterJob) || jo.ContainsKey("chapter"))//TODO change to jobType { return new DownloadChapter(this._clone, jo.GetValue("mangaConnector")!.ToObject(JsonSerializer.Create(new JsonSerializerSettings() diff --git a/Tranga/Jobs/UpdateMetadata.cs b/Tranga/Jobs/UpdateMetadata.cs index 2dbb625..93ef394 100644 --- a/Tranga/Jobs/UpdateMetadata.cs +++ b/Tranga/Jobs/UpdateMetadata.cs @@ -14,6 +14,7 @@ public class UpdateMetadata : Job } public UpdateMetadata(GlobalBase clone, MangaConnector connector, Manga manga, JobBoss jobBoss, string? parentJobId = null) : base(clone, connector, parentJobId: parentJobId) + public UpdateMetadata(GlobalBase clone, MangaConnector connector, Manga manga, string? parentJobId = null) : base(clone, JobType.UpdateMetaDataJob, connector, parentJobId: parentJobId) { this.manga = manga; this.jobBoss = jobBoss;