80 lines
2.3 KiB
C#
80 lines
2.3 KiB
C#
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using CRAWLER.Infrastructure;
|
|
using CRAWLER.Models;
|
|
|
|
namespace CRAWLER.ViewModels;
|
|
|
|
internal sealed class EditInstrumentWindowViewModel : ObservableObject
|
|
{
|
|
private readonly InstrumentRecord _draft;
|
|
private PendingPdfFile _selectedPendingPdf;
|
|
|
|
public EditInstrumentWindowViewModel(InstrumentRecord draft, bool isNewRecord)
|
|
{
|
|
_draft = draft ?? new InstrumentRecord();
|
|
PendingPdfFiles = new ObservableCollection<PendingPdfFile>();
|
|
ExistingAttachments = new ObservableCollection<PdfAttachment>(_draft.Attachments ?? Enumerable.Empty<PdfAttachment>());
|
|
WindowTitle = isNewRecord ? "Новая запись" : $"Редактирование: {(_draft.RegistryNumber ?? _draft.Name)}";
|
|
}
|
|
|
|
public string WindowTitle { get; }
|
|
|
|
public ObservableCollection<PendingPdfFile> PendingPdfFiles { get; }
|
|
|
|
public ObservableCollection<PdfAttachment> ExistingAttachments { get; }
|
|
|
|
public PendingPdfFile SelectedPendingPdf
|
|
{
|
|
get { return _selectedPendingPdf; }
|
|
set { SetProperty(ref _selectedPendingPdf, value); }
|
|
}
|
|
|
|
public InstrumentRecord Draft
|
|
{
|
|
get { return _draft; }
|
|
}
|
|
|
|
public void AddPendingFiles(IReadOnlyList<string> paths)
|
|
{
|
|
foreach (var path in paths.Where(path => !string.IsNullOrWhiteSpace(path)))
|
|
{
|
|
if (PendingPdfFiles.Any(item => string.Equals(item.SourcePath, path, StringComparison.OrdinalIgnoreCase)))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
PendingPdfFiles.Add(new PendingPdfFile
|
|
{
|
|
SourcePath = path,
|
|
DisplayName = System.IO.Path.GetFileName(path)
|
|
});
|
|
}
|
|
}
|
|
|
|
public void RemovePendingSelected()
|
|
{
|
|
if (SelectedPendingPdf != null)
|
|
{
|
|
PendingPdfFiles.Remove(SelectedPendingPdf);
|
|
}
|
|
}
|
|
|
|
public string[] GetPendingPaths()
|
|
{
|
|
return PendingPdfFiles.Select(item => item.SourcePath).ToArray();
|
|
}
|
|
|
|
public bool Validate(out string errorMessage)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(Draft.Name))
|
|
{
|
|
errorMessage = "Укажите наименование средства измерения.";
|
|
return false;
|
|
}
|
|
|
|
errorMessage = null;
|
|
return true;
|
|
}
|
|
}
|