2024-12-04 19:33:12 +00:00
|
|
|
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;
|
|
|
|
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;
|
|
|
|
private GroupPresenter? _selectedGroupItem;
|
2024-12-05 08:13:50 +00:00
|
|
|
public ObservableCollection<UserPresenter> Users { get => _users;}
|
|
|
|
public ObservableCollection<UserPresenter> _users;
|
|
|
|
|
|
|
|
|
|
|
|
private ObservableCollection<string> _secondComboBoxItems = new ObservableCollection<string> { "no sort", "name sort", "name sort rev" };
|
|
|
|
public ObservableCollection<string> SecondComboBoxItems => _secondComboBoxItems;
|
|
|
|
private string? _selectedSecondItem;
|
2024-12-04 19:33:12 +00:00
|
|
|
|
|
|
|
public ReactiveCommand<Unit, Unit> ButtonRemoveUsersByGroup {get; }
|
|
|
|
// public ReactiveCommand<Unit, Unit> ButtonAddUser { get; }
|
|
|
|
|
|
|
|
public GroupPresenter? SelectedGroupItem
|
|
|
|
{
|
|
|
|
get => _selectedGroupItem;
|
|
|
|
set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-12-05 08:13:50 +00:00
|
|
|
public string? SelectedSecondItem
|
|
|
|
{
|
|
|
|
get => _selectedSecondItem;
|
|
|
|
set => this.RaiseAndSetIfChanged(ref _selectedSecondItem, value);
|
|
|
|
}
|
2024-12-04 19:33:12 +00:00
|
|
|
public GroupViewModel(IGroupUseCase groupUseCase, IUserUseCase userUseCase)
|
|
|
|
{
|
|
|
|
foreach (var item in groupUseCase.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<GroupPresenter>(_groupPresentersDataSource);
|
|
|
|
|
|
|
|
_users = new ObservableCollection<UserPresenter>();
|
|
|
|
|
|
|
|
this.WhenAnyValue(vm => vm.SelectedGroupItem)
|
|
|
|
.Subscribe(_ => SetUsers());
|
2024-12-05 08:13:50 +00:00
|
|
|
|
|
|
|
this.WhenAnyValue(vm => vm.SelectedSecondItem)
|
|
|
|
.Subscribe(_ => SetUsers());
|
|
|
|
|
|
|
|
SelectedSecondItem = _secondComboBoxItems.First();
|
2024-12-04 19:33:12 +00:00
|
|
|
|
|
|
|
ButtonRemoveUsersByGroup = ReactiveCommand.Create(() => RemoveUsersByGroup(groupUseCase));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SetUsers()
|
|
|
|
{
|
|
|
|
if(SelectedGroupItem == null) return;
|
|
|
|
if (SelectedGroupItem.users == null) return;
|
2024-12-05 08:13:50 +00:00
|
|
|
var sortedUsers = SelectedSecondItem switch
|
|
|
|
{
|
|
|
|
"name sort" => SelectedGroupItem.users.OrderBy(user => user.Name),
|
|
|
|
"name sort rev" => SelectedGroupItem.users.OrderByDescending(user => user.Name),
|
|
|
|
_ => SelectedGroupItem.users
|
|
|
|
};
|
|
|
|
|
2024-12-04 19:33:12 +00:00
|
|
|
Users.Clear();
|
2024-12-05 08:13:50 +00:00
|
|
|
foreach (var item in sortedUsers)
|
2024-12-04 19:33:12 +00:00
|
|
|
{
|
|
|
|
Users.Add(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void RemoveUsersByGroup(IGroupUseCase groupUseCase)
|
|
|
|
{
|
|
|
|
if (_selectedGroupItem != null){
|
|
|
|
groupUseCase.RemoveUsersByGroup(_selectedGroupItem.Id);
|
|
|
|
}
|
|
|
|
|
|
|
|
var usersToRemove = Users.Where(user => user.Group.Id == _selectedGroupItem.Id).ToList();
|
|
|
|
foreach (var user in usersToRemove)
|
|
|
|
{
|
|
|
|
Users.Remove(user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string? UrlPathSegment { get; }
|
|
|
|
public IScreen HostScreen { get; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|