From 9534969c2d0da1549caa58c01db9e1902b5b4109 Mon Sep 17 00:00:00 2001 From: Robert McRackan Date: Wed, 4 Dec 2019 13:32:25 -0500 Subject: [PATCH] Series should sort irrespective of initial the/a/an (like Title already does) --- LibationWinForm/UNTESTED/GridEntry.cs | 50 +++++++++++++++--------- LibationWinForm/UNTESTED/ProductsGrid.cs | 6 +-- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/LibationWinForm/UNTESTED/GridEntry.cs b/LibationWinForm/UNTESTED/GridEntry.cs index 74eb6255..1955e4f2 100644 --- a/LibationWinForm/UNTESTED/GridEntry.cs +++ b/LibationWinForm/UNTESTED/GridEntry.cs @@ -26,15 +26,15 @@ namespace LibationWinForm [Browsable(false)] public IEnumerable TagsEnumerated => book.UserDefinedItem.TagsEnumerated; - // formatReplacements is what gets displayed + // displayValues is what gets displayed // the value that gets returned from the property is the cell's value // this allows for the value to be sorted one way and displayed another // eg: // orig title: The Computer // formatReplacement: The Computer // value for sorting: Computer - private Dictionary formatReplacements { get; } = new Dictionary(); - public bool TryGetFormatted(string key, out string value) => formatReplacements.TryGetValue(key, out value); + private Dictionary displayValues { get; } = new Dictionary(); + public bool TryDisplayValue(string key, out string value) => displayValues.TryGetValue(key, out value); public Image Cover => WindowsDesktopUtilities.WinAudibleImageServer.GetImage(book.PictureId, FileManager.PictureSize._80x80); @@ -43,16 +43,8 @@ namespace LibationWinForm { get { - formatReplacements[nameof(Title)] = book.Title; - - var sortName = book.Title - .Replace("|", "") - .Replace(":", "") - .ToLowerInvariant(); - if (sortName.StartsWith("the ") || sortName.StartsWith("a ") || sortName.StartsWith("an ")) - sortName = sortName.Substring(sortName.IndexOf(" ") + 1); - - return sortName; + displayValues[nameof(Title)] = book.Title; + return getSortName(book.Title); } } @@ -63,7 +55,7 @@ namespace LibationWinForm { get { - formatReplacements[nameof(Length)] + displayValues[nameof(Length)] = book.LengthInMinutes == 0 ? "" : $"{book.LengthInMinutes / 60} hr {book.LengthInMinutes % 60} min"; @@ -72,7 +64,29 @@ namespace LibationWinForm } } - public string Series => book.SeriesNames; + public string Series + { + get + { + displayValues[nameof(Series)] = book.SeriesNames; + return getSortName(book.SeriesNames); + } + } + + private static string[] sortPrefixIgnores { get; } = new[] { "the", "a", "an" }; + private static string getSortName(string unformattedName) + { + var sortName = unformattedName + .Replace("|", "") + .Replace(":", "") + .ToLowerInvariant() + .Trim(); + + if (sortPrefixIgnores.Any(prefix => sortName.StartsWith(prefix + " "))) + sortName = sortName.Substring(sortName.IndexOf(" ") + 1).TrimStart(); + + return sortName; + } private string descriptionCache = null; public string Description @@ -111,7 +125,7 @@ namespace LibationWinForm { get { - formatReplacements[nameof(Product_Rating)] = starString(book.Rating); + displayValues[nameof(Product_Rating)] = starString(book.Rating); return firstScore(book.Rating); } } @@ -120,7 +134,7 @@ namespace LibationWinForm { get { - formatReplacements[nameof(Purchase_Date)] = libraryBook.DateAdded.ToString("d"); + displayValues[nameof(Purchase_Date)] = libraryBook.DateAdded.ToString("d"); return libraryBook.DateAdded.ToString("yyyy-MM-dd HH:mm:ss"); } } @@ -129,7 +143,7 @@ namespace LibationWinForm { get { - formatReplacements[nameof(My_Rating)] = starString(book.UserDefinedItem.Rating); + displayValues[nameof(My_Rating)] = starString(book.UserDefinedItem.Rating); return firstScore(book.UserDefinedItem.Rating); } } diff --git a/LibationWinForm/UNTESTED/ProductsGrid.cs b/LibationWinForm/UNTESTED/ProductsGrid.cs index 5608c295..7d3de721 100644 --- a/LibationWinForm/UNTESTED/ProductsGrid.cs +++ b/LibationWinForm/UNTESTED/ProductsGrid.cs @@ -179,7 +179,7 @@ namespace LibationWinForm var liveGridEntry = getGridEntry(e.RowIndex); // EditTagsDialog should display better-formatted title - liveGridEntry.TryGetFormatted(nameof(liveGridEntry.Title), out string value); + liveGridEntry.TryDisplayValue(nameof(liveGridEntry.Title), out string value); var editTagsForm = new EditTagsDialog(value, liveGridEntry.Tags); if (editTagsForm.ShowDialog() != DialogResult.OK) @@ -201,11 +201,11 @@ namespace LibationWinForm private void replaceFormatted(object sender, DataGridViewCellFormattingEventArgs e) { var col = ((DataGridView)sender).Columns[e.ColumnIndex]; - if (col is DataGridViewTextBoxColumn textCol && getGridEntry(e.RowIndex).TryGetFormatted(textCol.Name, out string value)) + if (col is DataGridViewTextBoxColumn textCol && getGridEntry(e.RowIndex).TryDisplayValue(textCol.Name, out string value)) e.Value = value; } - private void hiddenFormatting(object sender, DataGridViewCellFormattingEventArgs e) + private void hiddenFormatting(object _, DataGridViewCellFormattingEventArgs e) { var isHidden = getGridEntry(e.RowIndex).TagsEnumerated.Contains("hidden");