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

93 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.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<BookViewModel> 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<BookViewModel>(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
}
}