2023-05-19 14:15:00 +02:00
|
|
|
|
using System.Text.Json;
|
|
|
|
|
|
|
|
|
|
namespace Tranga;
|
2023-05-17 23:23:01 +02:00
|
|
|
|
|
|
|
|
|
public class TaskManager
|
|
|
|
|
{
|
2023-05-19 14:00:30 +02:00
|
|
|
|
private readonly Dictionary<Publication, List<Chapter>> _chapterCollection;
|
2023-05-18 19:25:46 +02:00
|
|
|
|
private readonly HashSet<TrangaTask> _allTasks;
|
2023-05-17 23:23:01 +02:00
|
|
|
|
private bool _continueRunning = true;
|
|
|
|
|
|
|
|
|
|
public TaskManager()
|
|
|
|
|
{
|
2023-05-18 19:25:46 +02:00
|
|
|
|
_chapterCollection = new();
|
2023-05-19 14:15:00 +02:00
|
|
|
|
_allTasks = ImportTasks(Directory.GetCurrentDirectory());
|
2023-05-17 23:23:01 +02:00
|
|
|
|
Thread taskChecker = new(TaskCheckerThread);
|
|
|
|
|
taskChecker.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TaskCheckerThread()
|
|
|
|
|
{
|
|
|
|
|
while (_continueRunning)
|
|
|
|
|
{
|
2023-05-19 14:00:30 +02:00
|
|
|
|
foreach (TrangaTask task in _allTasks.Where(trangaTask => trangaTask.ShouldExecute(true)))
|
2023-05-17 23:23:01 +02:00
|
|
|
|
{
|
2023-05-19 14:00:30 +02:00
|
|
|
|
TaskExecutor.Execute(task, this._chapterCollection);
|
2023-05-17 23:23:01 +02:00
|
|
|
|
}
|
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 14:15:17 +02:00
|
|
|
|
public bool AddTask(TrangaTask.Task task, Connector connector, Publication publication, TimeSpan reoccurrence,
|
|
|
|
|
string language = "")
|
|
|
|
|
{
|
|
|
|
|
if(!_allTasks.Any(trangaTask => trangaTask.task != task && trangaTask.publication.downloadUrl != publication.downloadUrl))
|
|
|
|
|
return _allTasks.Add(new TrangaTask(connector, task, publication, reoccurrence, language));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool RemoveTask(TrangaTask.Task task, Publication publication)
|
|
|
|
|
{
|
|
|
|
|
return (_allTasks.RemoveWhere(trangaTask =>
|
|
|
|
|
trangaTask.task == task && trangaTask.publication.downloadUrl == publication.downloadUrl)
|
|
|
|
|
> 0);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-17 23:23:01 +02:00
|
|
|
|
public void Shutdown()
|
|
|
|
|
{
|
|
|
|
|
_continueRunning = false;
|
2023-05-19 14:15:00 +02:00
|
|
|
|
ExportTasks(Directory.GetCurrentDirectory());
|
2023-05-19 13:59:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 14:15:00 +02:00
|
|
|
|
public HashSet<TrangaTask> ImportTasks(string importFolderPath)
|
2023-05-19 13:59:26 +02:00
|
|
|
|
{
|
2023-05-19 14:15:00 +02:00
|
|
|
|
string filePath = Path.Join(importFolderPath, "tasks.json");
|
|
|
|
|
if (!File.Exists(filePath))
|
|
|
|
|
return new HashSet<TrangaTask>();
|
|
|
|
|
|
|
|
|
|
FileStream file = new FileStream(filePath, FileMode.Open);
|
|
|
|
|
|
|
|
|
|
TrangaTask[] importTasks = JsonSerializer.Deserialize<TrangaTask[]>(file, JsonSerializerOptions.Default)!;
|
|
|
|
|
return importTasks.ToHashSet();
|
2023-05-19 13:59:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 14:15:00 +02:00
|
|
|
|
public void ExportTasks(string exportFolderPath)
|
2023-05-19 13:59:26 +02:00
|
|
|
|
{
|
2023-05-19 14:15:00 +02:00
|
|
|
|
FileStream file = new FileStream(Path.Join(exportFolderPath, "tasks.json"), FileMode.CreateNew);
|
|
|
|
|
JsonSerializer.Serialize(file, _allTasks.ToArray(), JsonSerializerOptions.Default);
|
|
|
|
|
file.Close();
|
|
|
|
|
file.Dispose();
|
2023-05-17 23:23:01 +02:00
|
|
|
|
}
|
|
|
|
|
}
|