using Demo.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 MainWindowViewModel : ViewModelBase { private readonly IGroupUseCase _groupService; private List groupPresentersDataSource = new List(); private ObservableCollection _groups; public ObservableCollection Groups => _groups; public GroupPresenter? SelectedGroupItem { get => _selectedGroupItem; set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value); } private GroupPresenter? _selectedGroupItem; public ObservableCollection Users { get => _users;} public ObservableCollection _users; public MainWindowViewModel(IGroupUseCase groupUseCase) { _groupService = groupUseCase; foreach (var item in _groupService.GetAllGroupsWithUsers()) { GroupPresenter groupPresenter = new GroupPresenter { Id = item.ID, Name = item.Name, users = item.Users?.Select(user => new UserPresenter { Name = user.FIO, Guid = user.Guid, Group = new GroupPresenter { Id = item.ID, Name = item.Name } } ).ToList() }; groupPresentersDataSource.Add(groupPresenter); } _groups = new ObservableCollection(groupPresentersDataSource); _users = new ObservableCollection(); 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); } } } }