using System; using System.Collections.ObjectModel; 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.Enums; 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) { var files = await w.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions { AllowMultiple = true, Title = "Выберите одну или несколько картинок", FileTypeFilter = [FilePickerFileTypes.ImageAll] }); foreach (var file in files) { try { Images.Add(new ImageViewModel(new Image()) { FileName = new Bitmap(HttpUtility.UrlDecode(file.Path.AbsolutePath) ) }); } catch (ArgumentException) { var error = new Error { Title = "Ошибка обработки картинки", Message = $"Картинка не была добавлена: \"{HttpUtility.UrlDecode(file.Name)}\"", Severity = ErrorSeverity.Error }; _ = await WeakReferenceMessenger.Default.Send(new ErrorMessage(new ErrorViewModel(error))); } } 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)); public ObservableCollection Equipment { get; set; } [RelayCommand] private async Task AddEquipment() { var equipment = await WeakReferenceMessenger.Default.Send(new NewEquipmentMessage()); if (equipment != null) Equipment.Add(equipment); IsEquipmentEmpty = Equipment.Count == 0; } [RelayCommand] private async Task EditEquipment(EquipmentViewModel equipment) { var e = await WeakReferenceMessenger.Default.Send(new EditEquipmentMessage(equipment)) ?? equipment; Equipment[Equipment.IndexOf(equipment)] = e; } [RelayCommand] private void DeleteEquipment(EquipmentViewModel equipment) { Equipment.Remove(equipment); IsEquipmentEmpty = Equipment.Count == 0; } public ObservableCollection Properties { get; set; } [RelayCommand] private void AddPropertySimple() => Properties.Add(new PropertyViewModel()); [RelayCommand] private static void AddProperty(PropertyViewModel property) => property.Properties.Add(new PropertyViewModel(property)); [RelayCommand] private static void EditProperty(PropertyViewModel property) => property.IsEditing = !property.IsEditing; [RelayCommand] private void DeleteProperty(PropertyViewModel property) { if (property.Parent is null) Properties.Remove(property); else property.Parent?.Properties.Remove(property); } private bool CanCompleteEdit() => !string.IsNullOrWhiteSpace(Name); [RelayCommand(CanExecute = nameof(CanCompleteEdit))] private void CompleteEdit() { IsClosed = true; var point = new Point { Name = Name, Description = Description, Images = Images.Select(e => e.GetImage()).ToList(), Equipment = Equipment.Select(e => e.GetEquipment()).ToList(), Properties = Properties.Select(e => e.GetProperty()).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; [ObservableProperty] private bool _isClosed; private readonly PointViewModel _point; public EditPointDialogViewModel() { Name = ""; Description = ""; Images = []; Equipment = [ new EquipmentViewModel { Name = "Equipment Name Very Long", Description = "Really big description for the equipment to show the long textReally big description for the equipment to show the long textReally big description for the equipment to show the long textReally big description for the equipment to show the long text", Image = null }, new EquipmentViewModel { Name = "Equipment Name Very Long", Description = "Really big description for the equipment to show the long textReally big description for the equipment to show the long textReally big description for the equipment to show the long textReally big description for the equipment to show the long text", Image = null } ]; Properties = [ new PropertyViewModel { Name = "Property Name", Value = "Property Value", Properties = [ new PropertyViewModel { Name = "Property Name", Value = "Property Value", Properties = [ new PropertyViewModel { Name = "Property Name", Value = "Property Value", Properties = [], Parent = null }, new PropertyViewModel { Name = "Property Name", Value = "Property Value", Properties = [], Parent = null }, new PropertyViewModel { Name = "Property Name", Value = "Property Value", Properties = [], Parent = null } ], Parent = null }, new PropertyViewModel { Name = "Property Name", Value = "Property Value", Properties = [], Parent = null } ], Parent = null }, new PropertyViewModel { Name = "Property Name", Value = "Property Value", Properties = [], Parent = null } ]; IsImagesEmpty = true; IsEquipmentEmpty = 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))); Equipment = new ObservableCollection((point.Equipment ?? []).Select(e => new EquipmentViewModel(e))); Properties = new ObservableCollection((point.Properties ?? []).Select(e => new PropertyViewModel(e))); IsImagesEmpty = Images.Count == 0; IsEquipmentEmpty = Equipment.Count == 0; _point = point; } }