using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using System.Web; 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 GazpromApp.Models; using Image = GazpromApp.Models.Image; namespace GazpromApp.ViewModels; public partial class EditPointDialogViewModel : ViewModelBase { [ObservableProperty] [NotifyCanExecuteChangedFor(nameof(CompleteEditCommand))] private string? _name; [ObservableProperty] [NotifyCanExecuteChangedFor(nameof(CompleteEditCommand))] private string? _description; [ObservableProperty] private bool _isNameEditing; [ObservableProperty] private bool _isDescriptionEditing; [RelayCommand] private void EditName() { IsNameEditing = !IsNameEditing; IsDescriptionEditing = false; } [RelayCommand] private void EditDescription() { IsDescriptionEditing = !IsDescriptionEditing; IsNameEditing = false; } public ObservableCollection Images { get; set; } [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(HttpUtility.UrlDecode(file.Path.AbsolutePath) ) }); IsImagesEmpty = Images.Count == 0; } [RelayCommand] private void DeleteImage(ImageViewModel image) { Images.Remove(image); IsImagesEmpty = Images.Count == 0; } [RelayCommand] private static async Task ViewImage(ImageViewModel image) { _ = await WeakReferenceMessenger.Default.Send(new ViewImageMessage(image)); } private bool CanCompleteEdit() => !string.IsNullOrWhiteSpace(Name); [RelayCommand(CanExecute = nameof(CanCompleteEdit))] private void CompleteEdit() { IsClosed = true; Point point = new Point() { Name = Name, Description = Description, Images = Images.Select(e => e.GetImage()).ToList() }; WeakReferenceMessenger.Default.Send(new EditPointDone(new PointViewModel(point))); } [RelayCommand] private void CancelEdit(Window w) { IsClosed = true; WeakReferenceMessenger.Default.Send(new EditPointDone(_point)); } // Visual and other properties [ObservableProperty] private bool _isImagesEmpty; [ObservableProperty] private bool _isEquipmentEmpty = true; [ObservableProperty] private bool _isClosed; private readonly PointViewModel _point; public EditPointDialogViewModel() { Name = ""; Description = ""; Images = []; IsImagesEmpty = true; _point = new PointViewModel(new Point()); } public EditPointDialogViewModel(PointViewModel point) { Name = point.Name; Description = point.Description; Images = new ObservableCollection((point.Images ?? []).Select(e => new ImageViewModel(e))); foreach (ImageViewModel imageViewModel in Images) { Debug.WriteLine(imageViewModel.FileName); } IsImagesEmpty = Images.Count == 0; _point = point; } }