using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; using System.Windows.Input; using XLIMS.CONTRACT; using XLIMS.DATA.Models; using XLIMS.MVVM.Base; namespace XLIMS.SP.ViewModels { public class BooksViewModel:ViewModelBase { #region Constructor public BooksViewModel(ILimsService limsService,IDialogService dialogService) { _limsService = limsService; _dialogService = dialogService; _limsService.Books.SetChanged += OnBooksChanged; LoadDataAsync(); } #endregion //Constructor #region Events private async void OnBooksChanged() { await LoadDataAsync(); } #endregion //Events #region Fields private readonly ILimsService _limsService; private readonly IDialogService _dialogService; private bool _isLoading; private BookViewModel _currentBook; #endregion //Fields #region Properties public ObservableCollection AllBooks { get; set; } = new(); public BookViewModel CurrentBook { get=>_currentBook; set { _currentBook = value; OnPropertyChanged(); } } public bool IsLoading { get => _isLoading; set { _isLoading = value; OnPropertyChanged(); } } #endregion //Properties #region Methods public async Task LoadDataAsync() { IsLoading = true; try { // Параллельная загрузка данных из разных доменов через подсервисы var booksTask = await _limsService.Books.GetAllAsync(); AllBooks = new ObservableCollection(booksTask.Select(s => new BookViewModel(_limsService, s))); OnPropertyChanged(nameof(AllBooks)); } finally { IsLoading = false; } } private void Add() { _dialogService.ShowDialog(new BookViewModel(_limsService)); } private void Edit() { _dialogService.ShowDialog(CurrentBook); } private async Task DelAsync() { await CurrentBook.Remove(); AllBooks.Remove(CurrentBook); OnPropertyChanged(nameof(AllBooks)); } #endregion //Methods #region Commands public ICommand AddCommand =>new RelayCommand(p=> Add()); public ICommand EditCommand => new RelayCommand(p => Edit(),p=>CurrentBook!=null); public ICommand DelCommand => new AsyncRelayCommand(DelAsync, ()=>CurrentBook != null); #endregion //Commands } }