180 lines
5.6 KiB
C#
180 lines
5.6 KiB
C#
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Diagnostics;
|
|
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.Messages;
|
|
using GazpromApp.Models;
|
|
using GazpromApp.Views;
|
|
using Image = GazpromApp.Models.Image;
|
|
|
|
namespace GazpromApp.ViewModels;
|
|
|
|
public partial class EditPointDialogViewModel : ViewModelBase
|
|
{
|
|
[ObservableProperty]
|
|
[NotifyCanExecuteChangedFor(nameof(CompleteEditCommand))]
|
|
private string? _name;
|
|
|
|
[ObservableProperty]
|
|
[NotifyCanExecuteChangedFor(nameof(CompleteEditCommand))]
|
|
private string? _description;
|
|
|
|
[ObservableProperty]
|
|
private bool _isNameEditing;
|
|
|
|
[ObservableProperty]
|
|
private bool _isDescriptionEditing;
|
|
|
|
[RelayCommand]
|
|
private void EditName()
|
|
{
|
|
IsNameEditing = !IsNameEditing;
|
|
IsDescriptionEditing = false;
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void EditDescription()
|
|
{
|
|
IsDescriptionEditing = !IsDescriptionEditing;
|
|
IsNameEditing = false;
|
|
}
|
|
|
|
public ObservableCollection<ImageViewModel> Images { get; set; }
|
|
|
|
[RelayCommand]
|
|
private async Task AddImage(Window w)
|
|
{
|
|
IReadOnlyList<IStorageFile> files = await w.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions()
|
|
{
|
|
AllowMultiple = true,
|
|
Title = "Выберите одну или несколько картинок",
|
|
FileTypeFilter = [FilePickerFileTypes.ImageAll],
|
|
});
|
|
foreach (var file in files)
|
|
Images.Add(new ImageViewModel(new Image()) { FileName = new Bitmap(HttpUtility.UrlDecode(file.Path.AbsolutePath) ) });
|
|
|
|
IsImagesEmpty = Images.Count == 0;
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void DeleteImage(ImageViewModel image)
|
|
{
|
|
Images.Remove(image);
|
|
IsImagesEmpty = Images.Count == 0;
|
|
}
|
|
|
|
[RelayCommand]
|
|
private static async Task ViewImage(ImageViewModel image)
|
|
{
|
|
_ = await WeakReferenceMessenger.Default.Send(new ViewImageMessage(image));
|
|
}
|
|
|
|
public ObservableCollection<EquipmentViewModel> Equipment { get; set; }
|
|
|
|
[RelayCommand]
|
|
private async Task AddEquipment()
|
|
{
|
|
EquipmentViewModel? equipment = await WeakReferenceMessenger.Default.Send(new NewEquipmentMessage());
|
|
|
|
if (equipment != null)
|
|
Equipment.Add(equipment);
|
|
|
|
IsEquipmentEmpty = Equipment.Count == 0;
|
|
}
|
|
|
|
[RelayCommand]
|
|
private async Task EditEquipment(EquipmentViewModel equipment)
|
|
{
|
|
EquipmentViewModel e = await WeakReferenceMessenger.Default.Send(new EditEquipmentMessage(equipment)) ?? equipment;
|
|
Equipment[Equipment.IndexOf(equipment)] = e;
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void DeleteEquipment(EquipmentViewModel equipment)
|
|
{
|
|
Equipment.Remove(equipment);
|
|
|
|
IsEquipmentEmpty = Equipment.Count == 0;
|
|
}
|
|
|
|
private bool CanCompleteEdit() => !string.IsNullOrWhiteSpace(Name);
|
|
|
|
[RelayCommand(CanExecute = nameof(CanCompleteEdit))]
|
|
private void CompleteEdit()
|
|
{
|
|
IsClosed = true;
|
|
Point point = new Point()
|
|
{
|
|
Name = Name,
|
|
Description = Description,
|
|
Images = Images.Select(e => e.GetImage()).ToList(),
|
|
Equipment = Equipment.Select(e => e.GetEquipment()).ToList()
|
|
};
|
|
WeakReferenceMessenger.Default.Send(new EditPointDone(new PointViewModel(point)));
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void CancelEdit(Window w)
|
|
{
|
|
IsClosed = true;
|
|
WeakReferenceMessenger.Default.Send(new EditPointDone(_point));
|
|
}
|
|
|
|
// Visual and other properties
|
|
|
|
[ObservableProperty]
|
|
private bool _isImagesEmpty;
|
|
|
|
[ObservableProperty]
|
|
private bool _isEquipmentEmpty;
|
|
|
|
[ObservableProperty]
|
|
private bool _isClosed;
|
|
|
|
private readonly PointViewModel _point;
|
|
|
|
public EditPointDialogViewModel()
|
|
{
|
|
Name = "";
|
|
Description = "";
|
|
Images = [];
|
|
Equipment = [
|
|
new EquipmentViewModel
|
|
{
|
|
Name = "Equipment Name Very Long",
|
|
Description = "Really big description for the equipment to show the long textReally big description for the equipment to show the long textReally big description for the equipment to show the long textReally big description for the equipment to show the long text", Image = null
|
|
},
|
|
new EquipmentViewModel
|
|
{
|
|
Name = "Equipment Name Very Long",
|
|
Description = "Really big description for the equipment to show the long textReally big description for the equipment to show the long textReally big description for the equipment to show the long textReally big description for the equipment to show the long text", Image = null
|
|
}
|
|
];
|
|
|
|
IsImagesEmpty = true;
|
|
IsEquipmentEmpty = true;
|
|
|
|
_point = new PointViewModel(new Point());
|
|
}
|
|
|
|
public EditPointDialogViewModel(PointViewModel point)
|
|
{
|
|
Name = point.Name;
|
|
Description = point.Description;
|
|
Images = new ObservableCollection<ImageViewModel>((point.Images ?? []).Select(e => new ImageViewModel(e)));
|
|
Equipment = new ObservableCollection<EquipmentViewModel>((point.Equipment ?? []).Select(e => new EquipmentViewModel(e)));
|
|
|
|
IsImagesEmpty = Images.Count == 0;
|
|
IsEquipmentEmpty = Equipment.Count == 0;
|
|
|
|
_point = point;
|
|
}
|
|
} |