Minor refactoring.
This commit is contained in:
parent
65027fd001
commit
56a48c04bf
@ -18,7 +18,7 @@ namespace FileLiberator
|
|||||||
var client = new HttpClient();
|
var client = new HttpClient();
|
||||||
|
|
||||||
var progress = new Progress<DownloadProgress>();
|
var progress = new Progress<DownloadProgress>();
|
||||||
progress.ProgressChanged += OnProgressChanged;
|
progress.ProgressChanged += (_, e) => StreamingProgressChanged?.Invoke(this, e);
|
||||||
|
|
||||||
StreamingBegin?.Invoke(this, proposedDownloadFilePath);
|
StreamingBegin?.Invoke(this, proposedDownloadFilePath);
|
||||||
|
|
||||||
@ -32,9 +32,5 @@ namespace FileLiberator
|
|||||||
StreamingCompleted?.Invoke(this, proposedDownloadFilePath);
|
StreamingCompleted?.Invoke(this, proposedDownloadFilePath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void OnProgressChanged(object sender, DownloadProgress e)
|
|
||||||
{
|
|
||||||
StreamingProgressChanged.Invoke(this, e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,17 +23,12 @@ namespace FileLiberator
|
|||||||
.GetLibrary_Flat_NoTracking()
|
.GetLibrary_Flat_NoTracking()
|
||||||
.Where(libraryBook => processable.Validate(libraryBook));
|
.Where(libraryBook => processable.Validate(libraryBook));
|
||||||
|
|
||||||
public static async Task<StatusHandler> ProcessSingleAsync(this IProcessable processable, LibraryBook libraryBook)
|
public static async Task<StatusHandler> ProcessSingleAsync(this IProcessable processable, LibraryBook libraryBook, bool validate)
|
||||||
{
|
{
|
||||||
if (!processable.Validate(libraryBook))
|
if (validate && !processable.Validate(libraryBook))
|
||||||
return new StatusHandler { "Validation failed" };
|
return new StatusHandler { "Validation failed" };
|
||||||
|
|
||||||
return await processable.ProcessBookAsync_NoValidation(libraryBook);
|
Serilog.Log.Logger.Information("Begin " + nameof(ProcessSingleAsync) + " {@DebugInfo}", new
|
||||||
}
|
|
||||||
|
|
||||||
public static async Task<StatusHandler> ProcessBookAsync_NoValidation(this IProcessable processable, LibraryBook libraryBook)
|
|
||||||
{
|
|
||||||
Serilog.Log.Logger.Information("Begin " + nameof(ProcessBookAsync_NoValidation) + " {@DebugInfo}", new
|
|
||||||
{
|
{
|
||||||
libraryBook.Book.Title,
|
libraryBook.Book.Title,
|
||||||
libraryBook.Book.AudibleProductId,
|
libraryBook.Book.AudibleProductId,
|
||||||
|
|||||||
@ -13,7 +13,7 @@
|
|||||||
<!-- <PublishSingleFile>true</PublishSingleFile> -->
|
<!-- <PublishSingleFile>true</PublishSingleFile> -->
|
||||||
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
|
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
|
||||||
|
|
||||||
<Version>5.4.9.264</Version>
|
<Version>5.4.9.269</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||||
|
|||||||
@ -3,7 +3,6 @@ using System.Collections.Generic;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
using AudibleApi;
|
|
||||||
using AudibleApi.Authorization;
|
using AudibleApi.Authorization;
|
||||||
using DataLayer;
|
using DataLayer;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
@ -13,7 +12,6 @@ using FileManager;
|
|||||||
using InternalUtilities;
|
using InternalUtilities;
|
||||||
using LibationWinForms;
|
using LibationWinForms;
|
||||||
using LibationWinForms.Dialogs;
|
using LibationWinForms.Dialogs;
|
||||||
using Newtonsoft.Json;
|
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
|
|
||||||
|
|||||||
@ -40,9 +40,9 @@ namespace LibationWinForms.BookLiberation
|
|||||||
|
|
||||||
private void OnUnsubscribeAll(object sender, EventArgs e)
|
private void OnUnsubscribeAll(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
Disposed -= OnUnsubscribeAll;
|
|
||||||
if (Streamable is IAudioDecodable audioDecodable)
|
if (Streamable is IAudioDecodable audioDecodable)
|
||||||
{
|
{
|
||||||
|
Disposed -= OnUnsubscribeAll;
|
||||||
audioDecodable.RequestCoverArt -= OnRequestCoverArt;
|
audioDecodable.RequestCoverArt -= OnRequestCoverArt;
|
||||||
audioDecodable.TitleDiscovered -= OnTitleDiscovered;
|
audioDecodable.TitleDiscovered -= OnTitleDiscovered;
|
||||||
audioDecodable.AuthorsDiscovered -= OnAuthorsDiscovered;
|
audioDecodable.AuthorsDiscovered -= OnAuthorsDiscovered;
|
||||||
@ -62,10 +62,10 @@ namespace LibationWinForms.BookLiberation
|
|||||||
FileManager.PictureSize._500x500));
|
FileManager.PictureSize._500x500));
|
||||||
|
|
||||||
//Set default values from library
|
//Set default values from library
|
||||||
OnTitleDiscovered(null, libraryBook.Book.Title);
|
OnTitleDiscovered(sender, libraryBook.Book.Title);
|
||||||
OnAuthorsDiscovered(null, string.Join(", ", libraryBook.Book.Authors));
|
OnAuthorsDiscovered(sender, string.Join(", ", libraryBook.Book.Authors));
|
||||||
OnNarratorsDiscovered(null, string.Join(", ", libraryBook.Book.NarratorNames));
|
OnNarratorsDiscovered(sender, string.Join(", ", libraryBook.Book.NarratorNames));
|
||||||
OnCoverImageDiscovered(null,
|
OnCoverImageDiscovered(sender,
|
||||||
FileManager.PictureStorage.GetPicture(
|
FileManager.PictureStorage.GetPicture(
|
||||||
new FileManager.PictureDefinition(
|
new FileManager.PictureDefinition(
|
||||||
libraryBook.Book.PictureId,
|
libraryBook.Book.PictureId,
|
||||||
@ -74,7 +74,6 @@ namespace LibationWinForms.BookLiberation
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region IStreamable event handler overrides
|
#region IStreamable event handler overrides
|
||||||
|
|
||||||
public override void OnStreamingProgressChanged(object sender, DownloadProgress downloadProgress)
|
public override void OnStreamingProgressChanged(object sender, DownloadProgress downloadProgress)
|
||||||
{
|
{
|
||||||
if (!downloadProgress.ProgressPercentage.HasValue)
|
if (!downloadProgress.ProgressPercentage.HasValue)
|
||||||
@ -88,11 +87,9 @@ namespace LibationWinForms.BookLiberation
|
|||||||
|
|
||||||
public override void OnStreamingTimeRemaining(object sender, TimeSpan timeRemaining)
|
public override void OnStreamingTimeRemaining(object sender, TimeSpan timeRemaining)
|
||||||
=> updateRemainingTime((int)timeRemaining.TotalSeconds);
|
=> updateRemainingTime((int)timeRemaining.TotalSeconds);
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region IAudioDecodable event handlers
|
#region IAudioDecodable event handlers
|
||||||
|
|
||||||
public virtual void OnRequestCoverArt(object sender, Action<byte[]> setCoverArtDelegate)
|
public virtual void OnRequestCoverArt(object sender, Action<byte[]> setCoverArtDelegate)
|
||||||
=> setCoverArtDelegate(GetCoverArtDelegate?.Invoke());
|
=> setCoverArtDelegate(GetCoverArtDelegate?.Invoke());
|
||||||
|
|
||||||
@ -117,16 +114,13 @@ namespace LibationWinForms.BookLiberation
|
|||||||
|
|
||||||
public virtual void OnCoverImageDiscovered(object sender, byte[] coverArt)
|
public virtual void OnCoverImageDiscovered(object sender, byte[] coverArt)
|
||||||
=> pictureBox1.UIThread(() => pictureBox1.Image = Dinah.Core.Drawing.ImageReader.ToImage(coverArt));
|
=> pictureBox1.UIThread(() => pictureBox1.Image = Dinah.Core.Drawing.ImageReader.ToImage(coverArt));
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
// thread-safe UI updates
|
// thread-safe UI updates
|
||||||
private void updateBookInfo()
|
private void updateBookInfo()
|
||||||
=> bookInfoLbl.UIThread(() => bookInfoLbl.Text = $"{title}\r\nBy {authorNames}\r\nNarrated by {narratorNames}");
|
=> bookInfoLbl.UIThread(() => bookInfoLbl.Text = $"{title}\r\nBy {authorNames}\r\nNarrated by {narratorNames}");
|
||||||
|
|
||||||
private void updateRemainingTime(int remaining)
|
private void updateRemainingTime(int remaining)
|
||||||
=> remainingTimeLbl.UIThread(() => remainingTimeLbl.Text = $"ETA:\r\n{remaining} sec");
|
=> remainingTimeLbl.UIThread(() => remainingTimeLbl.Text = $"ETA:\r\n{remaining} sec");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,7 +22,7 @@ namespace LibationWinForms.BookLiberation
|
|||||||
|
|
||||||
//Don't unsubscribe from Dispose because it fires on
|
//Don't unsubscribe from Dispose because it fires on
|
||||||
//IStreamable.StreamingCompleted, and the IProcessable
|
//IStreamable.StreamingCompleted, and the IProcessable
|
||||||
//events need to live past that.
|
//events need to live past that event.
|
||||||
processable.Completed += OnUnsubscribeAll;
|
processable.Completed += OnUnsubscribeAll;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -42,7 +42,6 @@ namespace LibationWinForms.BookLiberation
|
|||||||
public virtual void OnBegin(object sender, LibraryBook libraryBook) => LogMe.Info($"Begin: {libraryBook.Book}");
|
public virtual void OnBegin(object sender, LibraryBook libraryBook) => LogMe.Info($"Begin: {libraryBook.Book}");
|
||||||
public virtual void OnStatusUpdate(object sender, string statusUpdate) => LogMe.Info("- " + statusUpdate);
|
public virtual void OnStatusUpdate(object sender, string statusUpdate) => LogMe.Info("- " + statusUpdate);
|
||||||
public virtual void OnCompleted(object sender, LibraryBook libraryBook) => LogMe.Info($"Completed: {libraryBook.Book}{Environment.NewLine}");
|
public virtual void OnCompleted(object sender, LibraryBook libraryBook) => LogMe.Info($"Completed: {libraryBook.Book}{Environment.NewLine}");
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -79,7 +79,7 @@ namespace LibationWinForms.BookLiberation
|
|||||||
var automatedBackupsForm = new AutomatedBackupsForm();
|
var automatedBackupsForm = new AutomatedBackupsForm();
|
||||||
var logMe = LogMe.RegisterForm(automatedBackupsForm);
|
var logMe = LogMe.RegisterForm(automatedBackupsForm);
|
||||||
|
|
||||||
var convertBook = CreateStreamProcessable<ConvertToMp3, AudioConvertForm>(null, logMe);
|
var convertBook = CreateStreamProcessable<ConvertToMp3, AudioConvertForm>(logMe);
|
||||||
|
|
||||||
await new BackupLoop(logMe, convertBook, automatedBackupsForm).RunBackupAsync();
|
await new BackupLoop(logMe, convertBook, automatedBackupsForm).RunBackupAsync();
|
||||||
}
|
}
|
||||||
@ -91,7 +91,7 @@ namespace LibationWinForms.BookLiberation
|
|||||||
var automatedBackupsForm = new AutomatedBackupsForm();
|
var automatedBackupsForm = new AutomatedBackupsForm();
|
||||||
var logMe = LogMe.RegisterForm(automatedBackupsForm);
|
var logMe = LogMe.RegisterForm(automatedBackupsForm);
|
||||||
|
|
||||||
var downloadPdf = CreateStreamProcessable<DownloadPdf, PdfDownloadForm>(completedAction, logMe);
|
var downloadPdf = CreateStreamProcessable<DownloadPdf, PdfDownloadForm>(logMe, completedAction);
|
||||||
|
|
||||||
await new BackupLoop(logMe, downloadPdf, automatedBackupsForm).RunBackupAsync();
|
await new BackupLoop(logMe, downloadPdf, automatedBackupsForm).RunBackupAsync();
|
||||||
}
|
}
|
||||||
@ -107,25 +107,22 @@ namespace LibationWinForms.BookLiberation
|
|||||||
}
|
}
|
||||||
|
|
||||||
var downloadFile = CreateStreamable<DownloadFile, DownloadForm>(onDownloadFileStreamingCompleted);
|
var downloadFile = CreateStreamable<DownloadFile, DownloadForm>(onDownloadFileStreamingCompleted);
|
||||||
|
async void runDownload() => await downloadFile.PerformDownloadFileAsync(url, destination);
|
||||||
new System.Threading.Thread(() => downloadFile.PerformDownloadFileAsync(url, destination).GetAwaiter().GetResult())
|
new Task(runDownload).Start();
|
||||||
{ IsBackground = true }
|
|
||||||
.Start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IProcessable CreateBackupBook(EventHandler<LibraryBook> completedAction, LogMe logMe)
|
private static IProcessable CreateBackupBook(EventHandler<LibraryBook> completedAction, LogMe logMe)
|
||||||
{
|
{
|
||||||
var downloadPdf = CreateStreamProcessable<DownloadPdf, PdfDownloadForm>(null, logMe);
|
var downloadPdf = CreateStreamProcessable<DownloadPdf, PdfDownloadForm>(logMe);
|
||||||
|
|
||||||
//Chain pdf download on DownloadDecryptBook.Completed
|
//Chain pdf download on DownloadDecryptBook.Completed
|
||||||
async void onDownloadDecryptBookCompleted(object sender, LibraryBook e)
|
async void onDownloadDecryptBookCompleted(object sender, LibraryBook e)
|
||||||
{
|
{
|
||||||
await downloadPdf.TryProcessAsync(e);
|
await downloadPdf.TryProcessAsync(e);
|
||||||
|
|
||||||
completedAction(sender, e);
|
completedAction(sender, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
var downloadDecryptBook = CreateStreamProcessable<DownloadDecryptBook, AudioDecryptForm>(onDownloadDecryptBookCompleted, logMe);
|
var downloadDecryptBook = CreateStreamProcessable<DownloadDecryptBook, AudioDecryptForm>(logMe, onDownloadDecryptBookCompleted);
|
||||||
return downloadDecryptBook;
|
return downloadDecryptBook;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,9 +131,10 @@ namespace LibationWinForms.BookLiberation
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TStrProc">The <see cref="IStreamProcessable"/> derrived type to create.</typeparam>
|
/// <typeparam name="TStrProc">The <see cref="IStreamProcessable"/> derrived type to create.</typeparam>
|
||||||
/// <typeparam name="TForm">The <see cref="ProcessBaseForm"/> derrived Form to create on <see cref="IProcessable.Begin"/>, Show on <see cref="IStreamable.StreamingBegin"/>, and Close & Dispose on <see cref="IStreamable.StreamingCompleted"/></typeparam>
|
/// <typeparam name="TForm">The <see cref="ProcessBaseForm"/> derrived Form to create on <see cref="IProcessable.Begin"/>, Show on <see cref="IStreamable.StreamingBegin"/>, and Close & Dispose on <see cref="IStreamable.StreamingCompleted"/></typeparam>
|
||||||
|
/// <param name="logMe">The logger</param>
|
||||||
/// <param name="completedAction">An additional event handler to handle <see cref="IProcessable.Completed"/></param>
|
/// <param name="completedAction">An additional event handler to handle <see cref="IProcessable.Completed"/></param>
|
||||||
/// <returns>A new <see cref="IStreamProcessable"/> of type <typeparamref name="TStrProc"/></returns>
|
/// <returns>A new <see cref="IStreamProcessable"/> of type <typeparamref name="TStrProc"/></returns>
|
||||||
private static TStrProc CreateStreamProcessable<TStrProc, TForm>(EventHandler<LibraryBook> completedAction = null, LogMe logMe = null)
|
private static TStrProc CreateStreamProcessable<TStrProc, TForm>(LogMe logMe, EventHandler<LibraryBook> completedAction = null)
|
||||||
where TForm : ProcessBaseForm, new()
|
where TForm : ProcessBaseForm, new()
|
||||||
where TStrProc : IStreamProcessable, new()
|
where TStrProc : IStreamProcessable, new()
|
||||||
{
|
{
|
||||||
@ -192,9 +190,9 @@ namespace LibationWinForms.BookLiberation
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected abstract Task RunAsync();
|
protected abstract Task RunAsync();
|
||||||
|
|
||||||
protected abstract string SkipDialogText { get; }
|
protected abstract string SkipDialogText { get; }
|
||||||
protected abstract MessageBoxButtons SkipDialogButtons { get; }
|
protected abstract MessageBoxButtons SkipDialogButtons { get; }
|
||||||
|
protected abstract MessageBoxDefaultButton SkipDialogDefaultButton { get; }
|
||||||
protected abstract DialogResult CreateSkipFileResult { get; }
|
protected abstract DialogResult CreateSkipFileResult { get; }
|
||||||
|
|
||||||
public async Task RunBackupAsync()
|
public async Task RunBackupAsync()
|
||||||
@ -214,13 +212,13 @@ namespace LibationWinForms.BookLiberation
|
|||||||
LogMe.Info("DONE");
|
LogMe.Info("DONE");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async Task<bool> ProcessOneAsync(Func<LibraryBook, Task<StatusHandler>> func, LibraryBook libraryBook)
|
protected async Task<bool> ProcessOneAsync(LibraryBook libraryBook, bool validate)
|
||||||
{
|
{
|
||||||
string logMessage;
|
string logMessage;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var statusHandler = await func(libraryBook);
|
var statusHandler = await Processable.ProcessSingleAsync(libraryBook, validate);
|
||||||
|
|
||||||
if (statusHandler.IsSuccess)
|
if (statusHandler.IsSuccess)
|
||||||
return true;
|
return true;
|
||||||
@ -258,7 +256,7 @@ $@" Title: {libraryBook.Book.Title}
|
|||||||
details = "[Error retrieving details]";
|
details = "[Error retrieving details]";
|
||||||
}
|
}
|
||||||
|
|
||||||
var dialogResult = MessageBox.Show(string.Format(SkipDialogText, details), "Skip importing this book?", SkipDialogButtons, MessageBoxIcon.Question);
|
var dialogResult = MessageBox.Show(string.Format(SkipDialogText, details), "Skip importing this book?", SkipDialogButtons, MessageBoxIcon.Question, SkipDialogDefaultButton);
|
||||||
|
|
||||||
if (dialogResult == DialogResult.Abort)
|
if (dialogResult == DialogResult.Abort)
|
||||||
return false;
|
return false;
|
||||||
@ -291,6 +289,7 @@ An error occurred while trying to process this book. Skip this book permanently?
|
|||||||
- Click NO to skip the book this time only. We'll try again later.
|
- Click NO to skip the book this time only. We'll try again later.
|
||||||
".Trim();
|
".Trim();
|
||||||
protected override MessageBoxButtons SkipDialogButtons => MessageBoxButtons.YesNo;
|
protected override MessageBoxButtons SkipDialogButtons => MessageBoxButtons.YesNo;
|
||||||
|
protected override MessageBoxDefaultButton SkipDialogDefaultButton => MessageBoxDefaultButton.Button2;
|
||||||
protected override DialogResult CreateSkipFileResult => DialogResult.Yes;
|
protected override DialogResult CreateSkipFileResult => DialogResult.Yes;
|
||||||
|
|
||||||
public BackupSingle(LogMe logMe, IProcessable processable, LibraryBook libraryBook)
|
public BackupSingle(LogMe logMe, IProcessable processable, LibraryBook libraryBook)
|
||||||
@ -302,7 +301,7 @@ An error occurred while trying to process this book. Skip this book permanently?
|
|||||||
protected override async Task RunAsync()
|
protected override async Task RunAsync()
|
||||||
{
|
{
|
||||||
if (_libraryBook is not null)
|
if (_libraryBook is not null)
|
||||||
await ProcessOneAsync(Processable.ProcessSingleAsync, _libraryBook);
|
await ProcessOneAsync(_libraryBook, validate: true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -319,6 +318,7 @@ An error occurred while trying to process this book.
|
|||||||
- IGNORE: Permanently ignore this book. Continue processing books. (Will not try this book again later.)
|
- IGNORE: Permanently ignore this book. Continue processing books. (Will not try this book again later.)
|
||||||
".Trim();
|
".Trim();
|
||||||
protected override MessageBoxButtons SkipDialogButtons => MessageBoxButtons.AbortRetryIgnore;
|
protected override MessageBoxButtons SkipDialogButtons => MessageBoxButtons.AbortRetryIgnore;
|
||||||
|
protected override MessageBoxDefaultButton SkipDialogDefaultButton => MessageBoxDefaultButton.Button1;
|
||||||
protected override DialogResult CreateSkipFileResult => DialogResult.Ignore;
|
protected override DialogResult CreateSkipFileResult => DialogResult.Ignore;
|
||||||
|
|
||||||
public BackupLoop(LogMe logMe, IProcessable processable, AutomatedBackupsForm automatedBackupsForm)
|
public BackupLoop(LogMe logMe, IProcessable processable, AutomatedBackupsForm automatedBackupsForm)
|
||||||
@ -329,7 +329,7 @@ An error occurred while trying to process this book.
|
|||||||
// support for 'skip this time only' requires state. iterators provide this state for free. therefore: use foreach/iterator here
|
// support for 'skip this time only' requires state. iterators provide this state for free. therefore: use foreach/iterator here
|
||||||
foreach (var libraryBook in Processable.GetValidLibraryBooks())
|
foreach (var libraryBook in Processable.GetValidLibraryBooks())
|
||||||
{
|
{
|
||||||
var keepGoing = await ProcessOneAsync(Processable.ProcessBookAsync_NoValidation, libraryBook);
|
var keepGoing = await ProcessOneAsync(libraryBook, validate: false);
|
||||||
if (!keepGoing)
|
if (!keepGoing)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user