diff --git a/Presence.Desktop/Models/GroupPresenter.cs b/Presence.Desktop/Models/GroupPresenter.cs index af7f88e..8a53ba9 100644 --- a/Presence.Desktop/Models/GroupPresenter.cs +++ b/Presence.Desktop/Models/GroupPresenter.cs @@ -11,7 +11,7 @@ using System.Threading.Tasks; public class GroupPresenter { public int Id { get; set; } - public string Name { get; set; } + public string? Name { get; set; } public IEnumerable? users { get; set; } = null; } } \ No newline at end of file diff --git a/Presence.Desktop/Models/PresencePresenter.cs b/Presence.Desktop/Models/PresencePresenter.cs index 77dcf55..9a3b930 100644 --- a/Presence.Desktop/Models/PresencePresenter.cs +++ b/Presence.Desktop/Models/PresencePresenter.cs @@ -14,6 +14,6 @@ using System.Threading.Tasks; public bool IsAttedance {get; set; } public DateOnly Date {get; set; } public int LessonNumber {get; set; } - public UserPresenter user { get; set; } + public UserPresenter? user { get; set; } } } \ No newline at end of file diff --git a/Presence.Desktop/Models/UserPresenter.cs b/Presence.Desktop/Models/UserPresenter.cs index 49eb718..ce6692e 100644 --- a/Presence.Desktop/Models/UserPresenter.cs +++ b/Presence.Desktop/Models/UserPresenter.cs @@ -18,7 +18,7 @@ namespace Presence.Desktop.Models } public Guid Guid { get; set; } - public string Name { get; set; } - public GroupPresenter Group { get; set; } + public string? Name { get; set; } + public GroupPresenter? Group { get; set; } } } \ No newline at end of file diff --git a/Presence.Desktop/ViewModels/GroupViewModel.cs b/Presence.Desktop/ViewModels/GroupViewModel.cs index 3511e6b..45df5d0 100644 --- a/Presence.Desktop/ViewModels/GroupViewModel.cs +++ b/Presence.Desktop/ViewModels/GroupViewModel.cs @@ -12,6 +12,8 @@ using System.Linq; using System.Reactive; using System.Reactive.Linq; using Avalonia.Controls; +using Presence.Desktop.Views; +using Avalonia.Input; namespace Presence.Desktop.ViewModels { @@ -19,7 +21,6 @@ namespace Presence.Desktop.ViewModels { private readonly IGroupUseCase _groupUseCase; private readonly IUserUseCase _userUseCase; - private readonly List _groupPresentersDataSource = new List(); private ObservableCollection _groups; public ObservableCollection Groups => _groups; @@ -44,9 +45,44 @@ namespace Presence.Desktop.ViewModels set => this.RaiseAndSetIfChanged(ref _selectedSecondItem, value); } + private UserPresenter? _selectedUser; + public UserPresenter? SelectedUser + { + get => _selectedUser; + set => this.RaiseAndSetIfChanged(ref _selectedUser, value); + } + + private ObservableCollection _selectedUsers = new ObservableCollection(); + public ObservableCollection SelectedUsers + { + get => _selectedUsers; + set => this.RaiseAndSetIfChanged(ref _selectedUsers, value); + } + + private bool _isMultipleUsersSelected; + public bool IsMultipleUsersSelected + { + get => _isMultipleUsersSelected; + set => this.RaiseAndSetIfChanged(ref _isMultipleUsersSelected, value); + } + + private string? _newFIO; + public string? NewFIO + { + get => _newFIO; + set => this.RaiseAndSetIfChanged(ref _newFIO, value); + } + + public UserPresenter? SelectedUserForUpdate + { + get => SelectedUsers?.FirstOrDefault(); + set => this.RaiseAndSetIfChanged(ref _selectedUser, value); + } + public ReactiveCommand ButtonRemoveUsersByGroup { get; } public ReactiveCommand ImportStudentsCommand { get; } - public ReactiveCommand RemoveUsersContext { get; } + public ReactiveCommand, Unit> RemoveSelectedUsersCommand { get; } + public ReactiveCommand UpdateUserFIOCommand { get; } public GroupViewModel(IGroupUseCase groupUseCase, IUserUseCase userUseCase) { @@ -68,29 +104,15 @@ namespace Presence.Desktop.ViewModels this.WhenAnyValue(vm => vm.SelectedSecondItem) .Subscribe(_ => SetUsers()); + this.WhenAnyValue(vm => vm.SelectedUsers.Count) + .Subscribe(count => IsMultipleUsersSelected = count > 1); + SelectedSecondItem = _secondComboBoxItems.First(); ButtonRemoveUsersByGroup = ReactiveCommand.Create(() => RemoveUsersByGroup()); ImportStudentsCommand = ReactiveCommand.CreateFromTask(async () => await ImportStudents()); - RemoveUsersContext = ReactiveCommand.Create(() => RemoveUsersByContextMenu()); - } - - private void RemoveUsersByContextMenu(){ - if (SelectedGroupItem == null || SelectedGroupItem.Id == null) return; - - var selectedUsers = Users.Where(u => u.IsSelected).ToList(); - - foreach (var user in selectedUsers) - { - var isRemoved = _userUseCase.RemoveUserByFIOnGroup(user.Name, SelectedGroupItem.Id); - if (isRemoved) - { - Users.Remove(user); - } - } - - RefreshGroups(); - SetUsers(); + RemoveSelectedUsersCommand = ReactiveCommand.Create>(DeleteUsers); + UpdateUserFIOCommand = ReactiveCommand.Create(UpdateUserFIO); } private void SetUsers() @@ -117,7 +139,7 @@ namespace Presence.Desktop.ViewModels private void RefreshGroups() { - _groupPresentersDataSource.Clear(); + _groups.Clear(); foreach (var group in _groupUseCase.GetAllGroupsWithUsers()) { @@ -132,10 +154,8 @@ namespace Presence.Desktop.ViewModels Group = new GroupPresenter { Id = group.ID, Name = group.Name } }).ToList() }; - _groupPresentersDataSource.Add(groupPresenter); + _groups.Add(groupPresenter); } - - _groups = new ObservableCollection(_groupPresentersDataSource); } private void RemoveUsersByGroup() @@ -147,7 +167,6 @@ namespace Presence.Desktop.ViewModels SetUsers(); } -//import users private async System.Threading.Tasks.Task ImportStudents() { if (SelectedGroupItem == null || SelectedGroupItem.Id == null) return; @@ -193,6 +212,64 @@ namespace Presence.Desktop.ViewModels } } + private void DeleteUsers(IEnumerable users) + { + if (users == null) return; + + var usersToDelete = new List(users); + + foreach (var user in usersToDelete) + { + var isDeleted = _userUseCase.RemoveUserByGuid(user.Guid); + if (isDeleted) + { + Users.Remove(user); + } + } + } + + private void DeleteUser(UserPresenter user) { + + if (user == null) return; + + var isDeleted = _userUseCase.RemoveUserByGuid(user.Guid); + if (isDeleted) { + Users.Remove(user); + } + } + + private void UpdateUserFIO(UserPresenter user) + { + Console.WriteLine("start"); + + if (user == null || string.IsNullOrWhiteSpace(NewFIO)) return; + + user.Name = NewFIO; + var updatedUser = new User + { + Guid = user.Guid, + FIO = NewFIO, + Group = new Group + { + ID = SelectedGroupItem.Id, + Name = SelectedGroupItem.Name + } + }; + var isUpdated = _userUseCase.UpdateUser(updatedUser); + + Console.WriteLine("end"); + + if (isUpdated != null) + { + NewFIO = string.Empty; + // SelectedUsers.Clear(); // Очистить выбранных пользователей + // SelectedUser = null; // Обнулить выбранного пользователя + // SelectedUserForUpdate = null; + RefreshGroups(); + SetUsers(); + } + } + public string? UrlPathSegment { get; } public IScreen HostScreen { get; } } diff --git a/Presence.Desktop/Views/GroupView.axaml b/Presence.Desktop/Views/GroupView.axaml index 9b7b6e2..eb4c800 100644 --- a/Presence.Desktop/Views/GroupView.axaml +++ b/Presence.Desktop/Views/GroupView.axaml @@ -13,21 +13,17 @@ -