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 DivisionsViewModel : ViewModelBase { #region Constructor public DivisionsViewModel(ILimsService limsService, IDialogService dialogService) { _limsService = limsService; _dialogService = dialogService; _limsService.Divisions.SetChanged += OnDivisionsChanged; LoadDataAsync(); } #endregion //Constructor #region Events private async void OnDivisionsChanged() { await LoadDataAsync(); } #endregion //Events #region Fields private readonly IDialogService _dialogService; private readonly ILimsService _limsService; private bool _isLoading; private DivisionViewModel _currentDivision; #endregion //Fields #region Properties public ObservableCollection AllDivisions { get; set; } = new(); public DivisionViewModel CurrentDivision { get => _currentDivision; set { _currentDivision = value; OnPropertyChanged(); } } public bool IsLoading { get => _isLoading; set { _isLoading = value; OnPropertyChanged(); } } #endregion //Properties #region Methods public async Task LoadDataAsync() { IsLoading = true; try { // Параллельная загрузка данных из разных доменов через подсервисы var divisionsTask = await _limsService.Divisions.GetAllAsync(); AllDivisions = new ObservableCollection(divisionsTask.Select(s => new DivisionViewModel(_limsService, s))); OnPropertyChanged(nameof(AllDivisions)); } finally { IsLoading = false; } } private void Add() { _dialogService.ShowDialog(new DivisionViewModel(_limsService)); } private void Edit() { _dialogService.ShowDialog(CurrentDivision); } private async Task DelAsync() { await CurrentDivision.Remove(); AllDivisions.Remove(CurrentDivision); OnPropertyChanged(nameof(AllDivisions)); } #endregion //Methods #region Commands public ICommand AddCommand => new RelayCommand(p => Add()); public ICommand EditCommand => new RelayCommand(p => Edit(), p => CurrentDivision != null); public ICommand DelCommand => new AsyncRelayCommand(DelAsync, () => CurrentDivision != null); #endregion //Commands } }