minor refactors

This commit is contained in:
Robert McRackan 2022-04-26 08:18:35 -04:00
parent 3dcbcf42ed
commit 323b8f2fb9
9 changed files with 49 additions and 47 deletions

View File

@ -20,8 +20,8 @@ namespace AaxDecrypter
public override void SetCoverArt(byte[] coverArt) public override void SetCoverArt(byte[] coverArt)
{ {
base.SetCoverArt(coverArt); base.SetCoverArt(coverArt);
if (coverArt is not null) if (coverArt is not null && AaxFile?.AppleTags is not null)
AaxFile?.AppleTags.SetCoverArt(coverArt); AaxFile.AppleTags.Cover = coverArt;
} }
/// <summary>Optional step to run after Metadata is retrieved</summary> /// <summary>Optional step to run after Metadata is retrieved</summary>

View File

@ -67,16 +67,17 @@ namespace AaxDecrypter
Serilog.Log.Logger.Error("Conversion failed"); Serilog.Log.Logger.Error("Conversion failed");
return IsSuccess; return IsSuccess;
} }
protected void OnRetrievedTitle(string title) protected void OnRetrievedTitle(string title)
=> RetrievedTitle?.Invoke(this, title); => RetrievedTitle?.Invoke(this, title);
protected void OnRetrievedAuthors(string authors) protected void OnRetrievedAuthors(string authors)
=> RetrievedAuthors?.Invoke(this, authors); => RetrievedAuthors?.Invoke(this, authors);
protected void OnRetrievedNarrators(string narrators) protected void OnRetrievedNarrators(string narrators)
=> RetrievedNarrators?.Invoke(this, narrators); => RetrievedNarrators?.Invoke(this, narrators);
protected void OnRetrievedCoverArt(byte[] coverArt) protected void OnRetrievedCoverArt(byte[] coverArt)
=> RetrievedCoverArt?.Invoke(this, coverArt); => RetrievedCoverArt?.Invoke(this, coverArt);
protected void OnDecryptProgressUpdate(DownloadProgress downloadProgress) protected void OnDecryptProgressUpdate(DownloadProgress downloadProgress)
=> DecryptProgressUpdate?.Invoke(this, downloadProgress); => DecryptProgressUpdate?.Invoke(this, downloadProgress);
protected void OnDecryptTimeRemaining(TimeSpan timeRemaining) protected void OnDecryptTimeRemaining(TimeSpan timeRemaining)

View File

