diff --git a/Models/Equipment.cs b/Models/Equipment.cs index b657ee7..5f50c56 100644 --- a/Models/Equipment.cs +++ b/Models/Equipment.cs @@ -21,6 +21,6 @@ public class Equipment // EF stuff public Image? Image { get; set; } public Models.Point? Point { get; set; } - public List Properties { get; } = []; - public List Inspections { get; } = []; + public List Properties { get; set; } = []; + public List Inspections { get; set; } = []; } \ No newline at end of file diff --git a/ViewModels/EditEquipmentDialogViewModel.cs b/ViewModels/EditEquipmentDialogViewModel.cs index 8236406..b473afc 100644 --- a/ViewModels/EditEquipmentDialogViewModel.cs +++ b/ViewModels/EditEquipmentDialogViewModel.cs @@ -1,6 +1,131 @@ -namespace GazpromApp.ViewModels; +using System.Collections.Generic; +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; -public class EditEquipmentDialogViewModel +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; + + [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; + + Image = IsImageSelected ? new ImageViewModel(new Models.Image()) { FileName = new Bitmap(HttpUtility.UrlDecode(files[0].Path.AbsolutePath)) } : null; + } + + [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; + } + + 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(), + 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 EditEquipmentDialogViewModel() + { + Name = ""; + Description = ""; + Image = null; + + IsImageSelected = false; + + _equipment = new EquipmentViewModel(new Equipment()); + } + + public EditEquipmentDialogViewModel(EquipmentViewModel equipment) + { + Name = equipment.Name; + Description = equipment.Description; + Image = equipment.Image; + + IsImageSelected = equipment.Image is { Bytes.Length: > 0 }; + + _equipment = equipment; + } } \ No newline at end of file diff --git a/Views/EditEquipmentDialog.axaml b/Views/EditEquipmentDialog.axaml index 13fa53b..e103004 100644 --- a/Views/EditEquipmentDialog.axaml +++ b/Views/EditEquipmentDialog.axaml @@ -2,8 +2,78 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" - mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" + xmlns:vm="using:GazpromApp.ViewModels" + x:Name="Window" + mc:Ignorable="d" d:DesignWidth="1000" d:DesignHeight="600" + Width="1000" Height="600" x:Class="GazpromApp.Views.EditEquipmentDialog" - Title="EditEquipmentDialog"> - Welcome to Avalonia! + x:DataType="vm:EditEquipmentDialogViewModel" + Title="EditEquipmentDialog" + WindowStartupLocation="CenterOwner" + Closing="Window_OnClosing" + Closed="Window_OnClosed"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -