presence_sample_v2/Presence.Desktop/ViewModels/MainWindowViewModel.cs

74 lines
2.3 KiB
C#
Raw Normal View History

2024-11-22 12:18:04 +00:00
using domain.UseCase;
2024-11-25 10:47:17 +00:00
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;
2024-11-22 12:18:04 +00:00
namespace Presence.Desktop.ViewModels
{
public class MainWindowViewModel : ViewModelBase
{
private readonly IGroupUseCase _groupService;
2024-11-25 10:47:17 +00:00
private 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;
public ObservableCollection<UserPresenter> Users { get => _users;}
public ObservableCollection<UserPresenter> _users;
public MainWindowViewModel(IGroupUseCase groupUseCase)
{
_groupService = groupUseCase;
foreach (var item in _groupService.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);
_users = new ObservableCollection<UserPresenter>();
this.WhenAnyValue(vm => vm.SelectedGroupItem)
.Subscribe(_ => SetUsers());
}
private void SetUsers()
{
if(SelectedGroupItem == null) return;
if (SelectedGroupItem.users == null) return;
Users.Clear();
foreach (var item in SelectedGroupItem.users)
{
Users.Add(item);
}
2024-11-22 12:18:04 +00:00
}
}
}
2024-11-25 10:47:17 +00:00