Skip to content

Commit

Permalink
refactor: Move ListView sort logic to behavior class
Browse files Browse the repository at this point in the history
Moved ListView sorting logic from code-behind to a new behavior class `ListViewClickSortBehavior`.
  • Loading branch information
DineshSolanki committed Jul 26, 2024
1 parent cb051e5 commit a0eccec
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 136 deletions.
77 changes: 77 additions & 0 deletions FoliCon/Modules/UI/ListViewClickSortBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System.Windows.Controls.Primitives;
using FoliCon.Modules.utils;

namespace FoliCon.Modules.UI;

public class ListViewClickSortBehavior : Behavior<ListView>
{
private GridViewColumnHeader _lastHeaderClicked;
private ListSortDirection _lastDirection = ListSortDirection.Ascending;

protected override void OnAttached()
{
base.OnAttached();

AssociatedObject.AddHandler(ButtonBase.ClickEvent, new RoutedEventHandler(OnClick));
((INotifyCollectionChanged) AssociatedObject.Items).CollectionChanged += ListView_CollectionChanged;
}

protected override void OnDetaching()
{
base.OnDetaching();

AssociatedObject.RemoveHandler(ButtonBase.ClickEvent, new RoutedEventHandler(OnClick));
((INotifyCollectionChanged) AssociatedObject.Items).CollectionChanged -= ListView_CollectionChanged;
}

private void ListView_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Reset)
{
UiUtils.SetColumnWidth(AssociatedObject);
}
}

private void OnClick(object sender, RoutedEventArgs e)
{
if (e.OriginalSource is not GridViewColumnHeader headerClicked) return;
if (headerClicked.Role == GridViewColumnHeaderRole.Padding) return;

ListSortDirection direction;
if (headerClicked != _lastHeaderClicked)
{
direction = ListSortDirection.Ascending;
}
else
{
direction = _lastDirection == ListSortDirection.Ascending
? ListSortDirection.Descending
: ListSortDirection.Ascending;
}

var header = headerClicked.Column.Header as string;
Sort(header, direction);

headerClicked.Column.HeaderTemplate = direction == ListSortDirection.Ascending
? Application.Current.Resources["HeaderTemplateArrowUp"] as DataTemplate
: Application.Current.Resources["HeaderTemplateArrowDown"] as DataTemplate;

// Remove arrow from previously sorted header
if (_lastHeaderClicked != null && _lastHeaderClicked != headerClicked)
{
_lastHeaderClicked.Column.HeaderTemplate = null;
}

_lastHeaderClicked = headerClicked;
_lastDirection = direction;
}

