Skip to content

Commit

Permalink
Enable functionality to filter tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
sabihoshi committed May 14, 2021
1 parent f2c0f17 commit b3b3079
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion GenshinLyreMidiPlayer.WPF/GenshinLyreMidiPlayer.WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<UseWPF>true</UseWPF>
<StartupObject>GenshinLyreMidiPlayer.WPF.App</StartupObject>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Version>1.10.1</Version>
<Version>1.10.2</Version>
<ApplicationIcon>item_windsong_lyre.ico</ApplicationIcon>
<Nullable>enable</Nullable>
<RepositoryUrl>https://github.com/sabihoshi/GenshinLyreMidiPlayer</RepositoryUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions GenshinLyreMidiPlayer.WPF/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
21 changes: 17 additions & 4 deletions GenshinLyreMidiPlayer.WPF/ViewModels/PlaylistViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,9 +29,13 @@ public PlaylistViewModel(IContainer ioc, IEventAggregator events)
_events = events;
}

public BindableCollection<MidiFile> Tracks { get; set; } = new();
public BindableCollection<MidiFile> FilteredTracks => string.IsNullOrWhiteSpace(FilterText)
? Tracks
: new(Tracks.Where(t => t.Title.Contains(FilterText, StringComparison.OrdinalIgnoreCase)));

public BindableCollection<MidiFile>? ShuffledTracks { get; set; }
private BindableCollection<MidiFile> ShuffledTracks { get; set; } = new();

public BindableCollection<MidiFile> Tracks { get; } = new();

public bool Shuffle { get; set; }

Expand All @@ -46,6 +51,8 @@ public PlaylistViewModel(IContainer ioc, IEventAggregator events)

public Stack<MidiFile> History { get; } = new();

public string FilterText { get; set; }

public string LoopStateString =>
Loop switch
{
Expand All @@ -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;
Expand Down Expand Up @@ -95,7 +107,7 @@ public void ToggleLoop()
return next;
}

public BindableCollection<MidiFile> GetPlaylist() => (Shuffle ? ShuffledTracks : Tracks)!;
public BindableCollection<MidiFile> GetPlaylist() => Shuffle ? ShuffledTracks : Tracks;

public async Task OpenFile()
{
Expand All @@ -122,7 +134,8 @@ public async Task AddFiles(IEnumerable<string> 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());
}

Expand Down
9 changes: 3 additions & 6 deletions GenshinLyreMidiPlayer.WPF/Views/PlaylistView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@

d:DataContext="{d:DesignInstance viewModels:PlaylistViewModel}">
<UserControl.Resources>
<modernWpf:ProductGroupKeyConverter x:Key="ProductGroupKeyConverter" />
<CollectionViewSource
x:Key="Tracks" Source="{Binding Tracks}">
<modernWpf:TrackGroupKeyConverter x:Key="TrackGroupKeyConverter" />
<CollectionViewSource x:Key="Tracks" Source="{Binding FilteredTracks}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription
PropertyName="Title"
Converter="{StaticResource ProductGroupKeyConverter}" />
<PropertyGroupDescription PropertyName="Title" Converter="{StaticResource TrackGroupKeyConverter}" />
</CollectionViewSource.GroupDescriptions>
<CollectionViewSource.SortDescriptions>
<componentModel:SortDescription PropertyName="Title" />
Expand Down

0 comments on commit b3b3079

Please sign in to comment.