Use event return value instead of passing a set delegate.

This commit is contained in:
Michael Bucari-Tovo 2022-05-24 15:47:30 -06:00
parent c88f47eed4
commit bc0009be6c
3 changed files with 11 additions and 13 deletions

View File

@ -4,7 +4,8 @@ namespace FileLiberator
{ {
public abstract class AudioDecodable : Processable public abstract class AudioDecodable : Processable
{ {
public event EventHandler<Action<byte[]>> RequestCoverArt; public delegate byte[] RequestCoverArtHandler(object sender, EventArgs eventArgs);
public event RequestCoverArtHandler RequestCoverArt;
public event EventHandler<string> TitleDiscovered; public event EventHandler<string> TitleDiscovered;
public event EventHandler<string> AuthorsDiscovered; public event EventHandler<string> AuthorsDiscovered;
public event EventHandler<string> NarratorsDiscovered; public event EventHandler<string> NarratorsDiscovered;
@ -32,10 +33,10 @@ namespace FileLiberator
NarratorsDiscovered?.Invoke(this, narrators); NarratorsDiscovered?.Invoke(this, narrators);
} }
protected void OnRequestCoverArt(Action<byte[]> setCoverArtDel) protected byte[] OnRequestCoverArt()
{ {
Serilog.Log.Logger.Debug("Event fired {@DebugInfo}", new { Name = nameof(RequestCoverArt) }); Serilog.Log.Logger.Debug("Event fired {@DebugInfo}", new { Name = nameof(RequestCoverArt) });
RequestCoverArt?.Invoke(this, setCoverArtDel); return RequestCoverArt?.Invoke(this, new());
} }
protected void OnCoverImageDiscovered(byte[] coverImage) protected void OnCoverImageDiscovered(byte[] coverImage)

View File

@ -247,7 +247,7 @@ namespace FileLiberator
if (e is not null) if (e is not null)
OnCoverImageDiscovered(e); OnCoverImageDiscovered(e);
else if (Configuration.Instance.AllowLibationFixup) else if (Configuration.Instance.AllowLibationFixup)
OnRequestCoverArt(abDownloader.SetCoverArt); abDownloader.SetCoverArt(OnRequestCoverArt());
} }
/// <summary>Move new files to 'Books' directory</summary> /// <summary>Move new files to 'Books' directory</summary>

View File

@ -59,7 +59,6 @@ namespace LibationWinForms.ProcessQueue
private Processable CurrentProcessable => _currentProcessable ??= Processes.Dequeue().Invoke(); private Processable CurrentProcessable => _currentProcessable ??= Processes.Dequeue().Invoke();
private Processable NextProcessable() => _currentProcessable = null; private Processable NextProcessable() => _currentProcessable = null;
private Processable _currentProcessable; private Processable _currentProcessable;
private Func<byte[]> GetCoverArtDelegate;
private readonly Queue<Func<Processable>> Processes = new(); private readonly Queue<Func<Processable>> Processes = new();
private readonly LogMe Logger; private readonly LogMe Logger;
@ -231,11 +230,14 @@ namespace LibationWinForms.ProcessQueue
BookText = $"{title}\r\nBy {authorNames}\r\nNarrated by {narratorNames}"; BookText = $"{title}\r\nBy {authorNames}\r\nNarrated by {narratorNames}";
} }
public void AudioDecodable_RequestCoverArt(object sender, Action<byte[]> setCoverArtDelegate) private byte[] AudioDecodable_RequestCoverArt(object sender, EventArgs e)
{ {
byte[] coverData = GetCoverArtDelegate(); byte[] coverData = PictureStorage
setCoverArtDelegate(coverData); .GetPictureSynchronously(
new PictureDefinition(LibraryBook.Book.PictureId, PictureSize._500x500));
AudioDecodable_CoverImageDiscovered(this, coverData); AudioDecodable_CoverImageDiscovered(this, coverData);
return coverData;
} }
private void AudioDecodable_CoverImageDiscovered(object sender, byte[] coverArt) private void AudioDecodable_CoverImageDiscovered(object sender, byte[] coverArt)
@ -272,11 +274,6 @@ namespace LibationWinForms.ProcessQueue
Logger.Info($"{Environment.NewLine}{((Processable)sender).Name} Step, Begin: {libraryBook.Book}"); Logger.Info($"{Environment.NewLine}{((Processable)sender).Name} Step, Begin: {libraryBook.Book}");
GetCoverArtDelegate = () => PictureStorage.GetPictureSynchronously(
new PictureDefinition(
libraryBook.Book.PictureId,
PictureSize._500x500));
title = libraryBook.Book.Title; title = libraryBook.Book.Title;
authorNames = libraryBook.Book.AuthorNames(); authorNames = libraryBook.Book.AuthorNames();
narratorNames = libraryBook.Book.NarratorNames(); narratorNames = libraryBook.Book.NarratorNames();