private void Sort(string sortBy, ListSortDirection direction)
{
var dataView = CollectionViewSource.GetDefaultView(AssociatedObject.ItemsSource);
dataView.SortDescriptions.Clear();
var sd = new SortDescription(sortBy, direction);
dataView.SortDescriptions.Add(sd);
dataView.Refresh();
}
}
6 changes: 5 additions & 1 deletion FoliCon/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
xmlns:extension="clr-namespace:FoliCon.Modules.Extension"
xmlns:ui="clr-namespace:FoliCon.Modules.UI"
xmlns:enums="clr-namespace:FoliCon.Models.Enums"
xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
mc:Ignorable="d"
prism:ViewModelLocator.AutoWireViewModel="True"
Title="{Binding Title}" Height="714" Width="851" MinWidth="850" MinHeight="630"
Expand Down Expand Up @@ -243,12 +244,15 @@
<ListView x:Name="FinalList" Grid.Row="3" Grid.ColumnSpan="2" Grid.Column="0"
ItemsSource="{Binding FinalListViewData.Data}"
SelectedItem="{Binding FinalListViewData.SelectedItem}"
hc:Empty.ShowEmpty="true" GridViewColumnHeader.Click="FinalList_OnClick" >
hc:Empty.ShowEmpty="true">
<hc:Interaction.Triggers>
<hc:EventTrigger EventName="MouseDoubleClick">
<hc:LaunchUriOrFileAction Path="{Binding FinalListViewData.SelectedItem.Folder}" />
</hc:EventTrigger>
</hc:Interaction.Triggers>
<b:Interaction.Behaviors>
<ui:ListViewClickSortBehavior/>
</b:Interaction.Behaviors>
<ListView.View>
<GridView>
<GridViewColumn Header="{extension:Lang Key={x:Static langs:LangKeys.Title}}" Width="Auto" DisplayMemberBinding="{Binding Title}" />
Expand Down
65 changes: 1 addition & 64 deletions FoliCon/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,11 @@ namespace FoliCon.Views;
/// </summary>
public partial class MainWindow
{
private GridViewColumnHeader _lastHeaderClicked;
private ListSortDirection _lastDirection = ListSortDirection.Ascending;
public MainWindow()
{
InitializeComponent();
((INotifyCollectionChanged)FinalList.Items).CollectionChanged += ListView_CollectionChanged;
}

private void ListView_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{

if (e.Action == NotifyCollectionChangedAction.Reset)
{
UiUtils.SetColumnWidth(FinalList);
}
}


private void CmbLanguage_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
Expand All @@ -46,58 +35,6 @@ private void CmbLanguage_OnSelectionChanged(object sender, SelectionChangedEvent

}

private void FinalList_OnClick(object sender, RoutedEventArgs e)
{
if (e.OriginalSource is not GridViewColumnHeader headerClicked)
{
return;
}

if (headerClicked.Role == GridViewColumnHeaderRole.Padding)
{
return;
}

ListSortDirection direction;
if (headerClicked != _lastHeaderClicked)
{
direction = ListSortDirection.Ascending;
}
else
{
direction = _lastDirection == ListSortDirection.Ascending
? ListSortDirection.Descending
: ListSortDirection.Ascending;
}

var header = headerClicked.Column.Header as string;
Sort(header, direction);

headerClicked.Column.HeaderTemplate = direction == ListSortDirection.Ascending
? Application.Current.Resources["HeaderTemplateArrowUp"] as DataTemplate
: Application.Current.Resources["HeaderTemplateArrowDown"] as DataTemplate;

// Remove arrow from previously sorted header
if (_lastHeaderClicked != null && _lastHeaderClicked != headerClicked)
{
_lastHeaderClicked.Column.HeaderTemplate = null;
}


_lastHeaderClicked = headerClicked;
_lastDirection = direction;
}
private void Sort(string sortBy, ListSortDirection direction)
{
var dataView =
CollectionViewSource.GetDefaultView(FinalList.ItemsSource);

dataView.SortDescriptions.Clear();
var sd = new SortDescription(sortBy, direction);
dataView.SortDescriptions.Add(sd);
dataView.Refresh();
}

private void MainWindow_OnClosed(object sender, EventArgs e)
{
FileUtils.DeleteFoliConTempDeviationDirectory();
Expand Down
6 changes: 5 additions & 1 deletion FoliCon/Views/SearchResult.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
xmlns:convertor="clr-namespace:FoliCon.Modules.Convertor"
xmlns:extension="clr-namespace:FoliCon.Modules.Extension"
xmlns:data="clr-namespace:FoliCon.Models.Data"
xmlns:ui="clr-namespace:FoliCon.Modules.UI"
mc:Ignorable="d"
prism:ViewModelLocator.AutoWireViewModel="True"
d:DataContext="{d:DesignInstance viewModels:SearchResultViewModel }" Unloaded="UserControl_Unloaded" Background="{DynamicResource RegionBrush}">
Expand Down Expand Up @@ -54,7 +55,7 @@
<Button Content="{extension:Lang Key={x:Static langs:LangKeys.Skip}}" HorizontalAlignment="Right" VerticalAlignment="Top" Grid.Row="0" Style="{StaticResource ButtonDanger}"
Command="{Binding SkipCommand}" IsCancel="True" ToolTip="{extension:Lang Key={x:Static langs:LangKeys.SkipThisTitle}}" />
<ListView x:Name="ListViewResult" ItemsSource="{Binding ResultListViewData.Data}" Background="{DynamicResource PrimaryRegionBrush}"
SelectedItem="{Binding ResultListViewData.SelectedItem}" IsSynchronizedWithCurrentItem="True" Grid.Row="1" hc:Empty.ShowEmpty="true" GridViewColumnHeader.Click="ListViewResult_OnClick">
SelectedItem="{Binding ResultListViewData.SelectedItem}" IsSynchronizedWithCurrentItem="True" Grid.Row="1" hc:Empty.ShowEmpty="true">
<hc:Interaction.Triggers>
<hc:EventTrigger EventName="MouseDoubleClick">
<hc:EventToCommand Command="{Binding PickCommand}" PassEventArgsToCommand="True"/>
Expand All @@ -63,6 +64,9 @@
<hc:EventToCommand Command="{Binding SelectionChanged}" PassEventArgsToCommand="True" />
</hc:EventTrigger>
</hc:Interaction.Triggers>
<i:Interaction.Behaviors>
<ui:ListViewClickSortBehavior/>
</i:Interaction.Behaviors>
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Title}" Width="auto" Header="{extension:Lang Key={x:Static langs:LangKeys.Title}}"/>
Expand Down
73 changes: 3 additions & 70 deletions FoliCon/Views/SearchResult.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,84 +1,17 @@
using FoliCon.Modules.utils;

namespace FoliCon.Views;
namespace FoliCon.Views;

/// <summary>
/// Interaction logic for SearchResult
/// </summary>
public partial class SearchResult
{
private GridViewColumnHeader _lastHeaderClicked;
private ListSortDirection _lastDirection = ListSortDirection.Ascending;

public SearchResult()
{
InitializeComponent();
((INotifyCollectionChanged)ListViewResult.Items).CollectionChanged += ListView_CollectionChanged;
}

private void ListView_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Reset)
{
UiUtils.SetColumnWidth(ListViewResult);
}
}

