Files
GazpromApp/ViewModels/EditEquipmentDialogViewModel.cs
T

198 lines
6.1 KiB
C#

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 EditEquipmentDialogViewModel : ViewModelBase
{
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(CompleteEditCommand))]
private string? _name;
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(CompleteEditCommand))]
private string? _description;
[ObservableProperty]
private bool _isNameEditing;
[ObservableProperty]
private bool _isDescriptionEditing;
public ObservableCollection<ImageViewModel> Images { get; set; }
public ObservableCollection<InspectionViewModel> Inspections { get; set; }
[RelayCommand]
private void EditName()
{
IsNameEditing = !IsNameEditing;
IsDescriptionEditing = false;
}
[RelayCommand]
private void EditDescription()
{
IsDescriptionEditing = !IsDescriptionEditing;
IsNameEditing = false;
}
[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 static async Task ViewImage(ImageViewModel image) => _ = await WeakReferenceMessenger.Default.Send(new ViewImageMessage(image));
[RelayCommand]
private void DeleteImage(ImageViewModel image)
{
Images.Remove(image);
IsImagesEmpty = Images.Count == 0;
}
[RelayCommand]
private void AddInspection()
{
Inspections.Add(new InspectionViewModel { Name = "Название инспекции", InspectionDate = DateTimeOffset.Now, PeriodValue = 1, PeriodType = PeriodType.Day });
}
[RelayCommand]
private void EditInspection(InspectionViewModel inspection)
{
foreach (var item in Inspections)
if (item != inspection)
item.IsEditing = false;
inspection.IsEditing = !inspection.IsEditing;
}
[RelayCommand]
private void DeleteInspection(InspectionViewModel inspection)
{
Inspections.Remove(inspection);
}
public ObservableCollection<PropertyViewModel> 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 equipment = new Equipment
{
Name = Name,
Description = Description,
Images = Images.Select(e => e.GetImage()).ToList(),
Inspections = Inspections.Select(e => e.GetInspection()).ToList(),
Properties = Properties.Select(e => e.GetProperty()).ToList()
};
WeakReferenceMessenger.Default.Send(new EditEquipmentDone(new EquipmentViewModel(equipment)));
}
[RelayCommand]
private void CancelEdit(Window w)
{
IsClosed = true;
WeakReferenceMessenger.Default.Send(new EditEquipmentDone(_equipment));
}
// Visual and other properties
[ObservableProperty]
private bool _isImagesEmpty;
[ObservableProperty]
private bool _isClosed;
private readonly EquipmentViewModel _equipment;
// ReSharper disable once MemberCanBeMadeStatic.Global
public PeriodType[] AllPeriods => Enum.GetValues<PeriodType>()
.Where(p => p != PeriodType.Unknown)
.ToArray();
public EditEquipmentDialogViewModel()
{
Name = "Testing";
Description = "Testing";
Images = [];
Inspections = [];
Properties = [];
IsImagesEmpty = true;
_equipment = new EquipmentViewModel();
}
public EditEquipmentDialogViewModel(EquipmentViewModel equipment)
{
Name = equipment.Name;
Description = equipment.Description;
Images = equipment.Images;
Inspections = new ObservableCollection<InspectionViewModel>(equipment.Inspections.Select(e => e));
Properties = new ObservableCollection<PropertyViewModel>(equipment.Properties.Select(e => e));
IsImagesEmpty = Images.Count == 0;
_equipment = equipment;
}
}