Skip to content

Commit

Permalink
Fix #915
Browse files Browse the repository at this point in the history
  • Loading branch information
SirSparkles committed Jul 6, 2023
1 parent cbd9533 commit 6500b58
Show file tree
Hide file tree
Showing 12 changed files with 504 additions and 301 deletions.
147 changes: 146 additions & 1 deletion TVRename/Forms/ListViewActionItemSorter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using BrightIdeasSoftware;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;

namespace TVRename;

Expand Down Expand Up @@ -48,6 +49,150 @@ public int Compare(OLVListItem? x, OLVListItem? y)
return xIsNull ? -1 : 1;
}

return new ActionItemSorter().Compare(x1, y1);
return Polarity() * Sorter.Compare(x1, y1);
}

/// <summary>
/// Class constructor. Initializes various elements
/// </summary>
public ListViewActionItemSorter()
{
// Initialize the column to '0'
SortColumn = 0;

// Initialize the sort order to 'none'
Order = SortOrder.None;

// Initialize the CaseInsensitiveComparer object
Sorter = new DefaultActionItemSorter();
}

private int Polarity()
{
return Order switch
{
// Calculate correct return value based on object comparison
SortOrder.Ascending =>
// Ascending sort is selected, return normal result of compare operation
1,
SortOrder.Descending =>
// Descending sort is selected, return negative result of compare operation
-1,
SortOrder.None =>
// Return '0' to indicate they are equal
0,
_ => 0
};
}

/// <summary>
/// Gets or sets the number of the column to which to apply the sorting operation (Defaults to '0').
/// </summary>
public int SortColumn { set; get; }

/// <summary>
/// Gets or sets the order of sorting to apply (for example, 'Ascending' or 'Descending').
/// </summary>
public SortOrder Order { set; get; }

/// <summary>
/// Case insensitive comparer object
/// </summary>
public ActionItemSorter Sorter { get; set; }

public void ClickedOn(int col, ActionItemSorter sorter)
{
Sorter = sorter;

// Determine if clicked column is already the column that is being sorted.
if (col == SortColumn)
{
// Reverse the current sort direction for this column.
Order = Order == SortOrder.Ascending ? SortOrder.Descending : SortOrder.Ascending;
}
else
{
// Set the column number that is to be sorted; default to ascending.
SortColumn = col;
Order = SortOrder.Ascending;
}
}
}

internal class OlvActionGroupComparer : IComparer<OLVListItem>
{
/// <summary>
/// Gets or sets the order of sorting to apply (for example, 'Ascending' or 'Descending').
/// </summary>
public SortOrder Order { set; get; }

/// <summary>
/// Case insensitive comparer object
/// </summary>
public ActionItemSorter Sorter { get; set; }

/// <summary>
/// Class constructor. Initializes various elements
/// </summary>
public OlvActionGroupComparer(ActionItemSorter sorter, SortOrder order)
{
// Initialize the sort order to 'none'
Order = order;

// Initialize the CaseInsensitiveComparer object
Sorter = sorter;
}

/// <summary>
/// Compare two rows
/// </summary>
/// <param name="x">row1</param>
/// <param name="y">row2</param>
/// <returns>An ordering indication: -1, 0, 1</returns>
public int Compare(object? x, object? y) => Compare(x as OLVListItem, y as OLVListItem);

/// <summary>
/// Compare two rows
/// </summary>
/// <param name="x">row1</param>
/// <param name="y">row2</param>
/// <returns>An ordering indication: -1, 0, 1</returns>
public int Compare(OLVListItem? x, OLVListItem? y)
{
Item? x1 = x?.RowObject as Item;
Item? y1 = y?.RowObject as Item;

// Handle nulls. Null values come last
bool xIsNull = x1 == null;
bool yIsNull = y1 == null;
if (xIsNull || yIsNull)
{
if (xIsNull && yIsNull)
{
return 0;
}

return xIsNull ? -1 : 1;
}

return Polarity() * Sorter.Compare(x1, y1);
}

private int Polarity()
{
return Order switch
{
// Calculate correct return value based on object comparison
SortOrder.Ascending =>
// Ascending sort is selected, return normal result of compare operation
1,
SortOrder.Descending =>
// Descending sort is selected, return negative result of compare operation
-1,
SortOrder.None =>
// Return '0' to indicate they are equal
0,
_ => 0
};
}
}
66 changes: 66 additions & 0 deletions TVRename/Forms/SeasonGroupComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using BrightIdeasSoftware;

namespace TVRename.Forms;

/// <summary>
/// This comparer sort list view specifically 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)
{
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 (sortOrder == SortOrder.Descending)
{
return 0 - result;
}

return result;
}

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

private readonly SortOrder sortOrder;
}
4 changes: 2 additions & 2 deletions TVRename/Forms/Tools/Recommendations/RecommendationView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,11 @@ private void lvRecommendations_ItemSelectionChanged(object sender, ListViewItemS
}
if (rr.Movie != null)
{
UI.SetHtmlBody(chrRecommendationPreview, rr.Movie.GetMovieHtmlOverview(rr));
chrRecommendationPreview.SetHtmlBody(rr.Movie.GetMovieHtmlOverview(rr));
}
else if (rr.Series != null)
{
UI.SetHtmlBody(chrRecommendationPreview, rr.Series.GetShowHtmlOverview(rr));
chrRecommendationPreview.SetHtmlBody(rr.Series.GetShowHtmlOverview(rr));
}
}
private void this_FormClosing(object sender, FormClosingEventArgs e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,7 @@ private void lvRecommendations_ItemSelectionChanged(object sender, ListViewItemS
{
if (e.Item is BrightIdeasSoftware.OLVListItem { RowObject: YtsRecommendationRow rr })
{
UI.SetHtmlBody(chrRecommendationPreview,
rr.Movie != null
chrRecommendationPreview.SetHtmlBody(rr.Movie != null
? rr.Movie.GetMovieHtmlOverview(false)
: rr.YtsMovie.GetMovieHtmlOverview());
}
Expand Down
3 changes: 1 addition & 2 deletions TVRename/Forms/Tools/YTSRecomendations/YtsViewerView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ private void lvRecommendations_ItemSelectionChanged(object sender, ListViewItemS
object? rowObject = (e.Item as BrightIdeasSoftware.OLVListItem)?.RowObject;
if (rowObject is YtsViewerRow rr)
{
UI.SetHtmlBody(chrRecommendationPreview,
rr.Movie != null
chrRecommendationPreview.SetHtmlBody(rr.Movie != null
? rr.Movie.GetMovieHtmlOverview(false)
: rr.YtsMovie.GetMovieHtmlOverview());
}
Expand Down
2 changes: 0 additions & 2 deletions TVRename/Forms/UI.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6500b58

Please sign in to comment.