Skip to content

Commit cdb51cc

Browse files
committed
Feature: Filter
1 parent 39dea91 commit cdb51cc

File tree

5 files changed

+78
-32
lines changed

5 files changed

+78
-32
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Collections.Generic;
2+
3+
namespace NETworkManager.Profiles;
4+
5+
/// <summary>
6+
/// Class used to filter profiles in the UI.
7+
/// </summary>
8+
public class ProfileFilterInfo
9+
{
10+
/// <summary>
11+
/// Search string for filtering profiles.
12+
/// </summary>
13+
public string Search { get; set; } = string.Empty;
14+
15+
/// <summary>
16+
/// Tags for filtering profiles.
17+
/// </summary>
18+
public IEnumerable<string> Tags { get; set; } = [];
19+
}

Source/NETworkManager.Profiles/ProfileManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static ProfileManager()
5050
/// <summary>
5151
/// ObservableCollection of all profile files.
5252
/// </summary>
53-
public static ObservableCollection<ProfileFileInfo> ProfileFiles { get; set; } = new();
53+
public static ObservableCollection<ProfileFileInfo> ProfileFiles { get; set; } = [];
5454

5555
/// <summary>
5656
/// Currently loaded profile file.
@@ -75,7 +75,7 @@ private set
7575
/// <summary>
7676
/// Currently loaded groups with profiles.
7777
/// </summary>
78-
public static List<GroupInfo> Groups { get; set; } = new();
78+
public static List<GroupInfo> Groups { get; set; } = [];
7979

8080
/// <summary>
8181
/// Indicates if profiles have changed.

Source/NETworkManager/ViewModels/PingMonitorHostViewModel.cs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,6 @@ public GridLength ProfileWidth
265265
#endregion
266266

267267
#region Constructor, load settings
268-
269268
public PingMonitorHostViewModel(IDialogCoordinator instance)
270269
{
271270
_isLoading = true;
@@ -281,13 +280,15 @@ public PingMonitorHostViewModel(IDialogCoordinator instance)
281280
HostsView.SortDescriptions.Add(new SortDescription(nameof(PingMonitorView.Group), ListSortDirection.Ascending));
282281

283282
// Profiles
284-
SetProfilesView();
283+
SetProfilesView(new ProfileFilterInfo());
285284

286285
ProfileManager.OnProfilesUpdated += ProfileManager_OnProfilesUpdated;
287286

288287
_searchDispatcherTimer.Interval = GlobalStaticConfiguration.SearchDispatcherTimerTimeSpan;
289288
_searchDispatcherTimer.Tick += SearchDispatcherTimer_Tick;
290289

290+
CreateTagsList();
291+
291292
LoadSettings();
292293

293294
_isLoading = false;
@@ -404,6 +405,20 @@ private void ClearSearchAction()
404405
Search = string.Empty;
405406
}
406407

408+
public ICommand SetFilterCommand => new RelayCommand(_ => SetFilterAction());
409+
410+
private void SetFilterAction()
411+
{
412+
413+
}
414+
415+
public ICommand ClearFilterCommand => new RelayCommand(_ => ClearFilterAction());
416+
417+
private void ClearFilterAction()
418+
{
419+
420+
}
421+
407422
#endregion
408423

409424
#region Methods
@@ -582,37 +597,18 @@ public void OnViewHide()
582597
_isViewActive = false;
583598
}
584599

585-
private void SetProfilesView(ProfileInfo profile = null)
600+
private void SetProfilesView(ProfileFilterInfo filter, ProfileInfo profile = null)
586601
{
587602
Profiles = new CollectionViewSource
588603
{
589-
Source = ProfileManager.Groups.SelectMany(x => x.Profiles).Where(x => x.PingMonitor_Enabled)
590-
.OrderBy(x => x.Group).ThenBy(x => x.Name)
604+
Source = ProfileManager.Groups.SelectMany(x => x.Profiles).Where(x => x.PingMonitor_Enabled && (
605+
string.IsNullOrEmpty(filter.Search) || x.Name.IndexOf(filter.Search) > -1 || x.PingMonitor_Host.IndexOf(filter.Search) > -1) && (
606+
filter.Tags.Count() == 0 || filter.Tags.All(tag => x.TagsCollection.Contains(tag)))
607+
).OrderBy(x => x.Group).ThenBy(x => x.Name)
591608
}.View;
592609

593610
Profiles.GroupDescriptions.Add(new PropertyGroupDescription(nameof(ProfileInfo.Group)));
594611

595-
Profiles.Filter = o =>
596-
{
597-
if (string.IsNullOrEmpty(Search))
598-
return true;
599-
600-
if (o is not ProfileInfo info)
601-
return false;
602-
603-
var search = Search.Trim();
604-
605-
// Search by: Tag=xxx (exact match, ignore case)
606-
/*
607-
if (search.StartsWith(ProfileManager.TagIdentifier, StringComparison.OrdinalIgnoreCase))
608-
return !string.IsNullOrEmpty(info.Tags) && info.PingMonitor_Enabled && info.Tags.Replace(" ", "").Split(';').Any(str => search.Substring(ProfileManager.TagIdentifier.Length, search.Length - ProfileManager.TagIdentifier.Length).Equals(str, StringComparison.OrdinalIgnoreCase));
609-
*/
610-
611-
// Search by: Name, PingMonitor_Host
612-
return info.Name.IndexOf(search, StringComparison.OrdinalIgnoreCase) > -1 ||
613-
info.PingMonitor_Host.IndexOf(search, StringComparison.OrdinalIgnoreCase) > -1;
614-
};
615-
616612
// Set specific profile or first if null
617613
SelectedProfile = null;
618614

@@ -628,9 +624,12 @@ private void RefreshProfiles()
628624
if (!_isViewActive)
629625
return;
630626

631-
SetProfilesView(SelectedProfile);
627+
SetProfilesView(new ProfileFilterInfo
628+
{
629+
Search = Search,
630+
Tags = ["web", "prod"]
631+
}, SelectedProfile);
632632
}
633-
634633
#endregion
635634