@ -17,19 +17,22 @@ namespace FileLiberator
RequestCoverArt?.Invoke(this, setCoverArtDel); RequestCoverArt?.Invoke(this, setCoverArtDel);
} }
protected void OnTitleDiscovered(string title) protected void OnTitleDiscovered(string title) => OnTitleDiscovered(null, title);
protected void OnTitleDiscovered(object _, string title)
{ {
Serilog.Log.Logger.Debug("Event fired {@DebugInfo}", new { Name = nameof(TitleDiscovered), Title = title }); Serilog.Log.Logger.Debug("Event fired {@DebugInfo}", new { Name = nameof(TitleDiscovered), Title = title });
TitleDiscovered?.Invoke(this, title); TitleDiscovered?.Invoke(this, title);
} }
protected void OnAuthorsDiscovered(string authors) protected void OnAuthorsDiscovered(string authors) => OnAuthorsDiscovered(null, authors);
protected void OnAuthorsDiscovered(object _, string authors)
{ {
Serilog.Log.Logger.Debug("Event fired {@DebugInfo}", new { Name = nameof(AuthorsDiscovered), Authors = authors }); Serilog.Log.Logger.Debug("Event fired {@DebugInfo}", new { Name = nameof(AuthorsDiscovered), Authors = authors });
AuthorsDiscovered?.Invoke(this, authors); AuthorsDiscovered?.Invoke(this, authors);
} }
protected void OnNarratorsDiscovered(string narrators) protected void OnNarratorsDiscovered(string narrators) => OnNarratorsDiscovered(null, narrators);
protected void OnNarratorsDiscovered(object _, string narrators)
{ {
Serilog.Log.Logger.Debug("Event fired {@DebugInfo}", new { Name = nameof(NarratorsDiscovered), Narrators = narrators }); Serilog.Log.Logger.Debug("Event fired {@DebugInfo}", new { Name = nameof(NarratorsDiscovered), Narrators = narrators });
NarratorsDiscovered?.Invoke(this, narrators); NarratorsDiscovered?.Invoke(this, narrators);

View File

@ -73,8 +73,8 @@ namespace FileLiberator
private void M4bBook_ConversionProgressUpdate(object sender, ConversionProgressEventArgs e) private void M4bBook_ConversionProgressUpdate(object sender, ConversionProgressEventArgs e)
{ {
var duration = m4bBook.Duration; var duration = m4bBook.Duration;
double remainingSecsToProcess = (duration - e.ProcessPosition).TotalSeconds; var remainingSecsToProcess = (duration - e.ProcessPosition).TotalSeconds;
double estTimeRemaining = remainingSecsToProcess / e.ProcessSpeed; var estTimeRemaining = remainingSecsToProcess / e.ProcessSpeed;
if (double.IsNormal(estTimeRemaining)) if (double.IsNormal(estTimeRemaining))
OnStreamingTimeRemaining(TimeSpan.FromSeconds(estTimeRemaining)); OnStreamingTimeRemaining(TimeSpan.FromSeconds(estTimeRemaining));

View File

@ -132,11 +132,12 @@ namespace FileLiberator
abDownloader = converter; abDownloader = converter;
} }
abDownloader.DecryptProgressUpdate += (_, progress) => OnStreamingProgressChanged(progress); abDownloader.DecryptProgressUpdate += OnStreamingProgressChanged;
abDownloader.DecryptTimeRemaining += (_, remaining) => OnStreamingTimeRemaining(remaining); abDownloader.DecryptTimeRemaining += OnStreamingTimeRemaining;
abDownloader.RetrievedTitle += (_, title) => OnTitleDiscovered(title);
abDownloader.RetrievedAuthors += (_, authors) => OnAuthorsDiscovered(authors); abDownloader.RetrievedTitle += OnTitleDiscovered;
abDownloader.RetrievedNarrators += (_, narrators) => OnNarratorsDiscovered(narrators); abDownloader.RetrievedAuthors += OnAuthorsDiscovered;
abDownloader.RetrievedNarrators += OnNarratorsDiscovered;
abDownloader.RetrievedCoverArt += AaxcDownloader_RetrievedCoverArt; abDownloader.RetrievedCoverArt += AaxcDownloader_RetrievedCoverArt;
abDownloader.FileCreated += (_, path) => OnFileCreated(libraryBook, path); abDownloader.FileCreated += (_, path) => OnFileCreated(libraryBook, path);
@ -172,7 +173,7 @@ namespace FileLiberator
throw new Exception(errorString("Locale")); throw new Exception(errorString("Locale"));
} }
private void AaxcDownloader_RetrievedCoverArt(object sender, byte[] e) private void AaxcDownloader_RetrievedCoverArt(object _, byte[] e)
{ {
if (e is not null) if (e is not null)
OnCoverImageDiscovered(e); OnCoverImageDiscovered(e);

View File

@ -12,8 +12,7 @@ namespace FileLiberator
{ {
var client = new HttpClient(); var client = new HttpClient();
var progress = new Progress<DownloadProgress>(); var progress = new Progress<DownloadProgress>(OnStreamingProgressChanged);
progress.ProgressChanged += (_, e) => OnStreamingProgressChanged(e);
OnStreamingBegin(proposedDownloadFilePath); OnStreamingBegin(proposedDownloadFilePath);

View File

@ -62,8 +62,7 @@ namespace FileLiberator
var api = await libraryBook.GetApiAsync(); var api = await libraryBook.GetApiAsync();
var downloadUrl = await api.GetPdfDownloadLinkAsync(libraryBook.Book.AudibleProductId); var downloadUrl = await api.GetPdfDownloadLinkAsync(libraryBook.Book.AudibleProductId);
var progress = new Progress<DownloadProgress>(); var progress = new Progress<DownloadProgress>(OnStreamingProgressChanged);
progress.ProgressChanged += (_, e) => OnStreamingProgressChanged(e);
var client = new HttpClient(); var client = new HttpClient();

View File

@ -18,12 +18,14 @@ namespace FileLiberator
StreamingBegin?.Invoke(this, filePath); StreamingBegin?.Invoke(this, filePath);
} }
protected void OnStreamingProgressChanged(DownloadProgress progress) protected void OnStreamingProgressChanged(DownloadProgress progress) => OnStreamingProgressChanged(null, progress);
protected void OnStreamingProgressChanged(object _, DownloadProgress progress)
{ {
StreamingProgressChanged?.Invoke(this, progress); StreamingProgressChanged?.Invoke(this, progress);
} }
protected void OnStreamingTimeRemaining(TimeSpan timeRemaining) protected void OnStreamingTimeRemaining(TimeSpan timeRemaining) => OnStreamingTimeRemaining(null, timeRemaining);
protected void OnStreamingTimeRemaining(object _, TimeSpan timeRemaining)
{ {
StreamingTimeRemaining?.Invoke(this, timeRemaining); StreamingTimeRemaining?.Invoke(this, timeRemaining);
} }

View File

@ -14,11 +14,6 @@ namespace LibationWinForms.BookLiberation
private Func<byte[]> GetCoverArtDelegate; private Func<byte[]> GetCoverArtDelegate;
// book info
private string title;
private string authorNames;
private string narratorNames;
#region Processable event handler overrides #region Processable event handler overrides
public override void Processable_Begin(object sender, LibraryBook libraryBook) public override void Processable_Begin(object sender, LibraryBook libraryBook)
{ {
@ -31,8 +26,8 @@ namespace LibationWinForms.BookLiberation
//Set default values from library //Set default values from library
AudioDecodable_TitleDiscovered(sender, libraryBook.Book.Title); AudioDecodable_TitleDiscovered(sender, libraryBook.Book.Title);
AudioDecodable_AuthorsDiscovered(sender, string.Join(", ", libraryBook.Book.Authors)); AudioDecodable_AuthorsDiscovered(sender, libraryBook.Book.AuthorNames);
AudioDecodable_NarratorsDiscovered(sender, string.Join(", ", libraryBook.Book.NarratorNames)); AudioDecodable_NarratorsDiscovered(sender, libraryBook.Book.NarratorNames);
AudioDecodable_CoverImageDiscovered(sender, AudioDecodable_CoverImageDiscovered(sender,
PictureStorage.GetPicture( PictureStorage.GetPicture(
new PictureDefinition( new PictureDefinition(
@ -60,9 +55,24 @@ namespace LibationWinForms.BookLiberation
updateRemainingTime((int)timeRemaining.TotalSeconds); updateRemainingTime((int)timeRemaining.TotalSeconds);
} }
private void updateRemainingTime(int remaining)
=> remainingTimeLbl.UIThreadAsync(() => remainingTimeLbl.Text = $"ETA:\r\n{formatTime(remaining)}");
private string formatTime(int seconds)
{
var timeSpan = new TimeSpan(0, 0, seconds);
return
timeSpan.TotalHours >= 1 ? $"{timeSpan:%h}h {timeSpan:mm}m {timeSpan:ss}s"
: timeSpan.TotalMinutes >= 1 ? $"{timeSpan:%m}m {timeSpan:ss}s"
: $"{seconds} sec";
}
#endregion #endregion
#region AudioDecodable event handlers #region AudioDecodable event handlers
private string title;
private string authorNames;
private string narratorNames;
public override void AudioDecodable_RequestCoverArt(object sender, Action<byte[]> setCoverArtDelegate) public override void AudioDecodable_RequestCoverArt(object sender, Action<byte[]> setCoverArtDelegate)
{ {
base.AudioDecodable_RequestCoverArt(sender, setCoverArtDelegate); base.AudioDecodable_RequestCoverArt(sender, setCoverArtDelegate);
@ -91,27 +101,14 @@ namespace LibationWinForms.BookLiberation
updateBookInfo(); updateBookInfo();
} }
private void updateBookInfo()
=> bookInfoLbl.UIThreadAsync(() => bookInfoLbl.Text = $"{title}\r\nBy {authorNames}\r\nNarrated by {narratorNames}");
public override void AudioDecodable_CoverImageDiscovered(object sender, byte[] coverArt) public override void AudioDecodable_CoverImageDiscovered(object sender, byte[] coverArt)
{ {
base.AudioDecodable_CoverImageDiscovered(sender, coverArt); base.AudioDecodable_CoverImageDiscovered(sender, coverArt);
pictureBox1.UIThreadAsync(() => pictureBox1.Image = Dinah.Core.Drawing.ImageReader.ToImage(coverArt)); pictureBox1.UIThreadAsync(() => pictureBox1.Image = Dinah.Core.Drawing.ImageReader.ToImage(coverArt));
} }
#endregion #endregion
// thread-safe UI updates
private void updateBookInfo()
=> bookInfoLbl.UIThreadAsync(() => bookInfoLbl.Text = $"{title}\r\nBy {authorNames}\r\nNarrated by {narratorNames}");
private void updateRemainingTime(int remaining)
=> remainingTimeLbl.UIThreadAsync(() => remainingTimeLbl.Text = $"ETA:\r\n{formatTime(remaining)}");
private string formatTime(int seconds)
{
var timeSpan = new TimeSpan(0, 0, seconds);
return
timeSpan.TotalHours >= 1 ? $"{timeSpan:%h}h {timeSpan:mm}m {timeSpan:ss}s"
: timeSpan.TotalMinutes >= 1 ? $"{timeSpan:%m}m {timeSpan:ss}s"
: $"{seconds} sec";
}
} }
} }