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