636635
#region Event

Source/NETworkManager/ViewModels/ProfileViewModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public ProfileViewModel(Action<ProfileViewModel> saveCommand, Action<ProfileView
5656
Groups.SortDescriptions.Add(new SortDescription());
5757

5858
TagsCollection = profileInfo.TagsCollection;
59+
5960
Tags = CollectionViewSource.GetDefaultView(TagsCollection);
6061
Tags.SortDescriptions.Add(new SortDescription("", ListSortDirection.Ascending));
6162

Source/NETworkManager/Views/PingMonitorHostView.xaml

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@
301301
<ColumnDefinition Width="*" />
302302
<ColumnDefinition Width="10" />
303303
<ColumnDefinition Width="Auto" />
304+
<ColumnDefinition Width="10" />
305+
<ColumnDefinition Width="Auto" />
304306
</Grid.ColumnDefinitions>
305307
<ToggleButton Grid.Column="0" Grid.Row="0"
306308
Focusable="False"
@@ -346,11 +348,11 @@
346348
<Button Grid.Column="4" Grid.Row="0"
347349
Focusable="False"
348350
Style="{StaticResource ResourceKey=CleanButton}"
349-
Command="{Binding Path=AddProfileCommand}"
351+
Command="{Binding Path=SetFilterCommand}"
350352
ToolTip="{x:Static Member=localization:Strings.AddProfileDots}">
351353
<Rectangle Width="16" Height="16">
352354
<Rectangle.OpacityMask>
353-
<VisualBrush Stretch="Uniform" Visual="{iconPacks:Material Kind=Plus}" />
355+
<VisualBrush Stretch="Uniform" Visual="{iconPacks:Material Kind=FilterOutline}" />
354356
</Rectangle.OpacityMask>
355357
<Rectangle.Style>
356358
<Style TargetType="{x:Type TypeName=Rectangle}">
@@ -368,6 +370,31 @@
368370
</Rectangle.Style>
369371
</Rectangle>
370372
</Button>
373+
<Button Grid.Column="6" Grid.Row="0"
374+
Focusable="False"
375+
Style="{StaticResource ResourceKey=CleanButton}"
376+
Command="{Binding Path=AddProfileCommand}"
377+
ToolTip="{x:Static Member=localization:Strings.AddProfileDots}">
378+
<Rectangle Width="16" Height="16">
379+
<Rectangle.OpacityMask>
380+
<VisualBrush Stretch="Uniform" Visual="{iconPacks:Material Kind=Plus}" />
381+
</Rectangle.OpacityMask>
382+
<Rectangle.Style>
383+
<Style TargetType="{x:Type TypeName=Rectangle}">
384+
<Setter Property="Fill"
385+
Value="{DynamicResource ResourceKey=MahApps.Brushes.Gray3}" />
386+
<Style.Triggers>
387+
<DataTrigger
388+
Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TypeName=Button}}, Path=IsMouseOver}"
389+
Value="True">
390+
<Setter Property="Fill"
391+
Value="{DynamicResource ResourceKey=MahApps.Brushes.Gray5}" />
392+
</DataTrigger>
393+
</Style.Triggers>
394+
</Style>
395+
</Rectangle.Style>
396+
</Rectangle>
397+
</Button>
371398
</Grid>
372399
<ListBox Grid.Column="0" Grid.Row="2" ItemsSource="{Binding Path=Profiles}"
373400
Visibility="{Binding Path=IsSearching, Converter={StaticResource ResourceKey=BooleanReverseToVisibilityCollapsedConverter}}"

0 commit comments

Comments
 (0)