private static bool validateStatus(StatusHandler statusHandler, AutomatedBackupsForm automatedBackupsForm) { if (statusHandler == null) { automatedBackupsForm.AppendText("Done. All books have been processed"); return(false); } if (statusHandler.HasErrors) { automatedBackupsForm.AppendText("ERROR. All books have not been processed. Most recent valid book: processing failed"); foreach (var errorMessage in statusHandler.Errors) { automatedBackupsForm.AppendText(errorMessage); } return(false); } if (!automatedBackupsForm.KeepGoing) { if (automatedBackupsForm.KeepGoingVisible && !automatedBackupsForm.KeepGoingChecked) { automatedBackupsForm.AppendText("'Keep going' is unchecked"); } return(false); } return(true); }
private static AutomatedBackupsForm attachToBackupsForm(IDownloadableProcessable downloadable) { #region create form var automatedBackupsForm = new AutomatedBackupsForm(); #endregion #region define how model actions will affect form behavior void begin(object _, LibraryBook libraryBook) => automatedBackupsForm.AppendText($"Begin: {libraryBook.Book}"); void statusUpdate(object _, string str) => automatedBackupsForm.AppendText("- " + str); // extra line after book is completely finished void completed(object _, LibraryBook libraryBook) => automatedBackupsForm.AppendText($"Completed: {libraryBook.Book}{Environment.NewLine}"); #endregion #region subscribe new form to model's events downloadable.Begin += begin; downloadable.StatusUpdate += statusUpdate; downloadable.Completed += completed; #endregion #region when form closes, unsubscribe from model's events // unsubscribe so disposed forms aren't still trying to receive notifications automatedBackupsForm.FormClosing += (_, __) => { downloadable.Begin -= begin; downloadable.StatusUpdate -= statusUpdate; downloadable.Completed -= completed; }; #endregion return(automatedBackupsForm); }
private static AutomatedBackupsForm attachToBackupsForm(BackupBook backupBook) { #region create form var automatedBackupsForm = new AutomatedBackupsForm(); #endregion #region define how model actions will affect form behavior void downloadBookBegin(object _, LibraryBook libraryBook) => automatedBackupsForm.AppendText($"Download Step, Begin: {libraryBook.Book}"); void statusUpdate(object _, string str) => automatedBackupsForm.AppendText("- " + str); void downloadBookCompleted(object _, LibraryBook libraryBook) => automatedBackupsForm.AppendText($"Download Step, Completed: {libraryBook.Book}"); void decryptBookBegin(object _, LibraryBook libraryBook) => automatedBackupsForm.AppendText($"Decrypt Step, Begin: {libraryBook.Book}"); // extra line after book is completely finished void decryptBookCompleted(object _, LibraryBook libraryBook) => automatedBackupsForm.AppendText($"Decrypt Step, Completed: {libraryBook.Book}{Environment.NewLine}"); void downloadPdfBegin(object _, LibraryBook libraryBook) => automatedBackupsForm.AppendText($"PDF Step, Begin: {libraryBook.Book}"); // extra line after book is completely finished void downloadPdfCompleted(object _, LibraryBook libraryBook) => automatedBackupsForm.AppendText($"PDF Step, Completed: {libraryBook.Book}{Environment.NewLine}"); #endregion #region subscribe new form to model's events backupBook.DownloadBook.Begin += downloadBookBegin; backupBook.DownloadBook.StatusUpdate += statusUpdate; backupBook.DownloadBook.Completed += downloadBookCompleted; backupBook.DecryptBook.Begin += decryptBookBegin; backupBook.DecryptBook.StatusUpdate += statusUpdate; backupBook.DecryptBook.Completed += decryptBookCompleted; backupBook.DownloadPdf.Begin += downloadPdfBegin; backupBook.DownloadPdf.StatusUpdate += statusUpdate; backupBook.DownloadPdf.Completed += downloadPdfCompleted; #endregion #region when form closes, unsubscribe from model's events // unsubscribe so disposed forms aren't still trying to receive notifications automatedBackupsForm.FormClosing += (_, __) => { backupBook.DownloadBook.Begin -= downloadBookBegin; backupBook.DownloadBook.StatusUpdate -= statusUpdate; backupBook.DownloadBook.Completed -= downloadBookCompleted; backupBook.DecryptBook.Begin -= decryptBookBegin; backupBook.DecryptBook.StatusUpdate -= statusUpdate; backupBook.DecryptBook.Completed -= decryptBookCompleted; backupBook.DownloadPdf.Begin -= downloadPdfBegin; backupBook.DownloadPdf.StatusUpdate -= statusUpdate; backupBook.DownloadPdf.Completed -= downloadPdfCompleted; }; #endregion return(automatedBackupsForm); }
private static async Task runSingleBackupAsync(IProcessable processable, AutomatedBackupsForm automatedBackupsForm, string productId) { automatedBackupsForm.Show(); try { var statusHandler = await processable.ProcessSingleAsync(productId); validateStatus(statusHandler, automatedBackupsForm); } catch (Exception ex) { automatedBackupsForm.AppendError(ex); } automatedBackupsForm.FinalizeUI(); }
// automated backups looper feels like a composible IProcessable: logic, UI, begin + process child + end // however the process step doesn't follow the pattern: Validate(product) + Process(product) private static async Task runBackupLoopAsync(IProcessable processable, AutomatedBackupsForm automatedBackupsForm) { automatedBackupsForm.Show(); try { var shouldContinue = true; while (shouldContinue) { var statusHandler = await processable.ProcessFirstValidAsync(); shouldContinue = validateStatus(statusHandler, automatedBackupsForm); } } catch (Exception ex) { automatedBackupsForm.AppendError(ex); } automatedBackupsForm.FinalizeUI(); }