From 2124946ed1a07d380d9f13deaa801e0d805ec503 Mon Sep 17 00:00:00 2001 From: Mateusz Skoczek Date: Sat, 2 Mar 2024 02:41:42 +0100 Subject: [PATCH] playlist search - title and author filter --- .../Home/HomePlaylistViewModel.cs | 59 +++++++++++++++++-- .../VDownload.Core.ViewModels.csproj | 4 +- .../Home/HomePlaylistView.xaml | 6 +- .../VDownload.Core.Views.csproj | 4 +- VDownload/VDownload.csproj | 2 +- 5 files changed, 64 insertions(+), 11 deletions(-) diff --git a/VDownload.Core/VDownload.Core.ViewModels/Home/HomePlaylistViewModel.cs b/VDownload.Core/VDownload.Core.ViewModels/Home/HomePlaylistViewModel.cs index c164796..aac264a 100644 --- a/VDownload.Core/VDownload.Core.ViewModels/Home/HomePlaylistViewModel.cs +++ b/VDownload.Core/VDownload.Core.ViewModels/Home/HomePlaylistViewModel.cs @@ -15,6 +15,7 @@ using VDownload.Services.Data.Settings; using VDownload.Services.UI.StoragePicker; using VDownload.Sources.Twitch.Configuration.Models; using SimpleToolkit.MVVM; +using System.Text.RegularExpressions; namespace VDownload.Core.ViewModels.Home { @@ -58,6 +59,28 @@ namespace VDownload.Core.ViewModels.Home [ObservableProperty] protected bool _isSomethingHidden; + public string TitleFilter + { + get => _titleFilter; + set + { + SetProperty(ref _titleFilter, value, nameof(TitleFilter)); + UpdateFilter(); + } + } + protected string _titleFilter; + + public string AuthorFilter + { + get => _authorFilter; + set + { + SetProperty(ref _authorFilter, value, nameof(AuthorFilter)); + UpdateFilter(); + } + } + protected string _authorFilter; + #endregion @@ -87,13 +110,15 @@ namespace VDownload.Core.ViewModels.Home _removedVideos.Clear(); + _titleFilter = string.Empty; + _authorFilter = string.Empty; Name = _playlist.Name; Videos.Clear(); foreach (Video video in playlist) { Videos.Add(new VideoViewModel(video, _settingsService, _storagePickerService), true); } - UpdateCounters(); + UpdateFilter(); } #endregion @@ -122,7 +147,7 @@ namespace VDownload.Core.ViewModels.Home _removedVideos.Add(video); - UpdateCounters(); + UpdateFilter(); } [RelayCommand] @@ -134,7 +159,7 @@ namespace VDownload.Core.ViewModels.Home } _removedVideos.Clear(); - UpdateCounters(); + UpdateFilter(); } [RelayCommand] @@ -162,8 +187,34 @@ namespace VDownload.Core.ViewModels.Home CloseRequested?.Invoke(this, EventArgs.Empty); } - protected void UpdateCounters() + protected void UpdateFilter() { + Regex titleRegex = new Regex(TitleFilter); + Regex authorRegex = new Regex(AuthorFilter); + + foreach (ObservableKeyValuePair item in Videos) + { + if (!titleRegex.IsMatch(item.Key.Title)) + { + item.Value = false; + continue; + } + + if (!authorRegex.IsMatch(item.Key.Author)) + { + item.Value = false; + continue; + } + + if (_removedVideos.Contains(item.Key)) + { + item.Value = false; + continue; + } + + item.Value = true; + } + RemovedCount = _removedVideos.Count; HiddenCount = Videos.Values.Where(x => !x).Count(); IsSomethingHidden = HiddenCount > 0; diff --git a/VDownload.Core/VDownload.Core.ViewModels/VDownload.Core.ViewModels.csproj b/VDownload.Core/VDownload.Core.ViewModels/VDownload.Core.ViewModels.csproj index d4e9bf9..2c41f44 100644 --- a/VDownload.Core/VDownload.Core.ViewModels/VDownload.Core.ViewModels.csproj +++ b/VDownload.Core/VDownload.Core.ViewModels/VDownload.Core.ViewModels.csproj @@ -13,8 +13,8 @@ - - + + diff --git a/VDownload.Core/VDownload.Core.Views/Home/HomePlaylistView.xaml b/VDownload.Core/VDownload.Core.Views/Home/HomePlaylistView.xaml index e86504d..a0b441f 100644 --- a/VDownload.Core/VDownload.Core.Views/Home/HomePlaylistView.xaml +++ b/VDownload.Core/VDownload.Core.Views/Home/HomePlaylistView.xaml @@ -75,14 +75,16 @@ VerticalAlignment="Center"/> + Grid.Column="1" + Text="{Binding TitleFilter, Mode=TwoWay}"/> + Grid.Column="1" + Text="{Binding AuthorFilter, Mode=TwoWay}"/> - - + + diff --git a/VDownload/VDownload.csproj b/VDownload/VDownload.csproj index 51dc1fe..0f887e5 100644 --- a/VDownload/VDownload.csproj +++ b/VDownload/VDownload.csproj @@ -155,7 +155,7 @@ - +