82 lines
2.7 KiB
C#
82 lines
2.7 KiB
C#
using domain.UseCase;
|
|
using DynamicData;
|
|
using DynamicData.Binding;
|
|
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 GroupViewModel : ViewModelBase, IRoutableViewModel
|
|
{
|
|
private readonly List<GroupPresenter> _groupPresentersDataSource = new List<GroupPresenter>();
|
|
private ObservableCollection<GroupPresenter> _groups;
|
|
public ObservableCollection<GroupPresenter> Groups => _groups;
|
|
|
|
public GroupPresenter? SelectedGroupItem {
|
|
get => _selectedGroupItem;
|
|
set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value); }
|
|
|
|
private GroupPresenter? _selectedGroupItem;
|
|
|
|
|
|
private IGroupUseCase _groupUseCase;
|
|
public ObservableCollection<UserPresenter> Users { get => _users;}
|
|
public ObservableCollection<UserPresenter> _users;
|
|
public GroupViewModel(IGroupUseCase groupUseCase)
|
|
{
|
|
_groupUseCase = groupUseCase;
|
|
_users = new ObservableCollection<UserPresenter>();
|
|
RefreshGroups();
|
|
this.WhenAnyValue(vm => vm.SelectedGroupItem)
|
|
.Subscribe(_ =>
|
|
{ RefreshGroups();
|
|
SetUsers();
|
|
});
|
|
|
|
}
|
|
|
|
private void SetUsers()
|
|
{
|
|
if(SelectedGroupItem == null) return;
|
|
Users.Clear();
|
|
var group = _groups.First(it => it.Id == SelectedGroupItem.Id);
|
|
if(group.users == null) return;
|
|
foreach (var item in group.users)
|
|
{
|
|
Users.Add(item);
|
|
}
|
|
}
|
|
|
|
private void RefreshGroups()
|
|
{
|
|
_groupPresentersDataSource.Clear();
|
|
foreach (var item in _groupUseCase.GetGroupsWithStudents())
|
|
{
|
|
GroupPresenter groupPresenter = new GroupPresenter
|
|
{
|
|
Id = item.Id,
|
|
Name = item.Name,
|
|
users = item.Users?.Select(user => new UserPresenter
|
|
{
|
|
Name = user.Name,
|
|
Guid = user.Guid,
|
|
Group = new GroupPresenter { Id = item.Id, Name = item.Name }
|
|
}
|
|
).ToList()
|
|
};
|
|
_groupPresentersDataSource.Add(groupPresenter);
|
|
}
|
|
_groups = new ObservableCollection<GroupPresenter>(_groupPresentersDataSource);
|
|
}
|
|
public string? UrlPathSegment { get; }
|
|
public IScreen HostScreen { get; }
|
|
}
|
|
}
|
|
|