Skip to content

Commit

Permalink
Support multilevel sort of message list
Browse files Browse the repository at this point in the history
  • Loading branch information
Dijji committed May 20, 2020
1 parent 7ccd7cc commit bff5d7b
Showing 1 changed file with 58 additions and 26 deletions.
84 changes: 58 additions & 26 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public partial class MainWindow : Window
private View view = new View();
private XstFile xstFile = null;
private List<string> tempFileNames = new List<string>();
private GridViewColumnHeader listViewSortCol = null;
private SortAdorner listViewSortAdorner = null;
private int searchIndex = -1;

public MainWindow()
Expand Down Expand Up @@ -144,14 +142,7 @@ private void treeFolders_SelectedItemChanged(object sender, RoutedPropertyChange
});

// If there is no sort in effect, sort by date in descending order
if (listViewSortCol == null)
{
string tag = "Date";
listViewSortCol = ((GridView)listMessages.View).Columns.Select(c => (GridViewColumnHeader)c.Header).Where(h => h.Tag.ToString() == tag).First();
listViewSortAdorner = new SortAdorner(listViewSortCol, ListSortDirection.Descending);
AdornerLayer.GetAdornerLayer(listViewSortCol).Add(listViewSortAdorner);
listMessages.Items.SortDescriptions.Add(new SortDescription(tag, ListSortDirection.Descending));
}
SortMessages("Date", ListSortDirection.Descending, ifNoneAlready: true);
}
}
catch (Exception ex)
Expand Down Expand Up @@ -185,22 +176,8 @@ private void listMessagesColumnHeader_Click(object sender, RoutedEventArgs e)
{
// Sort the messages by the clicked on column
GridViewColumnHeader column = (sender as GridViewColumnHeader);
string sortBy = column.Tag.ToString();
if (listViewSortCol != null)
{
AdornerLayer.GetAdornerLayer(listViewSortCol).Remove(listViewSortAdorner);
listMessages.Items.SortDescriptions.Clear();
}

ListSortDirection newDir = ListSortDirection.Ascending;
if (listViewSortCol == column && listViewSortAdorner.Direction == newDir)
newDir = ListSortDirection.Descending;

listViewSortCol = column;
listViewSortAdorner = new SortAdorner(listViewSortCol, newDir);
AdornerLayer.GetAdornerLayer(listViewSortCol).Add(listViewSortAdorner);
listMessages.Items.SortDescriptions.Add(new SortDescription(sortBy, newDir));

SortMessages(column.Tag.ToString(), ListSortDirection.Ascending);

searchIndex = listMessages.SelectedIndex;
listMessages.ScrollIntoView(listMessages.SelectedItem);
}
Expand Down Expand Up @@ -255,6 +232,61 @@ private void exportEmail_Executed(object sender, ExecutedRoutedEventArgs e)
}
}

private void SortMessages(string property, ListSortDirection direction, bool ifNoneAlready = false)
{
var targetCol = ((GridView)listMessages.View).Columns.Select(c => (GridViewColumnHeader)c.Header)
.First(h => h.Tag.ToString() == property);

// See if we have any sorts applied, in which case, we may be done
var sorts = listMessages.Items.SortDescriptions;
if (ifNoneAlready && sorts.Count > 0)
return;

// Look for a sort on the target columm
var matches = sorts.Where(s => s.PropertyName == property);
if (matches.Count() > 0)
{
// If there is one, just toggle it
var sort = matches.First();
direction = ((sort.Direction == ListSortDirection.Ascending) ?
ListSortDirection.Descending : ListSortDirection.Ascending);
sorts.Remove(sort);
}
//else
//{
// // If there is not one, see if we have the maximum number of sorts applied already
// // The algorithm works for any limit with no changes other than this test
// if (sorts.Count >= 2)
// {
// // If so, remove the oldest one
// var oldSort = sorts.Last();
// var oldCol = ((GridView)listMessages.View).Columns.Select(c => (GridViewColumnHeader)c.Header)
// .First(h => h.Tag.ToString() == oldSort.PropertyName);
// sorts.Remove(oldSort);

// // And the adorner that went with it
// var oldAdorners = AdornerLayer.GetAdornerLayer(oldCol);
// var oldAdorner = oldAdorners.GetAdorners(oldCol)?.Cast<SortAdorner>()?.FirstOrDefault(s => s != null);
// if (oldAdorner != null)
// oldAdorners.Remove(oldAdorner);
// }
//}

// Apply the requested sort as the dominant one, whatever it was before
sorts.Insert(0, new SortDescription(property, direction));

// Find any sort adorner applied to the target column
var adorners = AdornerLayer.GetAdornerLayer(targetCol);
var adorner = adorners.GetAdorners(targetCol)?.Cast<SortAdorner>()?.FirstOrDefault(s => s != null);
// If there is one, remove it
if (adorner != null)
adorners.Remove(adorner);

// Create and apply the requested adorner
adorner = new SortAdorner(targetCol, direction);
adorners.Add(adorner);
}

private void ExportEmails(IEnumerable<Message>messages)
{
string folderName = GetEmailsExportFolderName();
Expand Down

0 comments on commit bff5d7b

Please sign in to comment.