Files
XLIMS/XLIMS.SP/ViewModels/SpoisViewModel.cs
Курнат Андрей f0e11d6379 first edit
2026-01-31 16:11:36 +03:00

92 lines
2.8 KiB
C#

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