84 lines
2.7 KiB
C#
84 lines
2.7 KiB
C#
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<ImageViewModel> _images;
|
|
[ObservableProperty]
|
|
private ObservableCollection<EquipmentViewModel> _equipment;
|
|
[ObservableProperty]
|
|
private ObservableCollection<PropertyViewModel> _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<ImageViewModel>(point.Images.Select(i => new ImageViewModel(i)));
|
|
Equipment = new ObservableCollection<EquipmentViewModel>(point.Equipment.Select(i => new EquipmentViewModel(i){Parent = this}));
|
|
Properties = new ObservableCollection<PropertyViewModel>(point.Properties.Select(i => new PropertyViewModel(i)));
|
|
|
|
WeakReferenceMessenger.Default.Register<RemoveEquipmentMessage>(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;
|
|
}
|
|
} |