diff --git a/Tranga/TaskManager.cs b/Tranga/TaskManager.cs index 90a9b9e..4d6885d 100644 --- a/Tranga/TaskManager.cs +++ b/Tranga/TaskManager.cs @@ -2,7 +2,7 @@ public class TaskManager { - private readonly Dictionary _chapterCollection; + private Dictionary _chapterCollection; private readonly HashSet _allTasks; private bool _continueRunning = true; @@ -14,18 +14,18 @@ public class TaskManager taskChecker.Start(); } + public void AddTask(Connector connector, TrangaTask.availableTasks task, TimeSpan reoccurrence, Publication? publication = null, string language = "en") + { + this._allTasks.Add(new TrangaTask(connector, task, reoccurrence, publication, language)); + } + private void TaskCheckerThread() { while (_continueRunning) { foreach (TrangaTask task in _allTasks.Where(trangaTask => (DateTime.Now - trangaTask.lastExecuted) > trangaTask.reoccurrence)) { - if (!task.lastExecutedSuccessfully) - { - task.Abort(); - //Add logging that task has failed - } - task.Execute(); + task.Execute(ref _chapterCollection); } Thread.Sleep(1000); } @@ -39,7 +39,7 @@ public class TaskManager public Publication[] GetAddedPublications() { - throw new NotImplementedException(); + return this._chapterCollection.Keys.ToArray(); } public TrangaTask[] GetTasks() diff --git a/Tranga/TrangaTask.cs b/Tranga/TrangaTask.cs index 4aac005..d7ef34a 100644 --- a/Tranga/TrangaTask.cs +++ b/Tranga/TrangaTask.cs @@ -2,63 +2,77 @@ public class TrangaTask { - private readonly Action _taskAction; - private Task? _task; - public bool lastExecutedSuccessfully => _task is not null && _task.IsCompleted; public TimeSpan reoccurrence { get; } public DateTime lastExecuted { get; private set; } - - public TrangaTask(Action taskAction, TimeSpan reoccurrence) + public Connector connector { get; } + public availableTasks availableTaskToExecute { get; } + public enum availableTasks { - this._taskAction = taskAction; + downloadNewChapters, + updateChapters, + updatePublications + }; + public Publication? publication { get; } + public string language { get; } + + + public TrangaTask(Connector connector, availableTasks availableTask, TimeSpan reoccurrence, Publication? publication = null, string language = "en") + { + this.connector = connector; + this.availableTaskToExecute = availableTask; + this.lastExecuted = DateTime.Now.Subtract(reoccurrence); this.reoccurrence = reoccurrence; - } - - public void Abort() - { - if(_task is not null && !_task.IsCompleted) - _task.Dispose(); - } - - public void Execute() - { - lastExecuted = DateTime.Now; - _task = new (_taskAction); - _task.Start(); - } - - public static TrangaTask CreateDownloadChapterTask(Connector connector, Publication publication, Chapter chapter, TimeSpan reoccurrence) - { - void TaskAction() + this.publication = publication; + this.language = language; + if (publication is null && availableTask is availableTasks.updateChapters or availableTasks.downloadNewChapters) { - connector.DownloadChapter(publication, chapter); + if (publication is null) + throw new ArgumentException( + "If task is updateChapters or downloadNewChapters, Argument publication can not be null!"); } - return new TrangaTask(TaskAction, reoccurrence); - } - - public static TrangaTask CreateUpdateChaptersTask(ref Dictionary chapterCollection, Connector connector, Publication publication, string language, TimeSpan reoccurrence) - { - Dictionary pChapterCollection = chapterCollection; - - void TaskAction() - { - Chapter[] chapters = connector.GetChapters(publication, language); - if(pChapterCollection.TryAdd(publication, chapters)) - pChapterCollection[publication] = chapters; - } - return new TrangaTask(TaskAction, reoccurrence); } - public static TrangaTask CreateUpdatePublicationsTask(ref Dictionary chapterCollection, Connector connector, TimeSpan reoccurrence) + public void Execute(ref Dictionary chapterCollection) { - Dictionary pChapterCollection = chapterCollection; - - void TaskAction() + switch (this.availableTaskToExecute) { - Publication[] publications = connector.GetPublications(); - foreach (Publication publication in publications) - pChapterCollection.TryAdd(publication, Array.Empty()); + case availableTasks.updateChapters: + UpdateChapters(ref chapterCollection); + break; + case availableTasks.updatePublications: + UpdatePublications(ref chapterCollection); + break; + case availableTasks.downloadNewChapters: + DownloadNewChapters(UpdateChapters(ref chapterCollection)); + break; } - return new TrangaTask(TaskAction, reoccurrence); + + this.lastExecuted = DateTime.Now; + } + + private Chapter[] UpdateChapters(ref Dictionary chapterCollection) + { + Publication pPublication = (Publication)this.publication!; + Chapter[] presentChapters = chapterCollection[pPublication]; + Chapter[] allChapters = connector.GetChapters(pPublication); + chapterCollection[pPublication] = allChapters; + + Dictionary pChapter = presentChapters.ToDictionary(chapter => chapter.url, chapter => chapter); + Dictionary aChapter = allChapters.ToDictionary(chapter => chapter.url, chapter => chapter); + return aChapter.Except(pChapter).ToDictionary(pair => pair.Key, pair => pair.Value).Values.ToArray(); + } + + private void UpdatePublications(ref Dictionary chapterCollection) + { + Publication[] allPublications = connector.GetPublications(); + foreach(Publication publication in allPublications) + chapterCollection.TryAdd(publication, Array.Empty()); + } + + private void DownloadNewChapters(Chapter[] newChapters) + { + Publication pPublication = (Publication)this.publication!; + foreach(Chapter chapter in newChapters) + connector.DownloadChapter(pPublication, chapter); } } \ No newline at end of file