From cbdd305b69ca34ae92a3821f8bd8c1952c967b8f Mon Sep 17 00:00:00 2001 From: glax Date: Tue, 27 Jun 2023 22:59:23 +0200 Subject: [PATCH] TaskManager AddTask make better use of GetTasksMatching and GetTasksMatching easier usage --- Tranga/TaskManager.cs | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/Tranga/TaskManager.cs b/Tranga/TaskManager.cs index 5bbe9a6..a246251 100644 --- a/Tranga/TaskManager.cs +++ b/Tranga/TaskManager.cs @@ -123,32 +123,27 @@ public class TaskManager public void AddTask(TrangaTask newTask) { - logger?.WriteLine(this.GetType().ToString(), $"Adding new Task {newTask}"); - switch (newTask.task) { case TrangaTask.Task.UpdateLibraries: //Only one UpdateKomgaLibrary Task logger?.WriteLine(this.GetType().ToString(), $"Replacing old {newTask.task}-Task."); - _allTasks.RemoveWhere(trangaTask => trangaTask.task is TrangaTask.Task.UpdateLibraries); + if (GetTasksMatching(newTask).FirstOrDefault() is { } exists) + _allTasks.Remove(exists); _allTasks.Add(newTask); + ExportDataAndSettings(); break; - case TrangaTask.Task.MonitorPublication: - MonitorPublicationTask mpt = (MonitorPublicationTask)newTask; - if(!GetTasksMatching(mpt.task, mpt.connectorName, internalId:mpt.publication.internalId).Any()) - _allTasks.Add(newTask); - else - logger?.WriteLine(this.GetType().ToString(), $"Task already exists {newTask}"); - break; - case TrangaTask.Task.DownloadChapter: - DownloadChapterTask dct = (DownloadChapterTask)newTask; - if(!GetTasksMatching(dct.task, dct.connectorName, internalId:dct.publication.internalId, chapterSortNumber:dct.chapter.sortNumber).Any()) + default: + if (!GetTasksMatching(newTask).Any()) + { + logger?.WriteLine(this.GetType().ToString(), $"Adding new Task {newTask}"); _allTasks.Add(newTask); + ExportDataAndSettings(); + } else logger?.WriteLine(this.GetType().ToString(), $"Task already exists {newTask}"); break; } - ExportDataAndSettings(); } public void DeleteTask(TrangaTask removeTask) @@ -166,6 +161,24 @@ public class TaskManager DeleteTask(childTask); } + public IEnumerable GetTasksMatching(TrangaTask mTask) + { + switch (mTask.task) + { + case TrangaTask.Task.UpdateLibraries: + return GetTasksMatching(TrangaTask.Task.UpdateLibraries); + case TrangaTask.Task.DownloadChapter: + DownloadChapterTask dct = (DownloadChapterTask)mTask; + return GetTasksMatching(TrangaTask.Task.DownloadChapter, connectorName: dct.connectorName, + internalId: dct.publication.internalId); + case TrangaTask.Task.MonitorPublication: + MonitorPublicationTask mpt = (MonitorPublicationTask)mTask; + return GetTasksMatching(TrangaTask.Task.MonitorPublication, connectorName: mpt.connectorName, + internalId: mpt.publication.internalId); + } + return Array.Empty(); + } + public IEnumerable GetTasksMatching(TrangaTask.Task taskType, string? connectorName = null, string? searchString = null, string? internalId = null, string? chapterSortNumber = null) { switch (taskType)