From f8f5eac109847f4d05af2ec370833eae1d959b16 Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Fri, 10 Jun 2022 20:45:10 -0600 Subject: [PATCH] Refactor --- Source/LibationWinForms/Form1.RemoveBooks.cs | 104 ++++++++++++++++++ Source/LibationWinForms/Form1.ScanManual.cs | 89 +-------------- Source/LibationWinForms/Form1.cs | 1 + .../GridView/ProductsDisplay.Designer.cs | 2 +- .../GridView/ProductsDisplay.cs | 12 +- .../LibationWinForms/GridView/ProductsGrid.cs | 4 +- 6 files changed, 117 insertions(+), 95 deletions(-) create mode 100644 Source/LibationWinForms/Form1.RemoveBooks.cs diff --git a/Source/LibationWinForms/Form1.RemoveBooks.cs b/Source/LibationWinForms/Form1.RemoveBooks.cs new file mode 100644 index 00000000..35fe0b38 --- /dev/null +++ b/Source/LibationWinForms/Form1.RemoveBooks.cs @@ -0,0 +1,104 @@ +using AudibleUtilities; +using LibationWinForms.Dialogs; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace LibationWinForms +{ + public partial class Form1 + { + private ToolStripButton removeCheckedBtn = new(); + public void Configure_RemoveBooks() + { + + #region Create and Add Tool Strip Button + removeCheckedBtn.DisplayStyle = ToolStripItemDisplayStyle.Text; + removeCheckedBtn.Name = "removeSelectedBtn"; + removeCheckedBtn.Text = "Remove 0 Books"; + removeCheckedBtn.AutoToolTip = false; + removeCheckedBtn.ToolTipText = "Remove checked books and series\r\nfrom Libation's database.\r\n\r\nThey will remain in your Audible account."; + removeCheckedBtn.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; + removeCheckedBtn.Alignment = ToolStripItemAlignment.Left; + removeCheckedBtn.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; + removeCheckedBtn.Font = new System.Drawing.Font(removeCheckedBtn.Font, System.Drawing.FontStyle.Bold); + removeCheckedBtn.Click += (_, _) => productsDisplay.RemoveCheckedBooksAsync(); + removeCheckedBtn.Visible = false; + statusStrip1.Items.Insert(1, removeCheckedBtn); + #endregion + } + + private void removeLibraryBooksToolStripMenuItem_Click(object sender, EventArgs e) + { + // if 0 accounts, this will not be visible + // if 1 account, run scanLibrariesRemovedBooks() on this account + // if multiple accounts, another menu set will open. do not run scanLibrariesRemovedBooks() + using var persister = AudibleApiStorage.GetAccountsSettingsPersister(); + var accounts = persister.AccountsSettings.GetAll(); + + if (accounts.Count != 1) + return; + + var firstAccount = accounts.Single(); + scanLibrariesRemovedBooks(firstAccount); + } + + // selectively remove books from all accounts + private void removeAllAccountsToolStripMenuItem_Click(object sender, EventArgs e) + { + using var persister = AudibleApiStorage.GetAccountsSettingsPersister(); + var allAccounts = persister.AccountsSettings.GetAll(); + scanLibrariesRemovedBooks(allAccounts.ToArray()); + } + + // selectively remove books from some accounts + private void removeSomeAccountsToolStripMenuItem_Click(object sender, EventArgs e) + { + using var scanAccountsDialog = new ScanAccountsDialog(); + + if (scanAccountsDialog.ShowDialog() != DialogResult.OK) + return; + + if (!scanAccountsDialog.CheckedAccounts.Any()) + return; + + scanLibrariesRemovedBooks(scanAccountsDialog.CheckedAccounts.ToArray()); + } + + private async void scanLibrariesRemovedBooks(params Account[] accounts) + { + //This action is meant to operate on the entire library. + //For removing books within a filter set, use + //Visible Books > Remove from library + filterSearchTb.Enabled = false; + productsDisplay.Filter(null); + + removeCheckedBtn.Visible = true; + closeRemoveBooksColumnToolStripMenuItem.Visible = true; + await productsDisplay.ScanAndRemoveBooksAsync(accounts); + } + + private void closeRemoveBooksColumnToolStripMenuItem_Click(object sender, EventArgs e) + { + removeCheckedBtn.Visible = false; + closeRemoveBooksColumnToolStripMenuItem.Visible = false; + productsDisplay.CloseRemoveBooksColumn(); + + //Restore the filter + filterSearchTb.Enabled = true; + performFilter(filterSearchTb.Text); + } + + private void productsDisplay_RemovableCountChanged(object sender, int removeCount) + { + removeCheckedBtn.Text = removeCount switch + { + 1 => "Remove 1 Book", + _ => $"Remove {removeCount} Books" + }; + } + } +} diff --git a/Source/LibationWinForms/Form1.ScanManual.cs b/Source/LibationWinForms/Form1.ScanManual.cs index 1228979a..b90c8826 100644 --- a/Source/LibationWinForms/Form1.ScanManual.cs +++ b/Source/LibationWinForms/Form1.ScanManual.cs @@ -13,27 +13,10 @@ namespace LibationWinForms // this is for manual scan/import. Unrelated to auto-scan public partial class Form1 { - - private ToolStripButton removeCheckedBtn = new(); private void Configure_ScanManual() { this.Load += refreshImportMenu; AccountsSettingsPersister.Saved += refreshImportMenu; - - #region Create and Add Tool Strip Button - removeCheckedBtn.DisplayStyle = ToolStripItemDisplayStyle.Text; - removeCheckedBtn.Name = "removeSelectedBtn"; - removeCheckedBtn.Text = "Remove 0 Books"; - removeCheckedBtn.AutoToolTip = false; - removeCheckedBtn.ToolTipText = "Remove checked books and series\r\nfrom Libation's database.\r\n\r\nThey will remain in your Audible account."; - removeCheckedBtn.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; - removeCheckedBtn.Alignment = ToolStripItemAlignment.Left; - removeCheckedBtn.Anchor = AnchorStyles.Bottom | AnchorStyles.Left; - removeCheckedBtn.Font = new System.Drawing.Font(removeCheckedBtn.Font, System.Drawing.FontStyle.Bold); - removeCheckedBtn.Click += (_, _) => productsDisplay.RemoveCheckedBooksAsync(); - removeCheckedBtn.Visible = false; - statusStrip1.Items.Insert(1, removeCheckedBtn); - #endregion } private void refreshImportMenu(object _, EventArgs __) @@ -84,77 +67,7 @@ namespace LibationWinForms return; await scanLibrariesAsync(scanAccountsDialog.CheckedAccounts); - } - - private void removeLibraryBooksToolStripMenuItem_Click(object sender, EventArgs e) - { - // if 0 accounts, this will not be visible - // if 1 account, run scanLibrariesRemovedBooks() on this account - // if multiple accounts, another menu set will open. do not run scanLibrariesRemovedBooks() - using var persister = AudibleApiStorage.GetAccountsSettingsPersister(); - var accounts = persister.AccountsSettings.GetAll(); - - if (accounts.Count != 1) - return; - - var firstAccount = accounts.Single(); - scanLibrariesRemovedBooks(firstAccount); - } - - // selectively remove books from all accounts - private void removeAllAccountsToolStripMenuItem_Click(object sender, EventArgs e) - { - using var persister = AudibleApiStorage.GetAccountsSettingsPersister(); - var allAccounts = persister.AccountsSettings.GetAll(); - scanLibrariesRemovedBooks(allAccounts.ToArray()); - } - - // selectively remove books from some accounts - private void removeSomeAccountsToolStripMenuItem_Click(object sender, EventArgs e) - { - using var scanAccountsDialog = new ScanAccountsDialog(); - - if (scanAccountsDialog.ShowDialog() != DialogResult.OK) - return; - - if (!scanAccountsDialog.CheckedAccounts.Any()) - return; - - scanLibrariesRemovedBooks(scanAccountsDialog.CheckedAccounts.ToArray()); - } - - private async void scanLibrariesRemovedBooks(params Account[] accounts) - { - //This action is meant to operate on the entire library. - //For removing books within a filter set, use - //Visible Books > Remove from library - filterSearchTb.Enabled = false; - productsDisplay.Filter(null); - - removeCheckedBtn.Visible = true; - closeRemoveBooksColumnToolStripMenuItem.Visible = true; - await productsDisplay.ScanAndRemoveBooksAsync(accounts); - } - - private void closeRemoveBooksColumnToolStripMenuItem_Click(object sender, EventArgs e) - { - removeCheckedBtn.Visible = false; - closeRemoveBooksColumnToolStripMenuItem.Visible = false; - productsDisplay.CloseRemoveBooksColumn(); - - //Restore the filter - filterSearchTb.Enabled = true; - performFilter(filterSearchTb.Text); - } - - private void productsDisplay_RemovableCountChanged(object sender, int removeCount) - { - removeCheckedBtn.Text = removeCount switch - { - 1 => "Remove 1 Book", - _ => $"Remove {removeCount} Books" - }; - } + } private async Task scanLibrariesAsync(IEnumerable accounts) => await scanLibrariesAsync(accounts.ToArray()); private async Task scanLibrariesAsync(params Account[] accounts) diff --git a/Source/LibationWinForms/Form1.cs b/Source/LibationWinForms/Form1.cs index 8eb15705..068e7cac 100644 --- a/Source/LibationWinForms/Form1.cs +++ b/Source/LibationWinForms/Form1.cs @@ -44,6 +44,7 @@ namespace LibationWinForms Configure_VisibleBooks(); Configure_QuickFilters(); Configure_ScanManual(); + Configure_RemoveBooks(); Configure_Liberate(); Configure_Export(); Configure_Settings(); diff --git a/Source/LibationWinForms/GridView/ProductsDisplay.Designer.cs b/Source/LibationWinForms/GridView/ProductsDisplay.Designer.cs index abd5823e..b84d670c 100644 --- a/Source/LibationWinForms/GridView/ProductsDisplay.Designer.cs +++ b/Source/LibationWinForms/GridView/ProductsDisplay.Designer.cs @@ -44,7 +44,7 @@ this.productsGrid.CoverClicked += new LibationWinForms.GridView.GridEntryClickedEventHandler(this.productsGrid_CoverClicked); this.productsGrid.DetailsClicked += new LibationWinForms.GridView.LibraryBookEntryClickedEventHandler(this.productsGrid_DetailsClicked); this.productsGrid.DescriptionClicked += new LibationWinForms.GridView.GridEntryRectangleClickedEventHandler(this.productsGrid_DescriptionClicked); - this.productsGrid.RemovableCountChanged += new LibationWinForms.GridView.GridEntryClickedEventHandler(this.productsGrid_RemovableCountChanged); + this.productsGrid.RemovableCountChanged += new System.EventHandler(this.productsGrid_RemovableCountChanged); // // ProductsDisplay // diff --git a/Source/LibationWinForms/GridView/ProductsDisplay.cs b/Source/LibationWinForms/GridView/ProductsDisplay.cs index a86657c5..4487a8bd 100644 --- a/Source/LibationWinForms/GridView/ProductsDisplay.cs +++ b/Source/LibationWinForms/GridView/ProductsDisplay.cs @@ -82,7 +82,7 @@ namespace LibationWinForms.GridView #endregion - #region UI display functions + #region Scan and Remove Books public void CloseRemoveBooksColumn() => productsGrid.RemoveColumnVisible = false; @@ -127,7 +127,7 @@ namespace LibationWinForms.GridView foreach (var r in removable) r.Remove = RemoveStatus.Removed; - productsGrid_RemovableCountChanged(null); + productsGrid_RemovableCountChanged(this, null); } catch (Exception ex) { @@ -139,6 +139,10 @@ namespace LibationWinForms.GridView } } + #endregion + + #region UI display functions + public void Display() { try @@ -183,9 +187,9 @@ namespace LibationWinForms.GridView LiberateClicked?.Invoke(this, liveGridEntry.LibraryBook); } - private void productsGrid_RemovableCountChanged(GridEntry liveGridEntry) + private void productsGrid_RemovableCountChanged(object sender, EventArgs e) { - RemovableCountChanged?.Invoke(this, productsGrid.GetAllBookEntries().Count(lbe => lbe.Remove is RemoveStatus.Removed)); + RemovableCountChanged?.Invoke(sender, productsGrid.GetAllBookEntries().Count(lbe => lbe.Remove is RemoveStatus.Removed)); } } } diff --git a/Source/LibationWinForms/GridView/ProductsGrid.cs b/Source/LibationWinForms/GridView/ProductsGrid.cs index 77a6d295..eea3dc67 100644 --- a/Source/LibationWinForms/GridView/ProductsGrid.cs +++ b/Source/LibationWinForms/GridView/ProductsGrid.cs @@ -23,7 +23,7 @@ namespace LibationWinForms.GridView public event LibraryBookEntryClickedEventHandler DetailsClicked; public event GridEntryRectangleClickedEventHandler DescriptionClicked; public new event EventHandler Scroll; - public event GridEntryClickedEventHandler RemovableCountChanged; + public event EventHandler RemovableCountChanged; private GridEntryBindingList bindingList; internal IEnumerable GetVisibleBooks() @@ -88,7 +88,7 @@ namespace LibationWinForms.GridView if (e.ColumnIndex == removeGVColumn.Index) { gridEntryDataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit); - RemovableCountChanged?.Invoke(entry); + RemovableCountChanged?.Invoke(this, EventArgs.Empty); } }