using System; using System.Collections.Generic; 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; 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 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) { IReadOnlyList 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; } [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); } 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(), Inspections = Inspections.Select(e => e.GetInspection()).ToList(), 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 PeriodType[] AllPeriods => Enum.GetValues() .Where(p => p != PeriodType.Unknown) .ToArray(); public EditEquipmentDialogViewModel() { Name = "Testing"; Description = "Testing"; Image = null; Inspections = []; IsImageSelected = false; _equipment = new EquipmentViewModel(); } public EditEquipmentDialogViewModel(EquipmentViewModel equipment) { Name = equipment.Name; Description = equipment.Description; Image = equipment.Image; Inspections = new ObservableCollection(equipment.Inspections.Select(e => e)); IsImageSelected = equipment.Image is { Bytes.Length: > 0 }; _equipment = equipment; } }