Skip to content

Commit

Permalink
Add Favorites Import/Export buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxhy committed Feb 27, 2024
1 parent 5a0294b commit b938f86
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 6 deletions.
56 changes: 55 additions & 1 deletion KeyManager/Domain/FavoritesControlViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
using Leosac.KeyManager.Library;
using Leosac.KeyManager.Library.UI;
using Leosac.KeyManager.Library.UI.Domain;
using Leosac.WpfApp;
using MaterialDesignThemes.Wpf;
using Microsoft.Win32;
using System;
using System.Security.Cryptography;
using System.Threading.Tasks;

namespace Leosac.KeyManager.Domain
Expand All @@ -21,6 +24,16 @@ public FavoritesControlViewModel(ISnackbarMessageQueue snackbarMessageQueue)
{
RefreshFavorites();
});
ImportFavoritesCommand = new RelayCommand(
() =>
{
ImportFavorites();
});
ExportFavoritesCommand = new RelayCommand(
() =>
{
ExportFavorites();
});
OpenFavoriteCommand = new AsyncRelayCommand<Favorite>(
async fav =>
{
Expand Down Expand Up @@ -139,13 +152,54 @@ public void RefreshFavorites()
});
}

protected void ImportFavorites()
{
var ofd = new OpenFileDialog();
ofd.Filter = "JSON Files (*.json)|*.json";
ofd.CheckFileExists = true;
if (ofd.ShowDialog() == true)
{
try
{
var favorites = Favorites.LoadFromFile(ofd.FileName);
if (favorites != null)
{
favorites.SaveToFile();
RefreshFavorites();
}
}
catch(Exception ex)
{
SnackbarHelper.EnqueueError(_snackbarMessageQueue, ex);
}
}
}

protected void ExportFavorites()
{
var sfd = new SaveFileDialog();
sfd.Filter = "JSON Files (*.json)|*.json";
if (sfd.ShowDialog() == true)
{
try
{
Favorites.GetSingletonInstance()?.SaveToFile(sfd.FileName);
}
catch (Exception ex)
{
SnackbarHelper.EnqueueError(_snackbarMessageQueue, ex);
}
}
}

public RelayCommand? RefreshFavoritesCommand { get; set; }
public RelayCommand? ImportFavoritesCommand { get; set; }
public RelayCommand? ExportFavoritesCommand { get; set; }
public AsyncRelayCommand? CreateFavoriteCommand { get; set; }
public RelayCommand<Favorite>? RemoveFavoriteCommand { get; set; }
public AsyncRelayCommand<Favorite>? EditFavoriteCommand { get; }
public AsyncRelayCommand<Favorite>? OpenFavoriteCommand { get; }
public RelayCommand? ChangeMasterKeyCommand { get; }

public AsyncRelayCommand<object>? KeyStoreCommand { private get; set; }
}
}
32 changes: 27 additions & 5 deletions KeyManager/FavoritesControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@
Loaded="userControl_Loaded"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
<CollectionViewSource x:Key='Favorites' Source="{Binding Favorites.KeyStores}">
<CollectionViewSource.SortDescriptions>
<componentModel:SortDescription PropertyName="Name" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://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">
<DockPanel DockPanel.Dock="Top" Margin="15,5,15,5">
Expand Down Expand Up @@ -99,6 +104,23 @@
ToolTip="{x:Static properties:Resources.NewFavorite}" Margin="5">
<materialDesign:PackIcon Kind="StarAdd" Height="24" Width="24" Cursor="Hand" />
</Button>
<materialDesign:PopupBox PlacementMode="BottomAndAlignCentres" ToolTipService.Placement="Right" Style="{StaticResource MaterialDesignMultiFloatingActionPopupBox}">
<materialDesign:PopupBox.ToggleContent>
<materialDesign:PackIcon Kind="DotsVertical" Height="24" Width="24" Cursor="Hand" />
</materialDesign:PopupBox.ToggleContent>
<StackPanel>
<Button Style="{StaticResource MaterialDesignFloatingActionButton}"
Command="{Binding ImportFavoritesCommand}"
ToolTip="{x:Static properties:Resources.Import}" Margin="5">
<materialDesign:PackIcon Kind="Import" Height="24" Width="24" Cursor="Hand" />
</Button>
<Button Style="{StaticResource MaterialDesignFloatingActionButton}"
Command="{Binding ExportFavoritesCommand}"
ToolTip="{x:Static properties:Resources.Export}" Margin="5">
<materialDesign:PackIcon Kind="Export" Height="24" Width="24" Cursor="Hand" />
</Button>
</StackPanel>
</materialDesign:PopupBox>
</WrapPanel>
<WrapPanel DockPanel.Dock="Left" Orientation="Horizontal" HorizontalAlignment="Left">
<TextBlock FontSize="28" FontWeight="Bold" Text="{x:Static properties:Resources.Favorites}" />
Expand Down
18 changes: 18 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.

6 changes: 6 additions & 0 deletions KeyManager/Properties/Resources.fr.resx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@
<data name="EditFavorite" xml:space="preserve">
<value>Modifier le magasin de clés favoris</value>
</data>
<data name="Export" xml:space="preserve">
<value>Exporter</value>
</data>
<data name="FavoriteName" xml:space="preserve">
<value>Nom</value>
</data>
Expand All @@ -156,6 +159,9 @@
<data name="Favorites" xml:space="preserve">
<value>Magasins de clés favoris</value>
</data>
<data name="Import" xml:space="preserve">
<value>Importer</value>
</data>
<data name="Information" xml:space="preserve">
<value>Information</value>
</data>
Expand Down
6 changes: 6 additions & 0 deletions KeyManager/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@
<data name="EditFavorite" xml:space="preserve">
<value>Edit the Key Store Favorite</value>
</data>
<data name="Export" xml:space="preserve">
<value>Export</value>
</data>
<data name="FavoriteName" xml:space="preserve">
<value>Name</value>
</data>
Expand All @@ -156,6 +159,9 @@
<data name="Favorites" xml:space="preserve">
<value>Key Store Favorites</value>
</data>
<data name="Import" xml:space="preserve">
<value>Import</value>
</data>
<data name="Information" xml:space="preserve">
<value>Information</value>
</data>
Expand Down

0 comments on commit b938f86

Please sign in to comment.