Modifying points works
This commit is contained in:
@@ -1,6 +1,120 @@
|
||||
namespace GazpromApp.ViewModels;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
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 Image = GazpromApp.Models.Image;
|
||||
|
||||
namespace GazpromApp.ViewModels;
|
||||
|
||||
public partial class EditPointDialogViewModel : ViewModelBase
|
||||
{
|
||||
public ObservableCollection<ImageViewModel> Images { get; set; }
|
||||
|
||||
[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;
|
||||
}
|
||||
|
||||
[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(file.Path.AbsolutePath) });
|
||||
|
||||
IsImagesEmpty = Images.Count == 0;
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void DeleteImage(ImageViewModel image)
|
||||
{
|
||||
Images.Remove(image);
|
||||
IsImagesEmpty = Images.Count == 0;
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private async Task ViewImage(ImageViewModel image)
|
||||
{
|
||||
_ = await WeakReferenceMessenger.Default.Send(new ViewImageMessage(image));
|
||||
}
|
||||
|
||||
private bool CanCompleteEdit() => !string.IsNullOrWhiteSpace(Name);
|
||||
|
||||
[RelayCommand(CanExecute = nameof(CanCompleteEdit))]
|
||||
private void CompleteEdit()
|
||||
{
|
||||
Point point = new Point()
|
||||
{
|
||||
Name = Name,
|
||||
Description = Description,
|
||||
Images = Images.Select(e => e.GetImage()).ToList()
|
||||
};
|
||||
WeakReferenceMessenger.Default.Send(new EditPointDone(new PointViewModel(point)));
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private static void CancelEdit(Window w)
|
||||
{
|
||||
w.Close();
|
||||
}
|
||||
|
||||
[ObservableProperty]
|
||||
private bool _isImagesEmpty;
|
||||
|
||||
public EditPointDialogViewModel()
|
||||
{
|
||||
Name = "";
|
||||
Description = "";
|
||||
Images = [];
|
||||
IsImagesEmpty = true;
|
||||
}
|
||||
|
||||
public EditPointDialogViewModel(PointViewModel point)
|
||||
{
|
||||
Name = point.Name;
|
||||
Description = point.Description;
|
||||
Images = new ObservableCollection<ImageViewModel>((point.Images ?? []).Select(e => new ImageViewModel(e)));
|
||||
foreach (ImageViewModel imageViewModel in Images)
|
||||
{
|
||||
Debug.WriteLine(imageViewModel.FileName);
|
||||
}
|
||||
IsImagesEmpty = Images.Count == 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user