Library- and NotificationConnectors in GlobalBase
This commit is contained in:
parent
11461051f3
commit
8f309fcfd7
@ -1,4 +1,7 @@
|
||||
using Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Tranga.LibraryConnectors;
|
||||
using Tranga.NotificationConnectors;
|
||||
|
||||
namespace Tranga;
|
||||
|
||||
@ -6,17 +9,23 @@ public abstract class GlobalBase
|
||||
{
|
||||
protected Logger? logger { get; init; }
|
||||
protected TrangaSettings settings { get; init; }
|
||||
private HashSet<NotificationConnector> notificationConnectors { get; init; }
|
||||
private HashSet<LibraryConnector> libraryConnectors { get; init; }
|
||||
|
||||
public GlobalBase(GlobalBase clone)
|
||||
protected GlobalBase(GlobalBase clone)
|
||||
{
|
||||
this.logger = clone.logger;
|
||||
this.settings = clone.settings;
|
||||
this.notificationConnectors = clone.notificationConnectors;
|
||||
this.libraryConnectors = clone.libraryConnectors;
|
||||
}
|
||||
|
||||
public GlobalBase(Logger? logger, TrangaSettings settings)
|
||||
protected GlobalBase(Logger? logger, TrangaSettings settings)
|
||||
{
|
||||
this.logger = logger;
|
||||
this.settings = settings;
|
||||
this.notificationConnectors = settings.LoadNotificationConnectors();
|
||||
this.libraryConnectors = settings.LoadLibraryConnectors();
|
||||
}
|
||||
|
||||
protected void Log(string message)
|
||||
@ -29,6 +38,38 @@ public abstract class GlobalBase
|
||||
Log(string.Format(fStr, replace));
|
||||
}
|
||||
|
||||
protected void SendNotifications(string title, string text)
|
||||
{
|
||||
foreach (NotificationConnector nc in notificationConnectors)
|
||||
nc.SendNotification(title, text);
|
||||
}
|
||||
|
||||
protected void AddNotificationConnector(NotificationConnector notificationConnector)
|
||||
{
|
||||
notificationConnectors.RemoveWhere(nc => nc.GetType() == notificationConnector.GetType());
|
||||
notificationConnectors.Add(notificationConnector);
|
||||
|
||||
while(IsFileInUse(settings.notificationConnectorsFilePath))
|
||||
Thread.Sleep(100);
|
||||
File.WriteAllText(settings.notificationConnectorsFilePath, JsonConvert.SerializeObject(notificationConnectors));
|
||||
}
|
||||
|
||||
protected void UpdateLibraries()
|
||||
{
|
||||
foreach(LibraryConnector lc in libraryConnectors)
|
||||
lc.UpdateLibrary();
|
||||
}
|
||||
|
||||
protected void AddLibraryConnector(LibraryConnector libraryConnector)
|
||||
{
|
||||
libraryConnectors.RemoveWhere(lc => lc.GetType() == libraryConnector.GetType());
|
||||
libraryConnectors.Add(libraryConnector);
|
||||
|
||||
while(IsFileInUse(settings.libraryConnectorsFilePath))
|
||||
Thread.Sleep(100);
|
||||
File.WriteAllText(settings.libraryConnectorsFilePath, JsonConvert.SerializeObject(libraryConnectors));
|
||||
}
|
||||
|
||||
protected bool IsFileInUse(string filePath)
|
||||
{
|
||||
if (!File.Exists(filePath))
|
||||
|
@ -6,7 +6,7 @@ public class DownloadChapter : Job
|
||||
{
|
||||
public Chapter chapter { get; init; }
|
||||
|
||||
public DownloadChapter(MangaConnector connector, Chapter chapter) : base(connector)
|
||||
public DownloadChapter(GlobalBase clone, MangaConnector connector, Chapter chapter) : base(clone, connector)
|
||||
{
|
||||
this.chapter = chapter;
|
||||
}
|
||||
@ -16,6 +16,8 @@ public class DownloadChapter : Job
|
||||
Task downloadTask = new(delegate
|
||||
{
|
||||
mangaConnector.DownloadChapter(chapter, this.progressToken);
|
||||
UpdateLibraries();
|
||||
SendNotifications("Chapter downloaded", $"{chapter.parentPublication.sortName} - {chapter.chapterNumber}");
|
||||
});
|
||||
downloadTask.Start();
|
||||
return Array.Empty<Job>();
|
||||
|
@ -6,7 +6,7 @@ public class DownloadNewChapters : Job
|
||||
{
|
||||
public Publication publication { get; init; }
|
||||
|
||||
public DownloadNewChapters(MangaConnector connector, Publication publication, bool recurring = false) : base (connector, recurring)
|
||||
public DownloadNewChapters(GlobalBase clone, MangaConnector connector, Publication publication, bool recurring = false) : base (clone, connector, recurring)
|
||||
{
|
||||
this.publication = publication;
|
||||
}
|
||||
@ -18,7 +18,7 @@ public class DownloadNewChapters : Job
|
||||
List<Job> subJobs = new();
|
||||
foreach (Chapter chapter in chapters)
|
||||
{
|
||||
DownloadChapter downloadChapterJob = new(this.mangaConnector, chapter);
|
||||
DownloadChapter downloadChapterJob = new(this, this.mangaConnector, chapter);
|
||||
subJobs.Add(downloadChapterJob);
|
||||
}
|
||||
progressToken.Complete();
|
||||
|
@ -10,6 +10,9 @@ public class TrangaSettings
|
||||
public string downloadLocation { get; private set; }
|
||||
public string workingDirectory { get; init; }
|
||||
[JsonIgnore] public string settingsFilePath => Path.Join(workingDirectory, "settings.json");
|
||||
[JsonIgnore] public string libraryConnectorsFilePath => Path.Join(workingDirectory, "libraryConnectors.json");
|
||||
|
||||
[JsonIgnore] public string notificationConnectorsFilePath => Path.Join(workingDirectory, "notificationConnectors.json");
|
||||
[JsonIgnore] public string tasksFilePath => Path.Join(workingDirectory, "tasks.json");
|
||||
[JsonIgnore] public string coverImageCache => Path.Join(workingDirectory, "imageCache");
|
||||
public ushort? version { get; set; }
|
||||
@ -24,23 +27,32 @@ public class TrangaSettings
|
||||
this.downloadLocation = downloadLocation;
|
||||
}
|
||||
|
||||
public static TrangaSettings LoadSettings(string importFilePath, Logger? logger)
|
||||
public HashSet<LibraryConnector> LoadLibraryConnectors()
|
||||
{
|
||||
if (!File.Exists(importFilePath))
|
||||
return new TrangaSettings();
|
||||
|
||||
string toRead = File.ReadAllText(importFilePath);
|
||||
TrangaSettings? settings = JsonConvert.DeserializeObject<TrangaSettings>(File.ReadAllText(importFilePath),
|
||||
new JsonSerializerSettings
|
||||
if (!File.Exists(libraryConnectorsFilePath))
|
||||
return new HashSet<LibraryConnector>();
|
||||
return JsonConvert.DeserializeObject<HashSet<LibraryConnector>>(File.ReadAllText(libraryConnectorsFilePath),
|
||||
new JsonSerializerSettings()
|
||||
{
|
||||
Converters =
|
||||
{
|
||||
new NotificationManagerJsonConverter(),
|
||||
new LibraryManagerJsonConverter()
|
||||
}
|
||||
});
|
||||
return settings ?? new TrangaSettings();
|
||||
})!;
|
||||
}
|
||||
|
||||
public HashSet<NotificationConnector> LoadNotificationConnectors()
|
||||
{
|
||||
if (!File.Exists(notificationConnectorsFilePath))
|
||||
return new HashSet<NotificationConnector>();
|
||||
return JsonConvert.DeserializeObject<HashSet<NotificationConnector>>(File.ReadAllText(libraryConnectorsFilePath),
|
||||
new JsonSerializerSettings()
|
||||
{
|
||||
Converters =
|
||||
{
|
||||
new NotificationManagerJsonConverter()
|
||||
}
|
||||
})!;
|
||||
}
|
||||
|
||||
public void ExportSettings()
|
||||
|
Loading…
Reference in New Issue
Block a user