Files
DaSaSo/DaSaSo.ViewModel/ClientListViewModel.cs
2021-09-28 10:46:16 +02:00

94 lines
2.7 KiB
C#

using DaSaSo.Domain.Model;
using DaSaSo.Domain.Services;
using DaSaSo.ViewModel.Commands;
using DaSaSo.ViewModel.Enums;
using DaSaSo.ViewModel.Interface;
using Microsoft.Toolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace DaSaSo.ViewModel
{
public class ClientListViewModel : BaseViewModel
{
public ObservableCollection<Client> Clients { get; }
private Client? _selectedClient;
private IDataService<Client> _dataService;
private readonly IRenavigator renavigator;
IEnumerable<Client>? result;
bool _isLoading = true;
public ICommand SelectCommand { get; set; }
public ICommand EditCommand { get; set; }
public ICommand AddNewClientCommand { get; set; }
public Client SelectedClient
{
get => _selectedClient;
set
{
if(_selectedClient != value)
{
_selectedClient = value;
OnPropertyChanged();
OnPropertyChanged(nameof(CanSelectClient));
}
}
}
public bool IsLoading
{
get => _isLoading;
set
{
if(_isLoading != value)
{
_isLoading = value;
OnPropertyChanged();
}
}
}
public bool CanSelectClient => _selectedClient != null;
public ClientListViewModel(IDataService<Client> dataService, IActualProject actualProject, IRenavigator renavigator)
{
Clients = new ObservableCollection<Client>();
_dataService = dataService;
this.renavigator = renavigator;
LoadClient();
SelectCommand = new SelectClientCommand(actualProject, this); //= new RelayCommand(SelectClient, () => SelectedClient != null);
EditCommand = new EditClientCommand(_dataService,actualProject,renavigator,this);
AddNewClientCommand = new AddClientCommand(_dataService, actualProject, renavigator, this);
}
public async void LoadClient()
{
IsLoading = true;
var clients = await _dataService.GetAll();
//
InitCollection(Clients, clients);
IsLoading = false;
}
private void InitCollection(ObservableCollection<Client> target, IEnumerable<Client> source)
{
target.Clear();
foreach (var i in source)
target.Add(i);
}
}
}