Adding equipment works

This commit is contained in:
retr0volkov
2025-07-04 15:29:23 +03:00 Unverified
parent e4c1ded6ae
commit c0178625cd
31 changed files with 510 additions and 18 deletions
+58
View File
@@ -0,0 +1,58 @@
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using Avalonia.Media.Imaging;
using CommunityToolkit.Mvvm.ComponentModel;
using GazpromApp.Models;
namespace GazpromApp.ViewModels;
public partial class EquipmentViewModel : ViewModelBase
{
[ObservableProperty]
private string? _name;
[ObservableProperty]
private string? _description;
[ObservableProperty]
private ImageViewModel? _image;
public ObservableCollection<PropertyViewModel> Properties { get; set; }
public ObservableCollection<InspectionViewModel> Inspections { get; set; }
[ObservableProperty]
private string _nearestInspection;
public EquipmentViewModel()
{
Name = string.Empty;
Description = string.Empty;
Image = null;
Properties = [];
Inspections = [];
NearestInspection = DateTime.Now.ToLongDateString();
}
public EquipmentViewModel(Equipment equipment)
{
Name = equipment.Name;
Description = equipment.Description;
Image = new ImageViewModel(equipment.Image ?? new Image());
Properties = new ObservableCollection<PropertyViewModel>(equipment.Properties.Select(e => new PropertyViewModel(e)));
Inspections = new ObservableCollection<InspectionViewModel>(equipment.Inspections.Select(e => new InspectionViewModel(e)));
NearestInspection = DateTime.Now.ToLongDateString();
}
public Equipment GetEquipment()
{
return new Equipment()
{
Name = Name,
Description = Description,
Image = Image?.GetImage(),
};
}
}
+16
View File
@@ -0,0 +1,16 @@
using GazpromApp.Models;
namespace GazpromApp.ViewModels;
public partial class InspectionViewModel : ViewModelBase
{
public InspectionViewModel()
{
}
public InspectionViewModel(Inspection inspection)
{
}
}
+4 -1
View File
@@ -17,6 +17,7 @@ public partial class PointViewModel : ViewModelBase
[ObservableProperty]
private double? _longitude;
// EF stuff
[ObservableProperty]
private List<Image>? _images;
@@ -41,7 +42,9 @@ public partial class PointViewModel : ViewModelBase
return new Models.Point()
{
Name = this.Name,
Description = this.Description
Description = this.Description,
Equipment = this.Equipment ?? [],
Properties = this.Properties ?? [],
};
}
}
+28
View File
@@ -0,0 +1,28 @@
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using GazpromApp.Models;
namespace GazpromApp.ViewModels;
public partial class PropertyViewModel : ViewModelBase
{
[ObservableProperty]
private string _name;
[ObservableProperty]
private string _value;
public ObservableCollection<PropertyViewModel> Properties { get; set; } = [];
public PropertyViewModel()
{
Name = string.Empty;
Value = string.Empty;
}
public PropertyViewModel(Property property)
{
Name = property.Name;
Value = property.Value;
}
}
@@ -0,0 +1,6 @@
namespace GazpromApp.ViewModels;
public class EditEquipmentDialogViewModel
{
}
+49 -6
View File
@@ -12,6 +12,7 @@ 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;
@@ -75,6 +76,34 @@ public partial class EditPointDialogViewModel : ViewModelBase
{
_ = 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);
@@ -86,7 +115,8 @@ public partial class EditPointDialogViewModel : ViewModelBase
{
Name = Name,
Description = Description,
Images = Images.Select(e => e.GetImage()).ToList()
Images = Images.Select(e => e.GetImage()).ToList(),
Equipment = Equipment.Select(e => e.GetEquipment()).ToList()
};
WeakReferenceMessenger.Default.Send(new EditPointDone(new PointViewModel(point)));
}
@@ -104,7 +134,7 @@ public partial class EditPointDialogViewModel : ViewModelBase
private bool _isImagesEmpty;
[ObservableProperty]
private bool _isEquipmentEmpty = true;
private bool _isEquipmentEmpty;
[ObservableProperty]
private bool _isClosed;
@@ -116,7 +146,21 @@ public partial class EditPointDialogViewModel : ViewModelBase
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());
}
@@ -126,11 +170,10 @@ public partial class EditPointDialogViewModel : ViewModelBase
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);
}
Equipment = new ObservableCollection<EquipmentViewModel>((point.Equipment ?? []).Select(e => new EquipmentViewModel(e)));
IsImagesEmpty = Images.Count == 0;
IsEquipmentEmpty = Equipment.Count == 0;
_point = point;
}
+1 -1
View File
@@ -15,7 +15,7 @@ public partial class MainWindowViewModel : ViewModelBase
[RelayCommand]
private async Task AddPoint()
{
var point = await WeakReferenceMessenger.Default.Send(new NewPointMessage());
PointViewModel? point = await WeakReferenceMessenger.Default.Send(new NewPointMessage());
if (point != null)
Points.Add(point);
+75
View File
@@ -0,0 +1,75 @@
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;
namespace GazpromApp.ViewModels;
public partial class NewEquipmentDialogViewModel : ViewModelBase
{
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(EnterEquipmentCommand))]
private string? _name;
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(EnterEquipmentCommand))]
private string? _description;
[ObservableProperty]
private ImageViewModel? _image;
[RelayCommand]
private async Task AddImage(Window w)
{
IReadOnlyList<IStorageFile> 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 void DeleteImage()
{
Image = null;
IsImageSelected = false;
}
private bool CanEnterEquipment() => !string.IsNullOrEmpty(Name);
[RelayCommand (CanExecute = nameof(CanEnterEquipment))]
private void EnterEquipment()
{
Equipment equipment = new Equipment()
{
Name = Name,
Description = Description ?? string.Empty,
Image = (Image ?? new ImageViewModel()).GetImage()
};
WeakReferenceMessenger.Default.Send(new NewEquipmentDone(new EquipmentViewModel(equipment)));
}
[RelayCommand]
private static void CancelEnterEquipment(Window w)
{
w.Close();
}
// Visual and other stuff
[ObservableProperty]
private bool _isImageSelected;
}