using Avalonia.Controls; using Avalonia.Input; using Avalonia.Interactivity; using JdeScoping.SecureStoreManager.ViewModels; namespace JdeScoping.SecureStoreManager.Views; public partial class MainWindow : Window { private MainWindowViewModel? ViewModel => DataContext as MainWindowViewModel; /// /// Initializes a new instance of the class. /// public MainWindow() { InitializeComponent(); Loaded += MainWindow_Loaded; Closing += MainWindow_Closing; } private void MainWindow_Loaded(object? sender, RoutedEventArgs e) { if (ViewModel == null) return; // Subscribe to dialog request events (these open dialogs with their own DataContext) ViewModel.OnRequestNewStoreDialog += ShowNewStoreDialog; ViewModel.OnRequestOpenStoreDialog += ShowOpenStoreDialog; ViewModel.OnRequestAddSecretDialog += ShowAddSecretDialog; ViewModel.OnRequestEditSecretDialog += ShowEditSecretDialog; ViewModel.OnRequestClose += () => Close(); } private async void MainWindow_Closing(object? sender, WindowClosingEventArgs e) { if (ViewModel == null) return; e.Cancel = true; if (await ViewModel.PromptForUnsavedChangesAsync()) { e.Cancel = false; } } private void DataGrid_DoubleTapped(object? sender, TappedEventArgs e) { if (ViewModel?.SelectedSecret != null) { ViewModel.EditSecretCommand.Execute(null); } } private async void ShowNewStoreDialog() { if (ViewModel == null) return; var dialog = new NewStoreDialog(); var result = await dialog.ShowDialog(this); if (result == true) { var vm = dialog.ViewModel; await ViewModel.CreateNewStoreAsync(vm.StorePath, vm.KeyFilePath); } } private async void ShowOpenStoreDialog() { if (ViewModel == null) return; var dialog = new OpenStoreDialog(); var result = await dialog.ShowDialog(this); if (result == true) { var vm = dialog.ViewModel; await ViewModel.OpenExistingStoreAsync(vm.StorePath, vm.KeyFilePath); } } private async void ShowAddSecretDialog() { if (ViewModel == null) return; var dialog = new SecretEditDialog(); var result = await dialog.ShowDialog(this); if (result == true) { var vm = dialog.ViewModel; await ViewModel.SaveSecretAsync(vm.Key, vm.Value, isNew: true); } } private async void ShowEditSecretDialog(string key, string value) { if (ViewModel == null) return; var dialog = new SecretEditDialog(key, value); var result = await dialog.ShowDialog(this); if (result == true) { var vm = dialog.ViewModel; await ViewModel.SaveSecretAsync(vm.Key, vm.Value, isNew: false); } } }