Implement library/listview sorting
- Issue MatterHackers/MCCentral#2865 Add sorting fields to ILibraryItem as promised by sort menu
This commit is contained in:
parent
0657611a73
commit
52a143e955
2 changed files with 67 additions and 18 deletions
|
|
@ -142,7 +142,38 @@ namespace MatterHackers.MatterControl.CustomWidgets
|
|||
ModifiedDate
|
||||
}
|
||||
|
||||
public SortKey ActiveSort { get; set; } = SortKey.Name;
|
||||
private SortKey _activeSort = SortKey.Name;
|
||||
public SortKey ActiveSort
|
||||
{
|
||||
get => _activeSort;
|
||||
set
|
||||
{
|
||||
if (_activeSort != value)
|
||||
{
|
||||
_activeSort = value;
|
||||
this.ApplySort();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool _ascending = true;
|
||||
public bool Ascending
|
||||
{
|
||||
get => _ascending;
|
||||
set
|
||||
{
|
||||
if (_ascending != value)
|
||||
{
|
||||
_ascending = value;
|
||||
this.ApplySort();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplySort()
|
||||
{
|
||||
this.Reload().ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Empties the list children and repopulates the list with the source container content
|
||||
|
|
@ -175,14 +206,15 @@ namespace MatterHackers.MatterControl.CustomWidgets
|
|||
|
||||
itemsContentView.BeginReload();
|
||||
|
||||
var containerItems = from item in sourceContainer.ChildContainers
|
||||
IEnumerable<ILibraryItem> containerItems = from item in sourceContainer.ChildContainers
|
||||
where item.IsVisible && this.ContainerFilter(item)
|
||||
orderby item.Name
|
||||
select item;
|
||||
|
||||
// Folder items
|
||||
foreach (var childContainer in containerItems)
|
||||
foreach (var childContainer in this.SortItems(containerItems))
|
||||
{
|
||||
Console.WriteLine($"{childContainer.Name}:{childContainer.DateCreated}");
|
||||
|
||||
var listViewItem = new ListViewItem(childContainer, this);
|
||||
listViewItem.DoubleClick += listViewItem_DoubleClick;
|
||||
items.Add(listViewItem);
|
||||
|
|
@ -200,20 +232,9 @@ namespace MatterHackers.MatterControl.CustomWidgets
|
|||
&& this.ItemFilter(item)
|
||||
select item;
|
||||
|
||||
filteredResults = filteredResults.OrderBy(item =>
|
||||
{
|
||||
switch (ActiveSort)
|
||||
{
|
||||
case SortKey.Name:
|
||||
return item.Name;
|
||||
|
||||
default:
|
||||
return item.Name;
|
||||
}
|
||||
});
|
||||
|
||||
foreach (var item in filteredResults)
|
||||
foreach (var item in this.SortItems(filteredResults))
|
||||
{
|
||||
Console.WriteLine($"{item.Name}:{item.DateCreated}");
|
||||
var listViewItem = new ListViewItem(item, this);
|
||||
listViewItem.DoubleClick += listViewItem_DoubleClick;
|
||||
items.Add(listViewItem);
|
||||
|
|
@ -223,7 +244,6 @@ namespace MatterHackers.MatterControl.CustomWidgets
|
|||
}
|
||||
|
||||
itemsContentView.EndReload();
|
||||
|
||||
}
|
||||
|
||||
if (sourceContainer is ILibraryWritableContainer writableContainer)
|
||||
|
|
@ -236,6 +256,30 @@ namespace MatterHackers.MatterControl.CustomWidgets
|
|||
this.ContentReloaded?.Invoke(this, null);
|
||||
}
|
||||
|
||||
private IEnumerable<ILibraryItem> SortItems(IEnumerable<ILibraryItem> items)
|
||||
{
|
||||
switch (ActiveSort)
|
||||
{
|
||||
case SortKey.CreatedDate when this.Ascending:
|
||||
return items.OrderBy(item => item.DateCreated);
|
||||
|
||||
case SortKey.CreatedDate when !this.Ascending:
|
||||
return items.OrderByDescending(item => item.DateCreated);
|
||||
|
||||
case SortKey.ModifiedDate when this.Ascending:
|
||||
return items.OrderBy(item => item.DateModified);
|
||||
|
||||
case SortKey.ModifiedDate when !this.Ascending:
|
||||
return items.OrderByDescending(item => item.DateModified);
|
||||
|
||||
case SortKey.Name when !this.Ascending:
|
||||
return items.OrderByDescending(item => item.Name);
|
||||
|
||||
default:
|
||||
return items.OrderBy(item => item.Name);
|
||||
}
|
||||
}
|
||||
|
||||
private void WritableContainer_ItemContentChanged(object sender, ItemChangedEventArgs e)
|
||||
{
|
||||
var firstItem = items.Where(i => i.Model.ID == e.LibraryItem.ID).FirstOrDefault();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue