2023-05-18 21:08:09 +02:00
|
|
|
|
namespace Tranga;
|
2023-05-17 23:23:01 +02:00
|
|
|
|
|
|
|
|
|
public class TrangaTask
|
|
|
|
|
{
|
2023-05-18 21:08:09 +02:00
|
|
|
|
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)
|
2023-05-17 23:23:01 +02:00
|
|
|
|
{
|
2023-05-18 21:08:09 +02:00
|
|
|
|
this._taskAction = taskAction;
|
|
|
|
|
this.reoccurrence = reoccurrence;
|
|
|
|
|
}
|
2023-05-17 23:23:01 +02:00
|
|
|
|
|
2023-05-18 21:08:09 +02:00
|
|
|
|
public void Abort()
|
|
|
|
|
{
|
|
|
|
|
if(_task is not null && !_task.IsCompleted)
|
|
|
|
|
_task.Dispose();
|
|
|
|
|
}
|
2023-05-17 23:23:01 +02:00
|
|
|
|
|
2023-05-18 21:08:09 +02:00
|
|
|
|
public void Execute()
|
2023-05-17 23:23:01 +02:00
|
|
|
|
{
|
2023-05-18 21:08:09 +02:00
|
|
|
|
lastExecuted = DateTime.Now;
|
|
|
|
|
_task = new (_taskAction);
|
|
|
|
|
_task.Start();
|
2023-05-17 23:23:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-18 21:08:09 +02:00
|
|
|
|
public static TrangaTask CreateDownloadChapterTask(Connector connector, Publication publication, Chapter chapter, TimeSpan reoccurrence)
|
2023-05-17 23:23:01 +02:00
|
|
|
|
{
|
2023-05-18 21:08:09 +02:00
|
|
|
|
void TaskAction()
|
2023-05-17 23:23:01 +02:00
|
|
|
|
{
|
2023-05-18 21:08:09 +02:00
|
|
|
|
connector.DownloadChapter(publication, chapter);
|
2023-05-17 23:23:01 +02:00
|
|
|
|
}
|
2023-05-18 21:08:09 +02:00
|
|
|
|
return new TrangaTask(TaskAction, reoccurrence);
|
2023-05-17 23:23:01 +02:00
|
|
|
|
}
|
2023-05-18 21:08:09 +02:00
|
|
|
|
|
|
|
|
|
public static TrangaTask CreateUpdateChaptersTask(ref Dictionary<Publication, Chapter[]> chapterCollection, Connector connector, Publication publication, string language, TimeSpan reoccurrence)
|
2023-05-17 23:23:01 +02:00
|
|
|
|
{
|
2023-05-18 21:08:09 +02:00
|
|
|
|
Dictionary<Publication, Chapter[]> pChapterCollection = chapterCollection;
|
2023-05-18 19:12:17 +02:00
|
|
|
|
|
2023-05-18 21:08:09 +02:00
|
|
|
|
void TaskAction()
|
|
|
|
|
{
|
|
|
|
|
Chapter[] chapters = connector.GetChapters(publication, language);
|
|
|
|
|
if(pChapterCollection.TryAdd(publication, chapters))
|
|
|
|
|
pChapterCollection[publication] = chapters;
|
|
|
|
|
}
|
|
|
|
|
return new TrangaTask(TaskAction, reoccurrence);
|
2023-05-17 23:23:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-18 21:08:09 +02:00
|
|
|
|
public static TrangaTask CreateUpdatePublicationsTask(ref Dictionary<Publication, Chapter[]> chapterCollection, Connector connector, TimeSpan reoccurrence)
|
2023-05-17 23:23:01 +02:00
|
|
|
|
{
|
2023-05-18 21:08:09 +02:00
|
|
|
|
Dictionary<Publication, Chapter[]> pChapterCollection = chapterCollection;
|
2023-05-18 19:12:17 +02:00
|
|
|
|
|
2023-05-18 21:08:09 +02:00
|
|
|
|
void TaskAction()
|
|
|
|
|
{
|
|
|
|
|
Publication[] publications = connector.GetPublications();
|
|
|
|
|
foreach (Publication publication in publications)
|
|
|
|
|
pChapterCollection.TryAdd(publication, Array.Empty<Chapter>());
|
|
|
|
|
}
|
|
|
|
|
return new TrangaTask(TaskAction, reoccurrence);
|
2023-05-17 23:23:01 +02:00
|
|
|
|
}
|
|
|
|
|
}
|