diff --git a/LibationWinForms/BookLiberation/ProcessorAutomationController.cs b/LibationWinForms/BookLiberation/ProcessorAutomationController.cs index 1b6455e2..da02e3c2 100644 --- a/LibationWinForms/BookLiberation/ProcessorAutomationController.cs +++ b/LibationWinForms/BookLiberation/ProcessorAutomationController.cs @@ -47,13 +47,14 @@ namespace LibationWinForms.BookLiberation Serilog.Log.Logger.Information("Begin " + nameof(BackupSingleBookAsync) + " {@DebugInfo}", new { productId }); var backupBook = getWiredUpBackupBook(completedAction); - - (AutomatedBackupsForm automatedBackupsForm, LogMe logMe) = attachToBackupsForm(backupBook); - automatedBackupsForm.KeepGoingVisible = false; + + (Action unsibscribeEvents, LogMe logMe) = attachToBackupsForm(backupBook); var libraryBook = IProcessableExt.GetSingleLibraryBook(productId); // continue even if libraryBook is null. we'll display even that in the processing box - await new BackupSingle(logMe, backupBook, automatedBackupsForm, libraryBook).RunBackupAsync(); + await new BackupSingle(logMe, backupBook, libraryBook).RunBackupAsync(); + + unsibscribeEvents(); } public static async Task BackupAllBooksAsync(EventHandler completedAction = null) @@ -61,8 +62,12 @@ namespace LibationWinForms.BookLiberation Serilog.Log.Logger.Information("Begin " + nameof(BackupAllBooksAsync)); var backupBook = getWiredUpBackupBook(completedAction); + var automatedBackupsForm = new AutomatedBackupsForm(); + + (Action unsibscribeEvents, LogMe logMe) = attachToBackupsForm(backupBook, automatedBackupsForm); + + automatedBackupsForm.FormClosing += (_, __) => unsibscribeEvents(); - (AutomatedBackupsForm automatedBackupsForm, LogMe logMe) = attachToBackupsForm(backupBook); await new BackupLoop(logMe, backupBook, automatedBackupsForm).RunBackupAsync(); } @@ -93,11 +98,10 @@ namespace LibationWinForms.BookLiberation private static void updateIsLiberated(object sender, LibraryBook e) => ApplicationServices.SearchEngineCommands.UpdateIsLiberated(e.Book); - private static (AutomatedBackupsForm, LogMe) attachToBackupsForm(BackupBook backupBook) + private static (Action unsibscribeEvents, LogMe) attachToBackupsForm(BackupBook backupBook, AutomatedBackupsForm automatedBackupsForm = null) { - #region create form and logger - var automatedBackupsForm = new AutomatedBackupsForm(); - var logMe = LogMe.RegisterForm(automatedBackupsForm); + #region create logger + var logMe = automatedBackupsForm is null? new LogMe() : LogMe.RegisterForm(automatedBackupsForm); #endregion #region define how model actions will affect form behavior @@ -121,7 +125,7 @@ namespace LibationWinForms.BookLiberation #region when form closes, unsubscribe from model's events // unsubscribe so disposed forms aren't still trying to receive notifications - automatedBackupsForm.FormClosing += (_, __) => + Action unsibscribe = () => { backupBook.DecryptBook.Begin -= decryptBookBegin; backupBook.DecryptBook.StatusUpdate -= statusUpdate; @@ -132,7 +136,7 @@ namespace LibationWinForms.BookLiberation }; #endregion - return (automatedBackupsForm, logMe); + return (unsibscribe, logMe); } public static async Task BackupAllPdfsAsync(EventHandler completedAction = null) @@ -367,7 +371,7 @@ namespace LibationWinForms.BookLiberation protected IProcessable Processable { get; } protected AutomatedBackupsForm AutomatedBackupsForm { get; } - protected BackupRunner(LogMe logMe, IProcessable processable, AutomatedBackupsForm automatedBackupsForm) + protected BackupRunner(LogMe logMe, IProcessable processable, AutomatedBackupsForm automatedBackupsForm = null) { LogMe = logMe; Processable = processable; @@ -382,7 +386,7 @@ namespace LibationWinForms.BookLiberation public async Task RunBackupAsync() { - AutomatedBackupsForm.Show(); + AutomatedBackupsForm?.Show(); try { @@ -393,7 +397,7 @@ namespace LibationWinForms.BookLiberation LogMe.Error(ex); } - AutomatedBackupsForm.FinalizeUI(); + AutomatedBackupsForm?.FinalizeUI(); LogMe.Info("DONE"); } @@ -454,8 +458,8 @@ An error occurred while trying to process this book. Skip this book permanently? protected override MessageBoxButtons SkipDialogButtons => MessageBoxButtons.YesNo; protected override DialogResult CreateSkipFileResult => DialogResult.Yes; - public BackupSingle(LogMe logMe, IProcessable processable, AutomatedBackupsForm automatedBackupsForm, LibraryBook libraryBook) - : base(logMe, processable, automatedBackupsForm) + public BackupSingle(LogMe logMe, IProcessable processable, LibraryBook libraryBook) + : base(logMe, processable) { _libraryBook = libraryBook; } diff --git a/LibationWinForms/ProductsGrid.cs b/LibationWinForms/ProductsGrid.cs index 87f7e9f8..2e837432 100644 --- a/LibationWinForms/ProductsGrid.cs +++ b/LibationWinForms/ProductsGrid.cs @@ -182,15 +182,7 @@ namespace LibationWinForms return; } - // not liberated: liberate - var msg - = "Liberate entire library instead?" - + "\r\n\r\nClick Yes to begin liberating your entire library" - + "\r\n\r\nClick No to liberate this book only"; - if (MessageBox.Show(msg, "Liberate entire library?", MessageBoxButtons.YesNo) == DialogResult.Yes) - await BookLiberation.ProcessorAutomationController.BackupAllBooksAsync((_, libraryBook) => RefreshRow(libraryBook.Book.AudibleProductId)); - else - await BookLiberation.ProcessorAutomationController.BackupSingleBookAsync(productId, (_, __) => RefreshRow(productId)); + await BookLiberation.ProcessorAutomationController.BackupSingleBookAsync(productId, (_, __) => RefreshRow(productId)); } #endregion