Extract 'sortable' formatting from GridEntry

This commit is contained in:
Robert McRackan 2022-05-04 13:50:23 -04:00
parent 128facec21
commit ded175f2d2
3 changed files with 32 additions and 17 deletions

View File

@ -51,6 +51,9 @@ namespace DataLayer
: 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; }

27
DataLayer/Formatters.cs Normal file
View File

@ -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;
}
}
}

View File

@ -209,8 +209,8 @@ namespace LibationWinForms
/// </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 },
@ -234,21 +234,6 @@ namespace LibationWinForms
{ 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