93 lines
2.8 KiB
C#
93 lines
2.8 KiB
C#
using System;
|
|
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;
|
|
using Image = GazpromApp.Models.Image;
|
|
using Point = GazpromApp.Models.Point;
|
|
|
|
namespace GazpromApp.ViewModels;
|
|
|
|
public partial class NewPointDialogViewModel : ViewModelBase
|
|
{
|
|
[ObservableProperty]
|
|
[NotifyCanExecuteChangedFor(nameof(EnterPointCommand))]
|
|
private string? _name;
|
|
|
|
[ObservableProperty]
|
|
[NotifyCanExecuteChangedFor(nameof(EnterPointCommand))]
|
|
private string? _description;
|
|
|
|
public ObservableCollection<ImageViewModel> Images { get; } = [];
|
|
|
|
[RelayCommand]
|
|
private async Task AddImage(Window w)
|
|
{
|
|
var files = await w.StorageProvider.OpenFilePickerAsync(new FilePickerOpenOptions
|
|
{
|
|
AllowMultiple = true,
|
|
Title = "Выберите одну или несколько картинок",
|
|
FileTypeFilter = [FilePickerFileTypes.ImageAll]
|
|
});
|
|
foreach (var file in files)
|
|
{
|
|
try
|
|
{
|
|
Images.Add(new ImageViewModel(new Image()) { FileName = new Bitmap(HttpUtility.UrlDecode(file.Path.AbsolutePath)) });
|
|
}
|
|
catch (ArgumentException)
|
|
{
|
|
var error = new Error
|
|
{
|
|
Title = "Ошибка обработки картинки",
|
|
Message = $"Картинка не была добавлена: \"{HttpUtility.UrlDecode(file.Name)}\"",
|
|
Severity = ErrorSeverity.Error
|
|
};
|
|
_ = await WeakReferenceMessenger.Default.Send(new ErrorMessage(new ErrorViewModel(error)));
|
|
}
|
|
}
|
|
|
|
IsImagesEmpty = Images.Count == 0;
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void DeleteImage(ImageViewModel image)
|
|
{
|
|
Images.Remove(image);
|
|
IsImagesEmpty = Images.Count == 0;
|
|
}
|
|
|
|
private bool CanEnterPoint() => !string.IsNullOrEmpty(Name);
|
|
|
|
[RelayCommand (CanExecute = nameof(CanEnterPoint))]
|
|
private void EnterPoint()
|
|
{
|
|
var point = new Point
|
|
{
|
|
Name = Name,
|
|
Description = Description,
|
|
Images = Images.Select(e => e.GetImage()).ToList()
|
|
};
|
|
WeakReferenceMessenger.Default.Send(new NewPointDone(new PointViewModel(point)));
|
|
}
|
|
|
|
[RelayCommand]
|
|
private static void CancelEnterPoint(Window w)
|
|
{
|
|
w.Close();
|
|
}
|
|
|
|
// Visual properties
|
|
|
|
[ObservableProperty]
|
|
private bool _isImagesEmpty = true;
|
|
} |