Refinment

This commit is contained in:
Michael Bucari-Tovo 2021-08-11 21:19:38 -06:00
parent 1c239dc546
commit 79ed92f303
3 changed files with 35 additions and 39 deletions

View File

@ -13,7 +13,7 @@
<!-- <PublishSingleFile>true</PublishSingleFile> --> <!-- <PublishSingleFile>true</PublishSingleFile> -->
<RuntimeIdentifier>win-x64</RuntimeIdentifier> <RuntimeIdentifier>win-x64</RuntimeIdentifier>
<Version>5.4.9.252</Version> <Version>5.4.9.253</Version>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

View File

@ -63,7 +63,6 @@ namespace LibationLauncher
#if !DEBUG #if !DEBUG
checkForUpdate(config); checkForUpdate(config);
#endif #endif
Application.Run(new Form1()); Application.Run(new Form1());
} }

View File

@ -71,7 +71,6 @@ namespace LibationWinForms.BookLiberation
var backupBook = CreateBackupBook(completedAction, logMe); var backupBook = CreateBackupBook(completedAction, logMe);
await new BackupLoop(logMe, backupBook, automatedBackupsForm).RunBackupAsync(); await new BackupLoop(logMe, backupBook, automatedBackupsForm).RunBackupAsync();
} }
public static async Task ConvertAllBooksAsync() public static async Task ConvertAllBooksAsync()
@ -86,6 +85,33 @@ namespace LibationWinForms.BookLiberation
await new BackupLoop(logMe, convertBook, automatedBackupsForm).RunBackupAsync(); await new BackupLoop(logMe, convertBook, automatedBackupsForm).RunBackupAsync();
} }
public static async Task BackupAllPdfsAsync(EventHandler<LibraryBook> completedAction = null)
{
Serilog.Log.Logger.Information("Begin " + nameof(BackupAllPdfsAsync));
var automatedBackupsForm = new AutomatedBackupsForm();
LogMe logMe = LogMe.RegisterForm(automatedBackupsForm);
var downloadPdf = CreateStreamProcessable<DownloadPdf, PdfDownloadForm>(completedAction, logMe);
await new BackupLoop(logMe, downloadPdf, automatedBackupsForm).RunBackupAsync();
}
public static void DownloadFile(string url, string destination, bool showDownloadCompletedDialog = false)
{
void onDownloadFileStreamingCompleted(object o, string s)
{
if (showDownloadCompletedDialog)
MessageBox.Show("File downloaded to:\r\n\r\n" + s);
}
DownloadFile downloadFile = CreateStreamable<DownloadFile, DownloadForm>(onDownloadFileStreamingCompleted);
new System.Threading.Thread(() => downloadFile.PerformDownloadFileAsync(url, destination).GetAwaiter().GetResult())
{ 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>(null, logMe);
@ -102,39 +128,11 @@ namespace LibationWinForms.BookLiberation
return downloadDecryptBook; return downloadDecryptBook;
} }
public static async Task BackupAllPdfsAsync(EventHandler<LibraryBook> completedAction = null)
{
Serilog.Log.Logger.Information("Begin " + nameof(BackupAllPdfsAsync));
var automatedBackupsForm = new AutomatedBackupsForm();
LogMe logMe = LogMe.RegisterForm(automatedBackupsForm);
var downloadPdf = CreateStreamProcessable<DownloadPdf, DownloadForm>(completedAction, logMe);
await new BackupLoop(logMe, downloadPdf, automatedBackupsForm).RunBackupAsync();
}
public static void DownloadFile(string url, string destination, bool showDownloadCompletedDialog = false)
{
void OnCompleted(object o, string s)
{
if (showDownloadCompletedDialog)
MessageBox.Show("File downloaded to:\r\n\r\n" + s);
}
(DownloadFile downloadFile, DownloadForm downloadForm) = CreateStreamable<DownloadFile, DownloadForm>(OnCompleted);
new System.Threading.Thread(() =>downloadFile.PerformDownloadFileAsync(url, destination).GetAwaiter().GetResult())
{ IsBackground = true }
.Start();
}
/// <summary> /// <summary>
/// Create a new <see cref="IStreamProcessable"/> and links it to a new <see cref="ProcessBaseForm"/>. /// Create a new <see cref="IStreamProcessable"/> and links it to a new <see cref="ProcessBaseForm"/>.
/// </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"/> and and be Shown on <see cref="IStreamable.StreamingBegin"/></typeparam> /// <typeparam name="TForm">The <see cref="ProcessBaseForm"/> derrived Form to create on <see cref="IProcessable.Begin"/> and and be Shown on <see cref="IStreamable.StreamingBegin"/></typeparam>
/// <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>(EventHandler<LibraryBook> completedAction = null, LogMe logMe = null)
@ -146,7 +144,6 @@ namespace LibationWinForms.BookLiberation
strProc.Begin += (sender, libraryBook) => strProc.Begin += (sender, libraryBook) =>
{ {
var processForm = new TForm(); var processForm = new TForm();
Action<string> logAction = logMe is null ? (s) => { } : logMe.Info;
processForm.SetProcessable(strProc, logMe); processForm.SetProcessable(strProc, logMe);
processForm.OnBegin(sender, libraryBook); processForm.OnBegin(sender, libraryBook);
}; };
@ -161,21 +158,21 @@ namespace LibationWinForms.BookLiberation
/// Creates a new <see cref="IStreamable"/> and links it to a new <see cref="StreamBaseForm"/> /// Creates a new <see cref="IStreamable"/> and links it to a new <see cref="StreamBaseForm"/>
/// </summary> /// </summary>
/// <typeparam name="TStrProc">The <see cref="IStreamable"/> derrived type to create.</typeparam> /// <typeparam name="TStrProc">The <see cref="IStreamable"/> derrived type to create.</typeparam>
/// <typeparam name="TForm">The <see cref="StreamBaseForm"/> derrived form to create, which will be Shown on <see cref="IStreamable.StreamingBegin"/>.</typeparam> /// <typeparam name="TForm">The <see cref="StreamBaseForm"/> derrived Form to create, which will be Shown on <see cref="IStreamable.StreamingBegin"/>.</typeparam>
/// <param name="completedAction">An additional event handler to handle <see cref="IStreamable.StreamingCompleted"/></param> /// <param name="completedAction">An additional event handler to handle <see cref="IStreamable.StreamingCompleted"/></param>
private static (TStrProc, TForm) CreateStreamable<TStrProc, TForm>(EventHandler<string> completedAction = null) private static TStrProc CreateStreamable<TStrProc, TForm>(EventHandler<string> completedAction = null)
where TForm : StreamBaseForm, new() where TForm : StreamBaseForm, new()
where TStrProc : IStreamable, new() where TStrProc : IStreamable, new()
{ {
var strProc = new TStrProc(); var streamable = new TStrProc();
var streamForm = new TForm(); var streamForm = new TForm();
streamForm.SetStreamable(strProc); streamForm.SetStreamable(streamable);
if (completedAction != null) if (completedAction != null)
strProc.StreamingCompleted += completedAction; streamable.StreamingCompleted += completedAction;
return (strProc, streamForm); return streamable;
} }
} }