Skip to content

Commit

Permalink
Try to improve sorting of Items in the actions list
Browse files Browse the repository at this point in the history
  • Loading branch information
SirSparkles committed Jun 13, 2022
1 parent 30558e0 commit 6257d75
Show file tree
Hide file tree
Showing 13 changed files with 1,005 additions and 880 deletions.
4 changes: 2 additions & 2 deletions TVRename/Exporter/MissingXML.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ protected override void Do()

writer.WriteElement("id", missing.MissingEpisode.Show.TvdbCode);
writer.WriteElement("title", missing.MissingEpisode.TheCachedSeries.Name);
writer.WriteElement("season", Helpers.Pad(missing.MissingEpisode.AppropriateSeasonNumber));
writer.WriteElement("episode", Helpers.Pad(missing.MissingEpisode.AppropriateEpNum));
writer.WriteElement("season", missing.MissingEpisode.AppropriateSeasonNumber.Pad());
writer.WriteElement("episode", missing.MissingEpisode.AppropriateEpNum.Pad());
writer.WriteElement("episodeName", missing.MissingEpisode.Name);
writer.WriteElement("description", missing.MissingEpisode.Overview);

Expand Down
4 changes: 2 additions & 2 deletions TVRename/Exporter/UpcomingXML.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ protected override bool Generate([NotNull] System.IO.Stream str, [NotNull] IEnum
writer.WriteElement("ShowTvMazeId", ei.Show.TVmazeCode);
writer.WriteElement("ShowIMDB", ei.Show.ImdbCode);
writer.WriteElement("SeriesName", ei.Show.Name);
writer.WriteElement("SeasonNumber", Helpers.Pad(ei.AppropriateSeasonNumber));
writer.WriteElement("EpisodeNumber", Helpers.Pad(ei.AppropriateEpNum));
writer.WriteElement("SeasonNumber", ei.AppropriateSeasonNumber.Pad());
writer.WriteElement("EpisodeNumber", ei.AppropriateEpNum.Pad());
writer.WriteElement("EpisodeName", ei.Name);

writer.WriteStartElement("available");
Expand Down
184 changes: 92 additions & 92 deletions TVRename/Forms/UI.Designer.cs

Large diffs are not rendered by default.

68 changes: 67 additions & 1 deletion TVRename/Forms/UI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5008,12 +5008,17 @@ private void olvAction_BeforeCreatingGroups(object sender, [NotNull] CreateGroup
if (e.Parameters.PrimarySort == olvEpisode)
{
e.Parameters.PrimarySort = new OLVColumn("RawEp", "EpisodeNumber");
e.Parameters.GroupComparer = new SeasonGroupComparer(e.Parameters.GroupByOrder);
}

if (e.Parameters.PrimarySort == olvSeason)
if (e.Parameters.PrimarySort == olvSeason || e.Parameters.PrimarySort.AspectName == "SeasonNumberAsInt")
{
e.Parameters.PrimarySort = new OLVColumn("RawSe", "SeasonNumberAsInt");
e.Parameters.GroupComparer = new SeasonGroupComparer(e.Parameters.GroupByOrder);
}

e.Parameters.SecondarySort = new OLVColumn("key", "OrderKey");
e.Parameters.SecondarySortOrder = SortOrder.Ascending;
}

[NotNull]
Expand Down Expand Up @@ -5362,6 +5367,8 @@ private void olvAction_BeforeSorting(object sender, [NotNull] BeforeSortingEvent
e.ColumnToSort = new OLVColumn("RawSe", "SeasonNumberAsInt");
}

e.SecondaryColumnToSort = new OLVColumn("AbsoluteOrder", "OrderKey");
e.SecondarySortOrder = SortOrder.Ascending;
olvAction.ShowSortIndicator();
}

Expand All @@ -5385,4 +5392,63 @@ private void downloadInstallerToolStripMenuItem_Click(object sender, EventArgs e
Helpers.OpenUrl("https://go.microsoft.com/fwlink/p/?LinkId=2124703");
}
}

/// <summary>
/// This comparer sort list view specificallly for sesaons so that they appear in the season order
/// OLVGroups have a "SortValue" property,
/// which is used if present. Otherwise, the titles of the groups will be compared.
/// </summary>
public class SeasonGroupComparer : IComparer<OLVGroup>
{
/// <summary>
/// Create a group comparer
/// </summary>
/// <param name="order">The ordering for column values</param>
public SeasonGroupComparer(SortOrder order)
{
this.sortOrder = order;
}

/// <summary>
/// Compare the two groups. OLVGroups have a "SortValue" property,
/// which is used if present. Otherwise, the titles of the groups will be compared.
/// </summary>
/// <param name="x">group1</param>
/// <param name="y">group2</param>
/// <returns>An ordering indication: -1, 0, 1</returns>
public int Compare(OLVGroup x, OLVGroup y)
{
if (x is null || y is null)
{
return 0;
}

// If we can compare the sort values, do that.
// Otherwise do a case insensitive compare on the group header.
int result;
if (x.Items.Any() && y.Items.Any())
{
result = CompareValue(x).CompareTo(CompareValue(y));
}
else if (x.SortValue != null && y.SortValue != null)
{
result = x.SortValue.CompareTo(y.SortValue);
}
else
{
result = string.Compare(x.Header, y.Header, StringComparison.CurrentCultureIgnoreCase);
}

if (this.sortOrder == SortOrder.Descending)
{
return 0 - result;
}

return result;
}

private int CompareValue([NotNull] OLVGroup x) => ((Item)x.Items.First().RowObject).SeasonNumberAsInt ?? 0;

private readonly SortOrder sortOrder;
}
}
Loading

0 comments on commit 6257d75

Please sign in to comment.