diff --git a/Source/FileLiberator/DownloadDecryptBook.cs b/Source/FileLiberator/DownloadDecryptBook.cs index e6112650..a426cff2 100644 --- a/Source/FileLiberator/DownloadDecryptBook.cs +++ b/Source/FileLiberator/DownloadDecryptBook.cs @@ -165,7 +165,7 @@ namespace FileLiberator LameConfig = GetLameOptions(config) }; - var chapters = getChapters(contentLic.ContentMetadata.ChapterInfo.Chapters).OrderBy(c => c.StartOffsetMs).ToList(); + var chapters = flattenChapters(contentLic.ContentMetadata.ChapterInfo.Chapters).OrderBy(c => c.StartOffsetMs).ToList(); if (config.AllowLibationFixup || outputFormat == OutputFormat.Mp3) { @@ -192,7 +192,7 @@ namespace FileLiberator return dlOptions; } - public static List getChapters(IEnumerable chapters) + public static List flattenChapters(IEnumerable chapters, string titleConcat = ": ") { List chaps = new(); @@ -200,19 +200,14 @@ namespace FileLiberator { if (c.Chapters is not null) { - c.Chapters[0] = new AudibleApi.Common.Chapter - { - Title = c.Chapters[0].Title, - StartOffsetMs = c.StartOffsetMs, - StartOffsetSec = c.StartOffsetSec, - LengthMs = c.LengthMs + c.Chapters[0].LengthMs, - Chapters = c.Chapters[0].Chapters - }; + c.Chapters[0].StartOffsetMs = c.StartOffsetMs; + c.Chapters[0].StartOffsetSec = c.StartOffsetSec; + c.Chapters[0].LengthMs += c.LengthMs; - var children = getChapters(c.Chapters); + var children = flattenChapters(c.Chapters); foreach (var child in children) - child.Title = string.IsNullOrEmpty(c.Title) ? child.Title : $"{c.Title}: {child.Title}"; + child.Title = $"{c.Title}{titleConcat}{child.Title}"; chaps.AddRange(children); } diff --git a/Source/LibationWinForms/GridView/ProductsGrid.cs b/Source/LibationWinForms/GridView/ProductsGrid.cs index 578a83b8..35996f27 100644 --- a/Source/LibationWinForms/GridView/ProductsGrid.cs +++ b/Source/LibationWinForms/GridView/ProductsGrid.cs @@ -50,45 +50,52 @@ namespace LibationWinForms.GridView #region Button controls private void DataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e) { - // handle grid button click: https://stackoverflow.com/a/13687844 - if (e.RowIndex < 0) - return; + try + { + // handle grid button click: https://stackoverflow.com/a/13687844 + if (e.RowIndex < 0) + return; - var entry = getGridEntry(e.RowIndex); - if (entry is LibraryBookEntry lbEntry) - { - if (e.ColumnIndex == liberateGVColumn.Index) - LiberateClicked?.Invoke(lbEntry); - else if (e.ColumnIndex == tagAndDetailsGVColumn.Index) - DetailsClicked?.Invoke(lbEntry); - else if (e.ColumnIndex == descriptionGVColumn.Index) - DescriptionClicked?.Invoke(lbEntry, gridEntryDataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false)); - else if (e.ColumnIndex == coverGVColumn.Index) - CoverClicked?.Invoke(lbEntry); - } - else if (entry is SeriesEntry sEntry) - { - if (e.ColumnIndex == liberateGVColumn.Index) + var entry = getGridEntry(e.RowIndex); + if (entry is LibraryBookEntry lbEntry) { - if (sEntry.Liberate.Expanded) - bindingList.CollapseItem(sEntry); - else - bindingList.ExpandItem(sEntry); - - sEntry.NotifyPropertyChanged(nameof(sEntry.Liberate)); - - VisibleCountChanged?.Invoke(this, bindingList.BookEntries().Count()); + if (e.ColumnIndex == liberateGVColumn.Index) + LiberateClicked?.Invoke(lbEntry); + else if (e.ColumnIndex == tagAndDetailsGVColumn.Index) + DetailsClicked?.Invoke(lbEntry); + else if (e.ColumnIndex == descriptionGVColumn.Index) + DescriptionClicked?.Invoke(lbEntry, gridEntryDataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false)); + else if (e.ColumnIndex == coverGVColumn.Index) + CoverClicked?.Invoke(lbEntry); } - else if (e.ColumnIndex == descriptionGVColumn.Index) - DescriptionClicked?.Invoke(sEntry, gridEntryDataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false)); - else if (e.ColumnIndex == coverGVColumn.Index) - CoverClicked?.Invoke(sEntry); - } + else if (entry is SeriesEntry sEntry) + { + if (e.ColumnIndex == liberateGVColumn.Index) + { + if (sEntry.Liberate.Expanded) + bindingList.CollapseItem(sEntry); + else + bindingList.ExpandItem(sEntry); - if (e.ColumnIndex == removeGVColumn.Index) + sEntry.NotifyPropertyChanged(nameof(sEntry.Liberate)); + + VisibleCountChanged?.Invoke(this, bindingList.BookEntries().Count()); + } + else if (e.ColumnIndex == descriptionGVColumn.Index) + DescriptionClicked?.Invoke(sEntry, gridEntryDataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false)); + else if (e.ColumnIndex == coverGVColumn.Index) + CoverClicked?.Invoke(sEntry); + } + + if (e.ColumnIndex == removeGVColumn.Index) + { + gridEntryDataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit); + RemovableCountChanged?.Invoke(this, EventArgs.Empty); + } + } + catch(Exception ex) { - gridEntryDataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit); - RemovableCountChanged?.Invoke(this, EventArgs.Empty); + Serilog.Log.Logger.Error(ex, $"An error was encountered while processing a user click in the {nameof(ProductsGrid)}"); } } diff --git a/Source/_Tests/FileLiberator.Tests/DownloadDecryptBookTests.cs b/Source/_Tests/FileLiberator.Tests/DownloadDecryptBookTests.cs index 2c0726ed..a30e585f 100644 --- a/Source/_Tests/FileLiberator.Tests/DownloadDecryptBookTests.cs +++ b/Source/_Tests/FileLiberator.Tests/DownloadDecryptBookTests.cs @@ -185,11 +185,11 @@ namespace FileLiberator.Tests } }; - var flatChapters = DownloadDecryptBook.getChapters(hierarchicalChapters).OrderBy(c => c.StartOffsetMs).ToArray(); + var flatChapters = DownloadDecryptBook.flattenChapters(hierarchicalChapters); - flatChapters.Length.Should().Be(flatChapters.Length); + flatChapters.Count.Should().Be(expected.Length); - for (int i = 0; i < flatChapters.Length; i++) + for (int i = 0; i < flatChapters.Count; i++) { flatChapters[i].Title.Should().Be(expected[i].Title); flatChapters[i].StartOffsetMs.Should().Be(expected[i].StartOffsetMs);