Implemented filtering

- Also an additional extension method for later
This commit is contained in:
adroslice 2019-11-14 20:42:25 +01:00
parent 0bc82dc6b5
commit e671fd4185
4 changed files with 40 additions and 14 deletions

View File

@ -1,4 +1,6 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace BFR.Helpers namespace BFR.Helpers
{ {
@ -11,4 +13,18 @@ namespace BFR.Helpers
list.Add(t); list.Add(t);
} }
} }
public static class StringExtensions
{
public static string Replace(this string input, string pattern, string replacement, bool useRegex = false, bool useRegexReplace = false) =>
Regex.Replace(input,
useRegex ? pattern : Regex.Escape(pattern),
useRegexReplace ? replacement : replacement.Replace("$", "$$"));
public static bool RegexContains(this string input, string pattern)
{
try { return Regex.IsMatch(input, pattern); }
catch(Exception) { return false; }
}
}
} }

View File

@ -16,13 +16,13 @@
<!-- Filters --> <!-- Filters -->
<Grid Grid.Row="1" Grid.ColumnSpan="3" ColumnDefinitions="auto,*,auto,*,auto,auto,auto,auto" IsVisible="{Binding #FilterExpander.IsExpanded}"> <Grid Grid.Row="1" Grid.ColumnSpan="3" ColumnDefinitions="auto,*,auto,*,auto,auto,auto,auto" IsVisible="{Binding #FilterExpander.IsExpanded}">
<TextBlock Grid.Column="0" Text="Extension:"/> <TextBlock Grid.Column="0" Text="Extension:"/>
<TextBox Grid.Column="1"/> <TextBox Grid.Column="1" Text="{Binding FilterExtension}" KeyUp="FilterChangedAny"/>
<TextBlock Grid.Column="2" Text="Name:"/> <TextBlock Grid.Column="2" Text="Name:"/>
<TextBox Grid.Column="3"/> <TextBox Grid.Column="3" Text="{Binding FilterPattern}" KeyUp="FilterChangedAny"/>
<TextBlock Grid.Column="4" Text="Full Name:"/> <TextBlock Grid.Column="4" Text="Full Name:"/>
<CheckBox Grid.Column="5"/> <CheckBox Grid.Column="5" IsChecked="{Binding FilterFullName}" Click="FilterChangedAny"/>
<TextBlock Grid.Column="6" Text="Regex:"/> <TextBlock Grid.Column="6" Text="Regex:"/>
<CheckBox Grid.Column="7" Margin="4,4,5,4"/> <CheckBox Grid.Column="7" IsChecked="{Binding FilterRegex}" Click="FilterChangedAny" Margin="4,4,5,4"/>
</Grid> </Grid>
</Grid> </Grid>
</Border> </Border>

View File

@ -1,14 +1,17 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Avalonia.Input;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Markup.Xaml; using Avalonia.Markup.Xaml;
using Avalonia.Interactivity;
using BFR.DataModels;
using BFR.Helpers; using BFR.Helpers;
using BFR.DataModels;
namespace BFR namespace BFR
{ {
@ -24,11 +27,14 @@ namespace BFR
Filter(); Filter();
} }
public void FilterChangedAny(object sender, KeyEventArgs e) => Filter();
public void FilterChangedAny(object sender, RoutedEventArgs e) => Filter();
public void Filter() public void Filter()
{ {
Files.ReplaceAll(AllFiles); // Filter all files in the directory for those satisfying the given filters
Files.ReplaceAll(AllFiles.Where(x =>
// TODO: Apply filters (FilterExtension == "" || x.Extension == FilterExtension)
&& (FilterFullName ? x.FullName : x.Name).RegexContains(FilterRegex ? FilterPattern : Regex.Escape(FilterPattern))));
Preview(); Preview();
} }
@ -45,7 +51,7 @@ namespace BFR
Files.ReplaceAll(new List<FileModel>(Files)); Files.ReplaceAll(new List<FileModel>(Files));
} }
public MainWindow() => public MainWindow() =>
InitializeComponent(); InitializeComponent();
private void InitializeComponent() private void InitializeComponent()

View File

@ -1,6 +1,4 @@
using System; using Avalonia;
using Avalonia;
using Avalonia.Collections; using Avalonia.Collections;
using Avalonia.Controls; using Avalonia.Controls;
@ -16,5 +14,11 @@ namespace BFR
public AvaloniaList<FileModel> AllFiles { get; } = new AvaloniaList<FileModel>(); public AvaloniaList<FileModel> AllFiles { get; } = new AvaloniaList<FileModel>();
public AvaloniaList<FileModel> Files { get; } = new AvaloniaList<FileModel>(); public AvaloniaList<FileModel> Files { get; } = new AvaloniaList<FileModel>();
// Filters
public string FilterExtension { get; set; } = "";
public string FilterPattern { get; set; } = "";
public bool FilterFullName { get; set; } = false;
public bool FilterRegex { get; set; } = false;
} }
} }