Skip to content

Commit

Permalink
fixed IndexOutOfBound error on posterMode,
Browse files Browse the repository at this point in the history
Sorting added on MainWindow.
  • Loading branch information
DineshSolanki committed Aug 10, 2021
1 parent 4a7771d commit 845b129
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 25 deletions.
5 changes: 5 additions & 0 deletions FoliCon/Modules/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -624,5 +624,10 @@ public static (string ID, string MediaType) ReadMediaInfo(string folderPath)
var mediaInfo = (ID: id, MediaType: mediaType);
return mediaInfo;
}

public static int GetResultCount(bool isPickedById, dynamic result, string searchMode)
{
return isPickedById ? result != null ? 1 : 0 : searchMode == "Game" ? result.Length : result.TotalResults;
}
}
}
25 changes: 11 additions & 14 deletions FoliCon/ViewModels/SearchResultViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using FoliCon.Properties.Langs;
using HandyControl.Tools.Extension;
using DelegateCommand = Prism.Commands.DelegateCommand;
using MessageBox = HandyControl.Controls.MessageBox;
using HandyControl.Tools.Command;
using Prism.Commands;

namespace FoliCon.ViewModels
Expand Down Expand Up @@ -114,8 +112,7 @@ public bool IsSearchFocused

#region Commands

public DelegateCommand PickCommand { get; }
public DelegateCommand<RoutedEventArgs> SortResultCommand => new(SortResult);
public DelegateCommand<MouseButtonEventArgs> PickCommand { get; }
public DelegateCommand SkipCommand { get; }
public DelegateCommand SkipAllCommand { get; }
public DelegateCommand SearchAgainCommand { get; }
Expand All @@ -128,20 +125,14 @@ public SearchResultViewModel(IDialogService dialogService)
SearchAgainCommand = new DelegateCommand(SearchAgainMethod);
SkipCommand = new DelegateCommand(delegate { CloseDialog("false"); });
ResultListViewData = new ListViewData { Data = null, SelectedItem = null };
PickCommand = new DelegateCommand(PickMethod);
PickCommand = new DelegateCommand<MouseButtonEventArgs>(PickMethod);
SkipAllCommand = new DelegateCommand(delegate
{
GlobalVariables.SkipAll = true;
CloseDialog("false");
});
}

private void SortResult(RoutedEventArgs e)
{
string clickedHeader = (e.OriginalSource as GridViewColumnHeader)?.Column.Header.ToString();
MessageBox.Show(clickedHeader);
}

protected virtual void CloseDialog(string parameter)
{
var result = parameter?.ToLower(CultureInfo.InvariantCulture) switch
Expand Down Expand Up @@ -178,6 +169,7 @@ public virtual void OnDialogOpened(IDialogParameters parameters)
_fullFolderPath = parameters.GetValue<string>("folderpath");
_isPickedById = parameters.GetValue<bool>("isPickedById");
LoadData(SearchTitle);
FileList = Util.GetFileNamesFromFolder(_fullFolderPath);
}

private async void StartSearch(bool useBusy)
Expand All @@ -193,6 +185,7 @@ private async void StartSearch(bool useBusy)
var result = SearchMode == MediaTypes.Game
? await _igdbObject.SearchGameAsync(titleToSearch.Replace(@"\", " "))
: await _tmdbObject.SearchAsync(titleToSearch.Replace(@"\", " "), SearchMode);
if (Util.GetResultCount(_isPickedById, result.Result, SearchMode) == 0) return;
SearchResult = result;
if (useBusy)
{
Expand Down Expand Up @@ -220,8 +213,6 @@ private void LoadData(string searchTitle)
{
IsSearchFocused = true;
}

FileList = Util.GetFileNamesFromFolder(_fullFolderPath);
}

private void SearchAgainMethod()
Expand All @@ -232,8 +223,14 @@ private void SearchAgainMethod()
}
}

private void PickMethod()
private void PickMethod(MouseButtonEventArgs eventArgs)
{
if (eventArgs is not null)
{
var dataContext = ((FrameworkElement)eventArgs.OriginalSource).DataContext;
if (dataContext is not ListItem) return;
}

if (ResultListViewData.SelectedItem == null) return;
var pickedIndex = ResultListViewData.Data.IndexOf(ResultListViewData.SelectedItem);
var rating = "";
Expand Down
12 changes: 6 additions & 6 deletions FoliCon/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,12 @@
<ListView x:Name="FinalList" Grid.Row="3" Grid.ColumnSpan="2" Grid.Column="0"
ItemsSource="{Binding FinalListViewData.Data}"
SelectedItem="{Binding FinalListViewData.SelectedItem}"
MouseDoubleClick="ListView_MouseDoubleClick" hc:Empty.ShowEmpty="true">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding ListViewDoubleClickCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
hc:Empty.ShowEmpty="true" GridViewColumnHeader.Click="FinalList_OnClick" >
<hc:Interaction.Triggers>
<hc:EventTrigger EventName="MouseDoubleClick">
<hc:LaunchUriOrFileAction Path="{Binding FinalListViewData.SelectedItem.Folder}" />
</hc:EventTrigger>
</hc:Interaction.Triggers>
<ListView.View>
<GridView>
<GridViewColumn Header="{ex:Lang Key={x:Static langs:LangKeys.Title}}" Width="Auto" DisplayMemberBinding="{Binding Title}" />
Expand Down
46 changes: 42 additions & 4 deletions FoliCon/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using FoliCon.Modules;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Globalization;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using FoliCon.Models;
using FoliCon.Properties.Langs;
using HandyControl.Tools;
Expand All @@ -15,16 +18,14 @@ 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_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
}

private void ListView_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
Util.SetColumnWidth(FinalList);
Expand Down Expand Up @@ -53,5 +54,42 @@ 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;
var direction = headerClicked != _lastHeaderClicked
? ListSortDirection.Ascending
: _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();
}
}
}
2 changes: 1 addition & 1 deletion FoliCon/Views/SearchResult.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
SelectedItem="{Binding ResultListViewData.SelectedItem}" Grid.Row="1" hc:Empty.ShowEmpty="true" GridViewColumnHeader.Click="ListViewResult_OnClick">
<hc:Interaction.Triggers>
<hc:EventTrigger EventName="MouseDoubleClick">
<hc:InvokeCommandAction Command="{Binding PickCommand}" />
<hc:EventToCommand Command="{Binding PickCommand}" PassEventArgsToCommand="True"/>
</hc:EventTrigger>
</hc:Interaction.Triggers>
<ListView.View>
Expand Down
12 changes: 12 additions & 0 deletions FoliCon/Views/SearchResult.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using FoliCon.Models;

namespace FoliCon.Views
{
Expand Down Expand Up @@ -68,5 +70,15 @@ private void Sort(string sortBy, ListSortDirection direction)
dataView.SortDescriptions.Add(sd);
dataView.Refresh();
}

private void ListViewResult_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var dataContext = ((FrameworkElement)e.OriginalSource).DataContext;

if (dataContext is ListItem)
{
MessageBox.Show("Item's Double Click handled!");
}
}
}
}

0 comments on commit 845b129

Please sign in to comment.