148 lines
4.0 KiB
C#
148 lines
4.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
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;
|
|
|
|
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;
|
|
|
|
[RelayCommand]
|
|
private void EditName()
|
|
{
|
|
IsNameEditing = !IsNameEditing;
|
|
IsDescriptionEditing = false;
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void EditDescription()
|
|
{
|
|
IsDescriptionEditing = !IsDescriptionEditing;
|
|
IsNameEditing = false;
|
|
}
|
|
|
|
[RelayCommand]
|
|
private async Task AddImage(Window w)
|
|
{
|
|
IReadOnlyList<IStorageFile> files = await w.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions()
|
|
{
|
|
AllowMultiple = false,
|
|
Title = "Выберите картинку",
|
|
FileTypeFilter = [FilePickerFileTypes.ImageAll],
|
|
});
|
|
|
|
IsImageSelected = files.Count > 0;
|
|
|
|
try
|
|
{
|
|
Image = IsImageSelected ? new ImageViewModel(new Models.Image()) { FileName = new Bitmap(HttpUtility.UrlDecode(files[0].Path.AbsolutePath)) } : null;
|
|
}
|
|
catch (ArgumentException)
|
|
{
|
|
Error 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;
|
|
}
|
|
|
|
private bool CanCompleteEdit() => !string.IsNullOrWhiteSpace(Name);
|
|
|
|
[RelayCommand(CanExecute = nameof(CanCompleteEdit))]
|
|
private void CompleteEdit()
|
|
{
|
|
IsClosed = true;
|
|
Equipment equipment = new Equipment()
|
|
{
|
|
Name = Name,
|
|
Description = Description,
|
|
Image = Image?.GetImage(),
|
|
Properties = [],
|
|
};
|
|
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 EditEquipmentDialogViewModel()
|
|
{
|
|
Name = "";
|
|
Description = "";
|
|
Image = null;
|
|
|
|
IsImageSelected = false;
|
|
|
|
_equipment = new EquipmentViewModel(new Equipment());
|
|
}
|
|
|
|
public EditEquipmentDialogViewModel(EquipmentViewModel equipment)
|
|
{
|
|
Name = equipment.Name;
|
|
Description = equipment.Description;
|
|
Image = equipment.Image;
|
|
|
|
IsImageSelected = equipment.Image is { Bytes.Length: > 0 };
|
|
|
|
_equipment = equipment;
|
|
}
|
|
} |