using Avalonia.Controls; using Avalonia.Interactivity; using Avalonia.Platform.Storage; using LehrerApp.Desktop.ViewModels.Students; namespace LehrerApp.Desktop.Views.Students; public partial class DocumentationDialog : Window { public DocumentationDialog() => InitializeComponent(); private void OnSave(object? sender, RoutedEventArgs e) { if (DataContext is DocumentationDialogViewModel vm) { vm.SaveCommand.Execute(null); if (vm.Result is not null) Close(true); } } private void OnCancel(object? sender, RoutedEventArgs e) { if (DataContext is DocumentationDialogViewModel vm) vm.DiscardUnsavedAttachments(); Close(false); } private async void OnAddAttachment(object? sender, RoutedEventArgs e) { if (DataContext is not DocumentationDialogViewModel vm) return; var files = await StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions { Title = "Anhang auswählen", AllowMultiple = false, }); if (files.Count == 0) return; await using var stream = await files[0].OpenReadAsync(); vm.AddAttachment(files[0].Name, stream); } private async void OnOpenAttachment(object? sender, RoutedEventArgs e) { if (DataContext is not DocumentationDialogViewModel vm) return; if (sender is not Button { Tag: AttachmentItem item }) return; using var source = vm.OpenAttachment(item); if (source is null) return; var file = await StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions { Title = "Anhang speichern unter", SuggestedFileName = item.FileName, }); if (file is null) return; await using var target = await file.OpenWriteAsync(); await source.CopyToAsync(target); } }