From 30ba69eca7ffe562249cf88b7b7cce291fb47e1c Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Wed, 8 Jun 2022 08:52:25 -0600 Subject: [PATCH] Minor refactoring. --- Source/FileLiberator/Processable.cs | 3 +-- Source/LibationWinForms/GridView/ProductsGrid.cs | 14 +++++++------- .../LibationWinForms/GridView/QueryExtensions.cs | 3 +++ 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Source/FileLiberator/Processable.cs b/Source/FileLiberator/Processable.cs index ef1bb139..b8b984ff 100644 --- a/Source/FileLiberator/Processable.cs +++ b/Source/FileLiberator/Processable.cs @@ -29,8 +29,7 @@ namespace FileLiberator public IEnumerable GetValidLibraryBooks(IEnumerable library) => library.Where(libraryBook => Validate(libraryBook) - && libraryBook.Book.ContentType != ContentType.Parent - && (libraryBook.Book.ContentType != ContentType.Episode || Configuration.Instance.DownloadEpisodes) + && (libraryBook.Book.ContentType != ContentType.Episode || LibationFileManager.Configuration.Instance.DownloadEpisodes) ); public async Task ProcessSingleAsync(LibraryBook libraryBook, bool validate) diff --git a/Source/LibationWinForms/GridView/ProductsGrid.cs b/Source/LibationWinForms/GridView/ProductsGrid.cs index 6305722f..ff058f92 100644 --- a/Source/LibationWinForms/GridView/ProductsGrid.cs +++ b/Source/LibationWinForms/GridView/ProductsGrid.cs @@ -93,12 +93,11 @@ namespace LibationWinForms.GridView { var geList = dbBooks.Where(lb => lb.IsProduct()).Select(b => new LibraryBookEntry(b)).Cast().ToList(); - var parents = dbBooks.Where(lb => lb.IsEpisodeParent()); var episodes = dbBooks.Where(lb => lb.IsEpisodeChild()); - foreach (var parent in parents) + foreach (var parent in dbBooks.Where(lb => lb.IsEpisodeParent())) { - var seriesEpisodes = episodes.Where(lb => lb.Book.SeriesLink?.Any(s => s.Series.AudibleSeriesId == parent.Book.AudibleProductId) == true).ToList(); + var seriesEpisodes = episodes.FindChildren(parent).ToList(); if (!seriesEpisodes.Any()) continue; @@ -112,7 +111,7 @@ namespace LibationWinForms.GridView bindingList.CollapseAll(); syncBindingSource.DataSource = bindingList; VisibleCountChanged?.Invoke(this, bindingList.BookEntries().Count()); - } + } internal void UpdateGrid(List dbBooks) { @@ -133,10 +132,10 @@ namespace LibationWinForms.GridView { var existingEntry = allEntries.FindByAsin(libraryBook.Book.AudibleProductId); - if (libraryBook.IsEpisodeChild()) - AddOrUpdateEpisode(libraryBook, existingEntry, seriesEntries, dbBooks); - else if (libraryBook.IsProduct()) + if (libraryBook.IsProduct()) AddOrUpdateBook(libraryBook, existingEntry); + else if(libraryBook.IsEpisodeChild()) + AddOrUpdateEpisode(libraryBook, existingEntry, seriesEntries, dbBooks); } bindingList.SuspendFilteringOnUpdate = false; @@ -198,6 +197,7 @@ namespace LibationWinForms.GridView if (seriesBook is null) { + //This should be impossible because the importer ensures every episode has a parent. var ex = new ApplicationException($"Episode's series parent not found in database."); var seriesLinks = string.Join("\r\n", episodeBook.Book.SeriesLink?.Select(sb => $"{nameof(sb.Series.Name)}={sb.Series.Name}, {nameof(sb.Series.AudibleSeriesId)}={sb.Series.AudibleSeriesId}")); Serilog.Log.Logger.Error(ex, "Episode={episodeBook}, Series: {seriesLinks}", episodeBook, seriesLinks); diff --git a/Source/LibationWinForms/GridView/QueryExtensions.cs b/Source/LibationWinForms/GridView/QueryExtensions.cs index e6df05cb..c6257014 100644 --- a/Source/LibationWinForms/GridView/QueryExtensions.cs +++ b/Source/LibationWinForms/GridView/QueryExtensions.cs @@ -8,6 +8,9 @@ namespace LibationWinForms.GridView #nullable enable internal static class QueryExtensions { + public static IEnumerable FindChildren(this IEnumerable bookList, LibraryBook parent) + => bookList.Where(lb => lb.Book.SeriesLink?.Any(s => s.Series.AudibleSeriesId == parent.Book.AudibleProductId) == true); + public static IEnumerable BookEntries(this IEnumerable gridEntries) => gridEntries.OfType();