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 Clients { get; } private Client? _selectedClient; private IDataService _dataService; private readonly IRenavigator renavigator; IEnumerable? 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 dataService, IActualProject actualProject, IRenavigator renavigator) { Clients = new ObservableCollection(); _dataService = dataService; this.renavigator = renavigator; LoadClient(); SelectCommand = new SelectClientCommand(actualProject, this); //= new RelayCommand(SelectClient, () => SelectedClient != null); EditCommand = new EditClientCommand(actualProject,renavigator,this); //EditClientCommand = new RelayCommand(EditClient, () => SelectedClient != null); //AddNewClientCommand = new RelayCommand(AddNewClient); } private void edit() { //_navigator.UpdateViewModelCommand.Execute(EViewType.ClientEdit); } private async Task 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 target, IEnumerable source) { target.Clear(); foreach (var i in source) target.Add(i); } } }