presence_new/not relevant/UserViewModel.cs

84 lines
2.6 KiB
C#

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<UserPresenter> _userPresentersDataSource = new List<UserPresenter>();
private ObservableCollection<UserPresenter> _users;
public ObservableCollection<UserPresenter> 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<UserPresenter>(_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<UserPresenter>(_userPresentersDataSource);
}
}
}