From b3b3079e9e8690d6d8eaf6abb42d80eae120621b Mon Sep 17 00:00:00 2001 From: sabihoshi Date: Fri, 14 May 2021 18:37:00 +0800 Subject: [PATCH] Enable functionality to filter tracks --- .../GenshinLyreMidiPlayer.WPF.csproj | 2 +- ...Converter.cs => TrackGroupKeyConverter.cs} | 2 +- .../ViewModels/MainWindowViewModel.cs | 2 ++ .../ViewModels/PlaylistViewModel.cs | 21 +++++++++++++++---- .../Views/PlaylistView.xaml | 9 +++----- 5 files changed, 24 insertions(+), 12 deletions(-) rename GenshinLyreMidiPlayer.WPF/ModernWPF/{ProductGroupKeyConverter.cs => TrackGroupKeyConverter.cs} (88%) diff --git a/GenshinLyreMidiPlayer.WPF/GenshinLyreMidiPlayer.WPF.csproj b/GenshinLyreMidiPlayer.WPF/GenshinLyreMidiPlayer.WPF.csproj index bc3edaf..cdea9f5 100644 --- a/GenshinLyreMidiPlayer.WPF/GenshinLyreMidiPlayer.WPF.csproj +++ b/GenshinLyreMidiPlayer.WPF/GenshinLyreMidiPlayer.WPF.csproj @@ -6,7 +6,7 @@ true GenshinLyreMidiPlayer.WPF.App app.manifest - 1.10.1 + 1.10.2 item_windsong_lyre.ico enable https://github.com/sabihoshi/GenshinLyreMidiPlayer diff --git a/GenshinLyreMidiPlayer.WPF/ModernWPF/ProductGroupKeyConverter.cs b/GenshinLyreMidiPlayer.WPF/ModernWPF/TrackGroupKeyConverter.cs similarity index 88% rename from GenshinLyreMidiPlayer.WPF/ModernWPF/ProductGroupKeyConverter.cs rename to GenshinLyreMidiPlayer.WPF/ModernWPF/TrackGroupKeyConverter.cs index 0d8a34e..e612543 100644 --- a/GenshinLyreMidiPlayer.WPF/ModernWPF/ProductGroupKeyConverter.cs +++ b/GenshinLyreMidiPlayer.WPF/ModernWPF/TrackGroupKeyConverter.cs @@ -4,7 +4,7 @@ namespace GenshinLyreMidiPlayer.WPF.ModernWPF { - public class ProductGroupKeyConverter : IValueConverter + public class TrackGroupKeyConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => ((string) value).Substring(0, 1).ToUpper(); diff --git a/GenshinLyreMidiPlayer.WPF/ViewModels/MainWindowViewModel.cs b/GenshinLyreMidiPlayer.WPF/ViewModels/MainWindowViewModel.cs index 46b4f74..f7bfbde 100644 --- a/GenshinLyreMidiPlayer.WPF/ViewModels/MainWindowViewModel.cs +++ b/GenshinLyreMidiPlayer.WPF/ViewModels/MainWindowViewModel.cs @@ -38,6 +38,8 @@ protected override async void OnViewLoaded() // Work around because events do not conform to the signatures Stylet supports _navView = ((MainWindowView) View).NavView; + _navView.AutoSuggestBox.TextChanged += PlaylistView.OnFilterTextChanged; + _navView.SelectionChanged += Navigate; _navView.BackRequested += NavigateBack; diff --git a/GenshinLyreMidiPlayer.WPF/ViewModels/PlaylistViewModel.cs b/GenshinLyreMidiPlayer.WPF/ViewModels/PlaylistViewModel.cs index cbbcde3..688278a 100644 --- a/GenshinLyreMidiPlayer.WPF/ViewModels/PlaylistViewModel.cs +++ b/GenshinLyreMidiPlayer.WPF/ViewModels/PlaylistViewModel.cs @@ -10,6 +10,7 @@ using Melanchall.DryWetMidi.Core; using Microsoft.Win32; using ModernWpf; +using ModernWpf.Controls; using Stylet; using StyletIoC; using static Windows.Media.MediaPlaybackAutoRepeatMode; @@ -28,9 +29,13 @@ public PlaylistViewModel(IContainer ioc, IEventAggregator events) _events = events; } - public BindableCollection Tracks { get; set; } = new(); + public BindableCollection FilteredTracks => string.IsNullOrWhiteSpace(FilterText) + ? Tracks + : new(Tracks.Where(t => t.Title.Contains(FilterText, StringComparison.OrdinalIgnoreCase))); - public BindableCollection? ShuffledTracks { get; set; } + private BindableCollection ShuffledTracks { get; set; } = new(); + + public BindableCollection Tracks { get; } = new(); public bool Shuffle { get; set; } @@ -46,6 +51,8 @@ public PlaylistViewModel(IContainer ioc, IEventAggregator events) public Stack History { get; } = new(); + public string FilterText { get; set; } + public string LoopStateString => Loop switch { @@ -54,6 +61,11 @@ public PlaylistViewModel(IContainer ioc, IEventAggregator events) List => "\xE8EE" }; + public void OnFilterTextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs e) + { + FilterText = sender.Text; + } + public void ToggleShuffle() { Shuffle = !Shuffle; @@ -95,7 +107,7 @@ public void ToggleLoop() return next; } - public BindableCollection GetPlaylist() => (Shuffle ? ShuffledTracks : Tracks)!; + public BindableCollection GetPlaylist() => Shuffle ? ShuffledTracks : Tracks; public async Task OpenFile() { @@ -122,7 +134,8 @@ public async Task AddFiles(IEnumerable files) RefreshPlaylist(); await UpdateHistory(); - if (OpenedFile is null && Tracks.Count > 0) + var next = Next(); + if (OpenedFile is null && Tracks.Count > 0 && next is not null) _events.Publish(Next()); } diff --git a/GenshinLyreMidiPlayer.WPF/Views/PlaylistView.xaml b/GenshinLyreMidiPlayer.WPF/Views/PlaylistView.xaml index eb6eb54..cafdfb6 100644 --- a/GenshinLyreMidiPlayer.WPF/Views/PlaylistView.xaml +++ b/GenshinLyreMidiPlayer.WPF/Views/PlaylistView.xaml @@ -17,13 +17,10 @@ d:DataContext="{d:DesignInstance viewModels:PlaylistViewModel}"> - - + + - +