Skip to content

Commit

Permalink
Add Search input to Favorites view
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxhy committed May 21, 2024
1 parent d22c1f4 commit f0f3622
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 8 deletions.
58 changes: 57 additions & 1 deletion KeyManager/Domain/FavoritesControlViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Leosac.KeyManager.Library;
using Leosac.KeyManager.Library.KeyStore;
using Leosac.KeyManager.Library.UI;
using Leosac.KeyManager.Library.UI.Domain;
using Leosac.WpfApp;
using MaterialDesignThemes.Wpf;
using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Windows.Data;

namespace Leosac.KeyManager.Domain
{
Expand Down Expand Up @@ -71,7 +74,7 @@ public FavoritesControlViewModel(ISnackbarMessageQueue snackbarMessageQueue)
Favorites?.KeyStores.Remove(fav);
Favorites?.SaveToFile();
log.Info(String.Format("Favorite `{0}` removed.", fav.Name));
log.Info(string.Format("Favorite `{0}` removed.", fav.Name));
}
});
EditFavoriteCommand = new AsyncRelayCommand<Favorite>(
Expand Down Expand Up @@ -140,16 +143,49 @@ public bool IsDefaultMasterKey
set => SetProperty(ref _newMasterKey, value);
}

private string? _searchTerms;

public string? SearchTerms
{
get => _searchTerms;
set
{
if (SetProperty(ref _searchTerms, value))
{
RefreshFavoritesView();
}
}
}

public void RefreshFavorites()
{
IsLoadingFavorites = true;
SearchTerms = string.Empty;
Favorites = Favorites.GetSingletonInstance(true);
Task.Run(async () =>
{
// Just to be sure the animation is visible long enough...
await Task.Delay(500);
IsLoadingFavorites = false;
});
if (Favorites != null)
{
FavoritesView = CollectionViewSource.GetDefaultView(Favorites.KeyStores);
FavoritesView.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
FavoritesView.Filter = FavoritesFilter;
}
}

public void RefreshFavoritesView()
{
FavoritesView?.Refresh();
}

private ICollectionView? _favoritesView;
public ICollectionView? FavoritesView
{
get => _favoritesView;
set => SetProperty(ref _favoritesView, value);
}

protected void ImportFavorites()
Expand Down Expand Up @@ -192,6 +228,26 @@ protected void ExportFavorites()
}
}

private bool FavoritesFilter(object obj)
{
if (string.IsNullOrWhiteSpace(_searchTerms))
{
return true;
}

if (obj is Favorite item)
{
var terms = _searchTerms.ToLowerInvariant();

if (item.Name != null && item.Name.ToLowerInvariant().Contains(terms))
{
return true;
}
}

return false;
}

public RelayCommand? RefreshFavoritesCommand { get; set; }
public RelayCommand? ImportFavoritesCommand { get; set; }
public RelayCommand? ExportFavoritesCommand { get; set; }
Expand Down
30 changes: 23 additions & 7 deletions KeyManager/FavoritesControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack:https://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.PopupBox.xaml" />
</ResourceDictionary.MergedDictionaries>
<CollectionViewSource x:Key='Favorites' Source="{Binding Favorites.KeyStores}">
<CollectionViewSource.SortDescriptions>
<componentModel:SortDescription PropertyName="Name" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</ResourceDictionary>
</UserControl.Resources>
<DockPanel LastChildFill="True">
Expand Down Expand Up @@ -99,6 +94,25 @@
ToolTip="{x:Static properties:Resources.RefreshFavorites}" Margin="5">
<materialDesign:PackIcon Kind="Reload" Height="24" Width="24" Cursor="Hand" />
</Button>
<materialDesign:DialogHost DialogTheme="Inherit">
<materialDesign:DialogHost.DialogContent>
<WrapPanel Margin="16">
<TextBox TextWrapping="Wrap" Margin="3" MinWidth="200"
Text="{Binding SearchTerms}"
materialDesign:HintAssist.Hint="{x:Static properties:Resources.Search}"
Style="{StaticResource MaterialDesignFloatingHintTextBox}"/>
<Button Margin="4" Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}" IsDefault="True" IsCancel="True" Style="{StaticResource MaterialDesignFlatButton}">
<materialDesign:PackIcon Kind="Search" Height="24" Width="24" Cursor="Hand" />
</Button>
</WrapPanel>
</materialDesign:DialogHost.DialogContent>
<Button Style="{StaticResource MaterialDesignFloatingActionButton}"
ToolTip="{x:Static properties:Resources.Search}"
Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}"
Margin="5">
<materialDesign:PackIcon Kind="Search" Width="24" Height="24" />
</Button>
</materialDesign:DialogHost>
<Button Style="{StaticResource MaterialDesignFloatingActionButton}"
Command="{Binding CreateFavoriteCommand}"
ToolTip="{x:Static properties:Resources.NewFavorite}" Margin="5">
Expand All @@ -123,10 +137,12 @@
</materialDesign:PopupBox>
</WrapPanel>
<WrapPanel DockPanel.Dock="Left" Orientation="Horizontal" HorizontalAlignment="Left">
<TextBlock FontSize="28" FontWeight="Bold" Text="{x:Static properties:Resources.Favorites}" />
<materialDesign:Badged Badge="{Binding Favorites.KeyStores.Count}">
<TextBlock FontSize="28" FontWeight="Bold" Text="{x:Static properties:Resources.Favorites}" />
</materialDesign:Badged>
</WrapPanel>
</DockPanel>
<ListBox ItemsSource="{Binding Source={StaticResource Favorites}}" HorizontalContentAlignment="Stretch">
<ListBox ItemsSource="{Binding FavoritesView}" HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate DataType="lib:Favorite">
<DockPanel LastChildFill="True">
Expand Down
9 changes: 9 additions & 0 deletions KeyManager/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions KeyManager/Properties/Resources.fr.resx
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@
<data name="Save" xml:space="preserve">
<value>Enregistrer</value>
</data>
<data name="Search" xml:space="preserve">
<value>Rechercher</value>
</data>
<data name="Secret" xml:space="preserve">
<value>Secret</value>
</data>
Expand Down
3 changes: 3 additions & 0 deletions KeyManager/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@
<data name="Save" xml:space="preserve">
<value>Save</value>
</data>
<data name="Search" xml:space="preserve">
<value>Search</value>
</data>
<data name="Secret" xml:space="preserve">
<value>Secret</value>
</data>
Expand Down

0 comments on commit f0f3622

Please sign in to comment.