92 lines
2.6 KiB
C#
92 lines
2.6 KiB
C#
using System;
|
|
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 NewEquipmentDialogViewModel : ViewModelBase
|
|
{
|
|
[ObservableProperty]
|
|
[NotifyCanExecuteChangedFor(nameof(EnterEquipmentCommand))]
|
|
private string? _name;
|
|
|
|
[ObservableProperty]
|
|
[NotifyCanExecuteChangedFor(nameof(EnterEquipmentCommand))]
|
|
private string? _description;
|
|
|
|
[ObservableProperty]
|
|
private ImageViewModel? _image;
|
|
|
|
[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 void DeleteImage()
|
|
{
|
|
Image = null;
|
|
IsImageSelected = false;
|
|
}
|
|
|
|
private bool CanEnterEquipment() => !string.IsNullOrEmpty(Name);
|
|
|
|
[RelayCommand (CanExecute = nameof(CanEnterEquipment))]
|
|
private void EnterEquipment()
|
|
{
|
|
var equipment = new Equipment
|
|
{
|
|
Name = Name,
|
|
Description = Description ?? string.Empty,
|
|
Image = (Image ?? new ImageViewModel()).GetImage()
|
|
|
|
};
|
|
WeakReferenceMessenger.Default.Send(new NewEquipmentDone(new EquipmentViewModel(equipment)));
|
|
}
|
|
|
|
[RelayCommand]
|
|
private static void CancelEnterEquipment(Window w)
|
|
{
|
|
w.Close();
|
|
}
|
|
|
|
// Visual and other stuff
|
|
|
|
[ObservableProperty]
|
|
private bool _isImageSelected;
|
|
} |