Files
DaSaSo/DaSaSo.ViewModel/ClientListViewModel.cs
2021-09-15 17:31:59 +02:00

129 lines
3.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 IRelayCommand AddNewClientCommand { get; set; }
public Client SelectedClient
{
get => _selectedClient;
set
{
if(_selectedClient != value)
{
_selectedClient = value;
//SelectClientCommand.NotifyCanExecuteChanged();
//EditClientCommand.NotifyCanExecuteChanged();
OnPropertyChanged();
}
}
}
public bool IsLoading
{
get => _isLoading;
set
{
if(_isLoading != value)
{
_isLoading = value;
OnPropertyChanged();
}
}
}
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);
//EditClientCommand = new RelayCommand(EditClient, () => SelectedClient != null);
//AddNewClientCommand = new RelayCommand(AddNewClient);
}
private void edit()
{
//_navigator.UpdateViewModelCommand.Execute(EViewType.ClientEdit);
}
private async Task<Client> insertNewClient()
{
Client newClient = new Client()
{
Firstname = "",
LastName = "",
Country = "",
Postcode = ""
};
await _dataService.Create(newClient);
return newClient;
}
private async void AddNewClient()
{
var d = insertNewClient();
await d;
Mediator.Notify(Enums.EMediator.EDITCLIENT, d);
}
private void EditClient()
{
Mediator.Notify(Enums.EMediator.EDITCLIENT, SelectedClient);
}
private void SelectClient()
{
Mediator.Notify(Enums.EMediator.SELECTEDCLIENT, SelectedClient);
}
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);
}
}
}