using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using Avalonia; using Avalonia.Controls; using Avalonia.Media.Imaging; using Avalonia.Platform.Storage; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Messaging; using GazpromApp.Messages; using Image = GazpromApp.Models.Image; using Point = GazpromApp.Models.Point; namespace GazpromApp.ViewModels; public partial class NewPointDialogViewModel : ViewModelBase { [ObservableProperty] private string? _name; [ObservableProperty] private string? _description; public ObservableCollection Images { get; } = new ObservableCollection(); [RelayCommand] public void DeleteImage(ImageViewModel image) { Images.Remove(image); if (Images.Count == 0) IsImagesEmpty = true; } [RelayCommand] private async Task AddImage(Window w) { IReadOnlyList files = await w.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions() { AllowMultiple = true, Title = "Выберите одну или несколько картинок", FileTypeFilter = [FilePickerFileTypes.ImageAll], }); foreach (var file in files) Images.Add(new ImageViewModel(new Image()) { FileName = new Bitmap(file.Path.AbsolutePath) }); IsImagesEmpty = false; } [ObservableProperty] private bool _isImagesEmpty = true; private bool CanExecuteEnterPoint() => !string.IsNullOrEmpty(Name) || !string.IsNullOrEmpty(Description); [RelayCommand(CanExecute = nameof(CanExecuteEnterPoint))] public void EnterPoint() { Point point = new Point() { Name = Name, Description = Description, Images = Images.Select(e => e.GetImage()).ToList() }; WeakReferenceMessenger.Default.Send(new NewPointDone(new PointViewModel(point))); } }