202 lines
5.9 KiB
C#
202 lines
5.9 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;
|
|
|
|
[ObservableProperty]
|
|
private ImageViewModel? _image;
|
|
|
|
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 = false,
|
|
Title = "Выберите картинку",
|
|
FileTypeFilter = [FilePickerFileTypes.ImageAll]
|
|
});
|
|
|
|
IsImageSelected = files.Count > 0;
|
|
|
|
try
|
|
{
|
|
Image = IsImageSelected ? new ImageViewModel(new Image()) { FileName = new Bitmap(HttpUtility.UrlDecode(files[0].Path.AbsolutePath)) } : null;
|
|
}
|
|
catch (ArgumentException)
|
|
{
|
|
var error = new Error
|
|
{
|
|
Title = "Ошибка обработки картинки",
|
|
Message = "Картинка не была добавлена",
|
|
Severity = ErrorSeverity.Error
|
|
};
|
|
_ = await WeakReferenceMessenger.Default.Send(new ErrorMessage(new ErrorViewModel(error)));
|
|
|
|
IsImageSelected = false;
|
|
}
|
|
}
|
|
|
|
[RelayCommand]
|
|
private async Task ViewImage()
|
|
{
|
|
if (Image is null) return;
|
|
_ = await WeakReferenceMessenger.Default.Send(new ViewImageMessage(Image));
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void DeleteImage()
|
|
{
|
|
Image = null;
|
|
IsImageSelected = false;
|
|
}
|
|
|
|
[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,
|
|
Image = Image?.GetImage(),
|
|
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 _isImageSelected;
|
|
|
|
[ObservableProperty]
|
|
private bool _isClosed;
|
|
|
|
private readonly EquipmentViewModel _equipment;
|
|
|
|
public PeriodType[] AllPeriods => Enum.GetValues<PeriodType>()
|
|
.Where(p => p != PeriodType.Unknown)
|
|
.ToArray();
|
|
|
|
public EditEquipmentDialogViewModel()
|
|
{
|
|
Name = "Testing";
|
|
Description = "Testing";
|
|
Image = null;
|
|
Inspections = [];
|
|
Properties = [];
|
|
|
|
IsImageSelected = false;
|
|
|
|
_equipment = new EquipmentViewModel();
|
|
}
|
|
|
|
public EditEquipmentDialogViewModel(EquipmentViewModel equipment)
|
|
{
|
|
Name = equipment.Name;
|
|
Description = equipment.Description;
|
|
Image = equipment.Image;
|
|
Inspections = new ObservableCollection<InspectionViewModel>(equipment.Inspections.Select(e => e));
|
|
Properties = new ObservableCollection<PropertyViewModel>(equipment.Properties.Select(e => e));
|
|
|
|
IsImageSelected = equipment.Image is { Bytes.Length: > 0 };
|
|
|
|
_equipment = equipment;
|
|
}
|
|
} |