using Avalonia.Controls; using Avalonia.Platform.Storage; using Avalonia.Threading; using Microsoft.Extensions.DependencyInjection; namespace CatalogLite; public partial class MainWindow : Window { private readonly LiteCatalogViewModel _viewModel; public MainWindow() : this(Program.ServiceProvider.GetRequiredService()) { } public MainWindow(LiteCatalogViewModel viewModel) { InitializeComponent(); _viewModel = viewModel; DataContext = _viewModel; _viewModel.UiInvoker = action => Dispatcher.UIThread.Invoke(action); _viewModel.LoadConfigurationRequested += OnLoadConfigurationRequested; _viewModel.SelectSourceFolderRequested += OnSelectSourceFolderRequested; _viewModel.SelectDestinationFolderRequested += OnSelectDestinationFolderRequested; _viewModel.ShowMessageRequested += OnShowMessageRequested; } private async void OnLoadConfigurationRequested(object? sender, EventArgs e) { var files = await StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions { Title = "Carica configurazione XML", FileTypeFilter = [new FilePickerFileType("XML") { Patterns = ["*.xml"] }] }); if (files.Count > 0) { await _viewModel.LoadConfigurationFromFileAsync(files[0].Path.LocalPath); } } private async void OnSelectSourceFolderRequested(object? sender, EventArgs e) { var folders = await StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions { Title = "Seleziona cartella sorgente" }); if (folders.Count > 0) { _viewModel.SourcePath = LiteCatalogViewModel.NormalizeDirectoryPath(folders[0].Path.LocalPath); } } private async void OnSelectDestinationFolderRequested(object? sender, EventArgs e) { var folders = await StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions { Title = "Seleziona cartella destinazione" }); if (folders.Count > 0) { _viewModel.DestinationPath = LiteCatalogViewModel.NormalizeDirectoryPath(folders[0].Path.LocalPath); } } private async void OnShowMessageRequested(object? sender, LiteMessageEventArgs e) { await ShowMessageDialogAsync(e.Title, e.Message); } private async Task ShowMessageDialogAsync(string title, string message) { var closeButton = new Button { Content = "OK", HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Right, MinWidth = 92 }; var dialog = new Window { Title = title, Width = 420, Height = 180, CanResize = false, WindowStartupLocation = WindowStartupLocation.CenterOwner, Content = new Border { Padding = new Avalonia.Thickness(20), Child = new StackPanel { Spacing = 14, Children = { new TextBlock { Text = message, TextWrapping = Avalonia.Media.TextWrapping.Wrap }, closeButton } } } }; closeButton.Click += (_, _) => dialog.Close(); await dialog.ShowDialog(this); } }