Skip to content

Commit

Permalink
Extract 'sortable' formatting from GridEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
rmcrackan committed May 4, 2022
1 parent 128face commit ded175f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
3 changes: 3 additions & 0 deletions DataLayer/EfClasses/Book.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public string[] CategoriesIds
: Category.ParentCategory is null ? new[] { Category.AudibleCategoryId }
: new[] { Category.ParentCategory.AudibleCategoryId, Category.AudibleCategoryId };

public string TitleSortable => Formatters.GetSortName(Title);
public string SeriesSortable => Formatters.GetSortName(SeriesNames);

// is owned, not optional 1:1
public UserDefinedItem UserDefinedItem { get; private set; }

Expand Down
27 changes: 27 additions & 0 deletions DataLayer/Formatters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace DataLayer
{
internal class Formatters
{
private static string[] _sortPrefixIgnores { get; } = { "the", "a", "an" };

public 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;
}
}
}
19 changes: 2 additions & 17 deletions LibationWinForms/grid/GridEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ private void Commit()
/// </summary>
private Dictionary<string, Func<object>> CreateMemberValueDictionary() => new()
{
{ nameof(Title), () => GetSortName(Book.Title) },
{ nameof(Series), () => GetSortName(Book.SeriesNames) },
{ nameof(Title), () => Book.TitleSortable },
{ nameof(Series), () => Book.SeriesSortable },
{ nameof(Length), () => Book.LengthInMinutes },
{ nameof(MyRating), () => Book.UserDefinedItem.Rating.FirstScore },
{ nameof(PurchaseDate), () => LibraryBook.DateAdded },
Expand All @@ -234,21 +234,6 @@ private void Commit()
{ typeof(LiberatedStatus), new ObjectComparer<LiberatedStatus>() },
};

private static readonly string[] _sortPrefixIgnores = { "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;
}

#endregion

#region Static library display functions
Expand Down

0 comments on commit ded175f

Please sign in to comment.