using domain.UseCase; using DynamicData; using DynamicData.Binding; using Microsoft.EntityFrameworkCore.Metadata.Internal; using Presence.Desktop.Models; using ReactiveUI; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Reactive.Linq; using Tmds.DBus.Protocol; namespace Presence.Desktop.ViewModels { public class UserViewModel: ViewModelBase, IRoutableViewModel { private readonly List _userPresentersDataSource = new List(); private ObservableCollection _users; public ObservableCollection Users => _users; private IUserUseCase _userUseCase; private UserPresenter? _selectedUserItem; public UserPresenter? SelectedUserItem { get => _selectedUserItem; set => this.RaiseAndSetIfChanged(ref _selectedUserItem, value); } public string? UrlPathSegment { get; } public IScreen HostScreen { get; } public UserViewModel(IUserUseCase userUseCase) { foreach (var user in userUseCase.GetStudents()) { UserPresenter userPresenter = new UserPresenter { Id = user.Id, Name = user.FIO, Group = new GroupPresenter { Name = user.Group.Name, Id = user.Group.Id, } }; _userPresentersDataSource.Add(userPresenter); } _users = new ObservableCollection(_userPresentersDataSource); _userUseCase = userUseCase; RefreshUsers(); this.WhenAnyValue(vm => vm.SelectedUserItem) .Subscribe(_ => { RefreshUsers(); }); } private void RefreshUsers() { _userPresentersDataSource.Clear(); foreach (var user in _userUseCase.GetStudents()) { UserPresenter userPresenter = new UserPresenter { Id = user.Id, Name = user.FIO, Group = new GroupPresenter { Name = user.Group.Name, Id = user.Group.Id, } }; _userPresentersDataSource.Add(userPresenter); } _users = new ObservableCollection(_userPresentersDataSource); } } }