diff --git a/FoliCon/Modules/Util.cs b/FoliCon/Modules/Util.cs index fe395f4a..3aa5a8f8 100644 --- a/FoliCon/Modules/Util.cs +++ b/FoliCon/Modules/Util.cs @@ -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; + } } } \ No newline at end of file diff --git a/FoliCon/ViewModels/SearchResultViewModel.cs b/FoliCon/ViewModels/SearchResultViewModel.cs index 3a20d25e..18a26804 100644 --- a/FoliCon/ViewModels/SearchResultViewModel.cs +++ b/FoliCon/ViewModels/SearchResultViewModel.cs @@ -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 @@ -114,8 +112,7 @@ public bool IsSearchFocused #region Commands - public DelegateCommand PickCommand { get; } - public DelegateCommand SortResultCommand => new(SortResult); + public DelegateCommand PickCommand { get; } public DelegateCommand SkipCommand { get; } public DelegateCommand SkipAllCommand { get; } public DelegateCommand SearchAgainCommand { get; } @@ -128,7 +125,7 @@ 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(PickMethod); SkipAllCommand = new DelegateCommand(delegate { GlobalVariables.SkipAll = true; @@ -136,12 +133,6 @@ public SearchResultViewModel(IDialogService dialogService) }); } - 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 @@ -178,6 +169,7 @@ public virtual void OnDialogOpened(IDialogParameters parameters) _fullFolderPath = parameters.GetValue("folderpath"); _isPickedById = parameters.GetValue("isPickedById"); LoadData(SearchTitle); + FileList = Util.GetFileNamesFromFolder(_fullFolderPath); } private async void StartSearch(bool useBusy) @@ -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) { @@ -220,8 +213,6 @@ private void LoadData(string searchTitle) { IsSearchFocused = true; } - - FileList = Util.GetFileNamesFromFolder(_fullFolderPath); } private void SearchAgainMethod() @@ -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 = ""; diff --git a/FoliCon/Views/MainWindow.xaml b/FoliCon/Views/MainWindow.xaml index 9f4a447c..fcfc17fc 100644 --- a/FoliCon/Views/MainWindow.xaml +++ b/FoliCon/Views/MainWindow.xaml @@ -189,12 +189,12 @@ - - - - - + hc:Empty.ShowEmpty="true" GridViewColumnHeader.Click="FinalList_OnClick" > + + + + + diff --git a/FoliCon/Views/MainWindow.xaml.cs b/FoliCon/Views/MainWindow.xaml.cs index 58f816b7..1b4de053 100644 --- a/FoliCon/Views/MainWindow.xaml.cs +++ b/FoliCon/Views/MainWindow.xaml.cs @@ -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; @@ -15,16 +18,14 @@ namespace FoliCon.Views /// 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); @@ -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(); + } } } \ No newline at end of file diff --git a/FoliCon/Views/SearchResult.xaml b/FoliCon/Views/SearchResult.xaml index 76bf8481..61dfd312 100644 --- a/FoliCon/Views/SearchResult.xaml +++ b/FoliCon/Views/SearchResult.xaml @@ -54,7 +54,7 @@ SelectedItem="{Binding ResultListViewData.SelectedItem}" Grid.Row="1" hc:Empty.ShowEmpty="true" GridViewColumnHeader.Click="ListViewResult_OnClick"> - + diff --git a/FoliCon/Views/SearchResult.xaml.cs b/FoliCon/Views/SearchResult.xaml.cs index 846a455f..fe5ddbf0 100644 --- a/FoliCon/Views/SearchResult.xaml.cs +++ b/FoliCon/Views/SearchResult.xaml.cs @@ -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 { @@ -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!"); + } + } } } \ No newline at end of file