private void ListViewResult_OnClick(object sender, RoutedEventArgs e)
private void UserControl_Unloaded(object sender, RoutedEventArgs e)
{
if (e.OriginalSource is not GridViewColumnHeader headerClicked)
{
return;
}

if (headerClicked.Role == GridViewColumnHeaderRole.Padding)
{
return;
}

ListSortDirection direction;
if (headerClicked != _lastHeaderClicked)
{
direction = ListSortDirection.Ascending;
}
else
{
direction = _lastDirection == ListSortDirection.Ascending
? ListSortDirection.Descending
: ListSortDirection.Ascending;
}

var header = headerClicked.Column.Header as string;
Sort(header, direction);

headerClicked.Column.HeaderTemplate = direction == ListSortDirection.Ascending
? Application.Current.Resources["HeaderTemplateArrowUp"] as DataTemplate
: Application.Current.Resources["HeaderTemplateArrowDown"] as DataTemplate;

// Remove arrow from previously sorted header
if (_lastHeaderClicked != null && _lastHeaderClicked != headerClicked)
{
_lastHeaderClicked.Column.HeaderTemplate = null;
}


_lastHeaderClicked = headerClicked;
_lastDirection = direction;
WebBox.Browser.Dispose();
}

private void Sort(string sortBy, ListSortDirection direction)
{
var dataView =
CollectionViewSource.GetDefaultView(ListViewResult.ItemsSource);

dataView.SortDescriptions.Clear();
var sd = new SortDescription(sortBy, direction);
dataView.SortDescriptions.Add(sd);
dataView.Refresh();
}

private void UserControl_Unloaded(object sender, RoutedEventArgs e)
{
WebBox.Browser.Dispose();
}
}

0 comments on commit a0eccec

Please sign in to comment.