WIP dependency Injection
This commit is contained in:
117
DaSaSo.ViewModel/ClientListViewModel.cs
Normal file
117
DaSaSo.ViewModel/ClientListViewModel.cs
Normal file
@@ -0,0 +1,117 @@
|
||||
using DaSaSo.Domain.Model;
|
||||
using DaSaSo.Domain.Services;
|
||||
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;
|
||||
IEnumerable<Client>? result;
|
||||
bool _isLoading = true;
|
||||
|
||||
public IRelayCommand SelectClientCommand { get; set; }
|
||||
public IRelayCommand EditClientCommand { 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)
|
||||
{
|
||||
Clients = new ObservableCollection<Client>();
|
||||
_dataService = dataService;
|
||||
|
||||
|
||||
//LoadClient();
|
||||
SelectClientCommand = new RelayCommand(SelectClient, () => SelectedClient != null);
|
||||
EditClientCommand = new RelayCommand(EditClient, () => SelectedClient != null);
|
||||
AddNewClientCommand = new RelayCommand(AddNewClient);
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user