using System; using System.Collections.ObjectModel; using System.Linq; using System.Threading.Tasks; using System.Web; using Avalonia.Controls; using Avalonia.Media.Imaging; 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 EditPointViewModel : 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 WeakReferenceMessenger.Default.Send(new RequestImageMessage(this)); foreach (var file in files) { try { Images.Add(new ImageViewModel(new Image()) { FileName = new Bitmap(HttpUtility.UrlDecode(file.Path.AbsolutePath)), Parent = this}); } catch (ArgumentException) { var error = new Error { Title = "Ошибка обработки картинки", Message = $"Картинка не была добавлена: \"{HttpUtility.UrlDecode(file.Name)}\"", Severity = ErrorSeverity.Error }; _ = await WeakReferenceMessenger.Default.Send(new ErrorMessage(new ErrorViewModel(error))); } } } public ObservableCollection Equipment { get; set; } public ObservableCollection Properties { get; set; } [RelayCommand] private void AddProperty() => Properties.Add(new PropertyViewModel {Parent = this}); private void RemoveProperty(object sender, RemovePropertyMessage msg) { if (Properties.Contains(msg.Property)) Properties.Remove(msg.Property); } private bool CanCompleteEdit() => !string.IsNullOrWhiteSpace(Name); [RelayCommand(CanExecute = nameof(CanCompleteEdit))] private void CompleteEdit() { _point.Name = Name; _point.Description = Description; _point.Images = Images; _point.Equipment = Equipment; _point.Properties = Properties; _ = _point.GetPoint(); WeakReferenceMessenger.Default.Send(new EditPointMessage(_point)); } [RelayCommand] private void CancelEdit(Window w) { WeakReferenceMessenger.Default.Send(new EditPointMessage(_point)); } // Visual and other properties private readonly PointViewModel _point; public EditPointViewModel() { Name = "fsdfasdfasd"; Description = "asfasdfasdfa"; 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", Images = [] }, 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", Images = [] } ]; 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 } ]; _point = new PointViewModel(new Point()); } public EditPointViewModel(PointViewModel point) { Name = point.Name; Description = point.Description; Images = new ObservableCollection(point.Images); Images.ToList().ForEach(i => i.Parent = this); Equipment = new ObservableCollection(point.Equipment); Properties = new ObservableCollection(point.Properties); _point = point; WeakReferenceMessenger.Default.Register(this, RemoveProperty); } ~EditPointViewModel() { WeakReferenceMessenger.Default.UnregisterAll(this); } }