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(); ExistingAttachments = new ObservableCollection(_draft.Attachments ?? Enumerable.Empty()); WindowTitle = isNewRecord ? "Новая запись" : $"Редактирование: {(_draft.RegistryNumber ?? _draft.Name)}"; } public string WindowTitle { get; } public ObservableCollection PendingPdfFiles { get; } public ObservableCollection ExistingAttachments { get; } public PendingPdfFile SelectedPendingPdf { get { return _selectedPendingPdf; } set { SetProperty(ref _selectedPendingPdf, value); } } public InstrumentRecord Draft { get { return _draft; } } public void AddPendingFiles(IReadOnlyList 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; } }