using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Threading.Tasks; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Messaging; using GazpromApp.Messages; using GazpromApp.Models; namespace GazpromApp.ViewModels; public partial class PointViewModel : ViewModelBase { private Point Point { get; } [ObservableProperty] private string? _name; [ObservableProperty] private string? _description; [ObservableProperty] private DateTime _creationDate; [ObservableProperty] private double? _latitude; [ObservableProperty] private double? _longitude; [RelayCommand] private void AddEquipment() { Equipment.Add(new EquipmentViewModel(new Equipment()) {Name = "Новое оборудование", Description = "Описание оборудования", Parent = this}); WeakReferenceMessenger.Default.Send(new EditPointMessage(this)); } [RelayCommand] private void RemovePoint() { WeakReferenceMessenger.Default.Send(new RemovePointMessage(this)); } private void RemoveEquipment(object sender, RemoveEquipmentMessage msg) { if (msg.Equipment.GetEquipment().Point == GetPoint()) Equipment.Remove(msg.Equipment); } [ObservableProperty] private ObservableCollection _images; [ObservableProperty] private ObservableCollection _equipment; [ObservableProperty] private ObservableCollection _properties; public PointViewModel(Point point) { Point = point; Name = point.Name; CreationDate = point.CreationDate; Description = point.Description; Latitude = point.Latitude; Longitude = point.Longitude; Images = new ObservableCollection(point.Images.Select(i => new ImageViewModel(i))); Equipment = new ObservableCollection(point.Equipment.Select(i => new EquipmentViewModel(i){Parent = this})); Properties = new ObservableCollection(point.Properties.Select(i => new PropertyViewModel(i))); WeakReferenceMessenger.Default.Register(this, RemoveEquipment); } public Point GetPoint() { Point.Name = Name; Point.Description = Description; Point.CreationDate = CreationDate; Point.Equipment = Equipment.Select(e => e.GetEquipment()).ToList(); Point.Images = Images.Select(i => i.GetImage()).ToList(); Point.Properties = Properties.Select(p => p.GetProperty()).ToList(); return Point; } }