diff --git a/Source/LibationAvalonia/ViewModels/ProductsDisplayViewModel.cs b/Source/LibationAvalonia/ViewModels/ProductsDisplayViewModel.cs index a973b63c..242c965a 100644 --- a/Source/LibationAvalonia/ViewModels/ProductsDisplayViewModel.cs +++ b/Source/LibationAvalonia/ViewModels/ProductsDisplayViewModel.cs @@ -118,7 +118,7 @@ namespace LibationAvalonia.ViewModels //Create the filtered-in list before adding entries to avoid a refresh FilteredInGridEntries = geList.Union(geList.OfType().SelectMany(s => s.Children)).FilterEntries(FilterString); - SOURCE.AddRange(geList.OrderByDescending(e => e.DateAdded)); + SOURCE.AddRange(geList.OrderDescending(new RowComparer(null))); //Add all children beneath their parent foreach (var series in SOURCE.OfType().ToList()) diff --git a/Source/LibationAvalonia/ViewModels/RowComparer.cs b/Source/LibationAvalonia/ViewModels/RowComparer.cs index ef3170d8..a9d40378 100644 --- a/Source/LibationAvalonia/ViewModels/RowComparer.cs +++ b/Source/LibationAvalonia/ViewModels/RowComparer.cs @@ -16,11 +16,13 @@ namespace LibationAvalonia.ViewModels public RowComparer(DataGridColumn column) { Column = column; - PropertyName = Column.SortMemberPath; + PropertyName = Column?.SortMemberPath ?? nameof(IGridEntry.DateAdded); } //Avalonia doesn't expose the column's CurrentSortingState, so we must get it through reflection - protected override ListSortDirection? GetSortOrder() - => CurrentSortingStatePi.GetValue(HeaderCellPi.GetValue(Column)) as ListSortDirection?; + protected override ListSortDirection GetSortOrder() + => Column is null ? ListSortDirection.Descending + : CurrentSortingStatePi.GetValue(HeaderCellPi.GetValue(Column)) is ListSortDirection lsd ? lsd + : ListSortDirection.Descending; } } diff --git a/Source/LibationUiBase/GridView/EntryStatus.cs b/Source/LibationUiBase/GridView/EntryStatus.cs index 71f04ccf..a7fbcc78 100644 --- a/Source/LibationUiBase/GridView/EntryStatus.cs +++ b/Source/LibationUiBase/GridView/EntryStatus.cs @@ -58,7 +58,7 @@ namespace LibationUiBase.GridView public abstract object BackgroundBrush { get; } public object ButtonImage => GetLiberateIcon(); public string ToolTip => GetTooltip(); - internal Book Book { get; } + private Book Book { get; } private DateTime lastBookUpdate; private LiberatedStatus bookStatus; diff --git a/Source/LibationUiBase/GridView/RowComparerBase.cs b/Source/LibationUiBase/GridView/RowComparerBase.cs index ed33e985..a24cf5d3 100644 --- a/Source/LibationUiBase/GridView/RowComparerBase.cs +++ b/Source/LibationUiBase/GridView/RowComparerBase.cs @@ -61,16 +61,10 @@ namespace LibationUiBase.GridView { //Podcast episodes usually all have the same PurchaseDate and DateAdded property: //the date that the series was added to the library. So when sorting by PurchaseDate - //and DateAdded, compare SeriesOrder instead. - // - //Note that DateAdded is not a grid column and users cannot sort by that property. - //Entries are only sorted by DateAdded as a default sorting order when no other - //sorting column has been chose. We negate the comparison so that episodes are listed - //in ascending order. + //and DateAdded, compare SeriesOrder instead.. return PropertyName switch { - nameof(IGridEntry.PurchaseDate) => geA.SeriesOrder.CompareTo(geB.SeriesOrder), - nameof(IGridEntry.DateAdded) => geA.SeriesOrder.CompareTo(geB.SeriesOrder) * (GetSortOrder() is ListSortDirection.Descending ? 1 : -1), + nameof(IGridEntry.DateAdded) or nameof (IGridEntry.PurchaseDate) => geA.SeriesOrder.CompareTo(geB.SeriesOrder), _ => InternalCompare(geA, geB), }; } @@ -79,7 +73,7 @@ namespace LibationUiBase.GridView return InternalCompare(parentA, parentB); } - protected abstract ListSortDirection? GetSortOrder(); + protected abstract ListSortDirection GetSortOrder(); private int InternalCompare(IGridEntry x, IGridEntry y) { diff --git a/Source/LibationUiBase/GridView/SeriesEntry[TStatus].cs b/Source/LibationUiBase/GridView/SeriesEntry[TStatus].cs index f3ed63fc..eaf49fcb 100644 --- a/Source/LibationUiBase/GridView/SeriesEntry[TStatus].cs +++ b/Source/LibationUiBase/GridView/SeriesEntry[TStatus].cs @@ -47,7 +47,7 @@ namespace LibationUiBase.GridView Children = children .Select(c => new LibraryBookEntry(c, this)) - .OrderBy(c => c.SeriesIndex) + .OrderByDescending(c => c.SeriesOrder) .ToList(); UpdateLibraryBook(parent); diff --git a/Source/LibationWinForms/GridView/GridEntryBindingList.cs b/Source/LibationWinForms/GridView/GridEntryBindingList.cs index 871d4513..463ce41a 100644 --- a/Source/LibationWinForms/GridView/GridEntryBindingList.cs +++ b/Source/LibationWinForms/GridView/GridEntryBindingList.cs @@ -1,5 +1,4 @@ using ApplicationServices; -using Dinah.Core.DataBinding; using LibationUiBase.GridView; using System; using System.Collections.Generic; @@ -25,6 +24,8 @@ namespace LibationWinForms.GridView public GridEntryBindingList(IEnumerable enumeration) : base(new List(enumeration)) { SearchEngineCommands.SearchEngineUpdated += SearchEngineCommands_SearchEngineUpdated; + + refreshEntries(); } @@ -216,9 +217,7 @@ namespace LibationWinForms.GridView { var itemsList = (List)Items; //User Order/OrderDescending and replace items in list instead of using List.Sort() to achieve stable sorting. - var sortedItems - = Comparer.SortOrder == ListSortDirection.Ascending ? itemsList.Order(Comparer).ToList() - : itemsList.OrderDescending(Comparer).ToList(); + var sortedItems = Comparer.OrderEntries(itemsList).ToList(); itemsList.Clear(); itemsList.AddRange(sortedItems); diff --git a/Source/LibationWinForms/GridView/ProductsGrid.cs b/Source/LibationWinForms/GridView/ProductsGrid.cs index 54a82439..ba24cd8e 100644 --- a/Source/LibationWinForms/GridView/ProductsGrid.cs +++ b/Source/LibationWinForms/GridView/ProductsGrid.cs @@ -181,7 +181,7 @@ namespace LibationWinForms.GridView geList.AddRange(seriesEntry.Children); } - bindingList = new GridEntryBindingList(geList.OrderByDescending(e => e.DateAdded)); + bindingList = new GridEntryBindingList(geList); bindingList.CollapseAll(); syncBindingSource.DataSource = bindingList; VisibleCountChanged?.Invoke(this, bindingList.GetFilteredInItems().Count()); diff --git a/Source/LibationWinForms/GridView/RowComparer.cs b/Source/LibationWinForms/GridView/RowComparer.cs index 3758fa00..0e344578 100644 --- a/Source/LibationWinForms/GridView/RowComparer.cs +++ b/Source/LibationWinForms/GridView/RowComparer.cs @@ -1,12 +1,20 @@ using LibationUiBase.GridView; +using System.Collections.Generic; using System.ComponentModel; +using System.Linq; namespace LibationWinForms.GridView { internal class RowComparer : RowComparerBase { - public ListSortDirection? SortOrder { get; set; } - public override string PropertyName { get; set; } - protected override ListSortDirection? GetSortOrder() => SortOrder; + public ListSortDirection SortOrder { get; set; } = ListSortDirection.Descending; + public override string PropertyName { get; set; } = nameof(IGridEntry.DateAdded); + protected override ListSortDirection GetSortOrder() => SortOrder; + + /// + /// Helper method for ordering grid entries + /// + public IOrderedEnumerable OrderEntries(IEnumerable entries) + => SortOrder is ListSortDirection.Descending ? entries.OrderDescending(this) : entries.Order(this); } }