using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Platform.Storage;
using JdeScoping.ConfigManager.Constants;
using JdeScoping.ConfigManager.ViewModels.Dialogs;
using MsBox.Avalonia;
using MsBox.Avalonia.Enums;
namespace JdeScoping.ConfigManager.Views.Dialogs;
///
/// Dialog for unlocking an existing secure store.
///
public partial class UnlockStoreDialog : Window
{
///
/// Gets the view model for this dialog.
///
public UnlockStoreDialogViewModel ViewModel => (UnlockStoreDialogViewModel)DataContext!;
///
/// Design-time constructor. Required for XAML previewer.
///
public UnlockStoreDialog() : this(string.Empty)
{
}
///
/// Initializes a new instance of the class.
///
/// The path to the store file to unlock.
public UnlockStoreDialog(string storePath)
{
InitializeComponent();
DataContext = new UnlockStoreDialogViewModel(storePath);
Loaded += UnlockStoreDialog_Loaded;
}
private void UnlockStoreDialog_Loaded(object? sender, RoutedEventArgs e)
{
ViewModel.OnShowOpenFileDialog += ShowOpenFileDialogAsync;
}
private async Task ShowOpenFileDialogAsync(string title, string fileTypeName, string pattern)
{
var files = await StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
{
Title = title,
AllowMultiple = false,
FileTypeFilter = new[]
{
new FilePickerFileType(fileTypeName) { Patterns = new[] { pattern } },
new FilePickerFileType(SecureStoreFileExtensions.AllFilesTypeName) { Patterns = new[] { SecureStoreFileExtensions.AllFilesPattern } }
}
});
return files.Count > 0 ? files[0].Path.LocalPath : null;
}
private async void UnlockButton_Click(object? sender, RoutedEventArgs e)
{
if (!ViewModel.IsValid)
{
var box = MessageBoxManager.GetMessageBoxStandard(
SecureStoreStrings.ValidationErrorTitle,
ViewModel.ValidationError ?? SecureStoreStrings.DefaultValidationError,
ButtonEnum.Ok,
MsBox.Avalonia.Enums.Icon.Warning);
await box.ShowWindowDialogAsync(this);
return;
}
Close(true);
}
private void CancelButton_Click(object? sender, RoutedEventArgs e)
{
Close(false);
}
}