From e09931058bc7801d9456d8f4488e86baf26d74f6 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 22 Dec 2024 22:41:45 +0300 Subject: [PATCH] context_menu --- Presence.Desktop/Models/GroupPresenter.cs | 2 +- Presence.Desktop/Models/PresencePresenter.cs | 2 +- Presence.Desktop/Models/UserPresenter.cs | 4 +- Presence.Desktop/ViewModels/GroupViewModel.cs | 129 ++++++++++++++---- Presence.Desktop/Views/GroupView.axaml | 43 +++--- .../bin/Debug/net8.0/Presence.Desktop.dll | Bin 48640 -> 52736 bytes .../bin/Debug/net8.0/Presence.Desktop.pdb | Bin 22652 -> 24324 bytes Presence.Desktop/bin/Debug/net8.0/data.dll | Bin 25088 -> 25088 bytes Presence.Desktop/bin/Debug/net8.0/data.pdb | Bin 17368 -> 17392 bytes Presence.Desktop/bin/Debug/net8.0/domain.dll | Bin 30720 -> 30720 bytes Presence.Desktop/bin/Debug/net8.0/domain.pdb | Bin 21508 -> 21508 bytes .../net8.0/Avalonia/Presence.Desktop.dll | Bin 48640 -> 52736 bytes .../net8.0/Avalonia/Presence.Desktop.pdb | Bin 22652 -> 24324 bytes .../obj/Debug/net8.0/Avalonia/resources | Bin 5710 -> 6332 bytes .../net8.0/Presence.Desktop.AssemblyInfo.cs | 2 +- .../Presence.Desktop.AssemblyInfoInputs.cache | 2 +- ...nce.Desktop.csproj.AssemblyReference.cache | Bin 25702 -> 25702 bytes .../obj/Debug/net8.0/Presence.Desktop.dll | Bin 28160 -> 30720 bytes .../obj/Debug/net8.0/Presence.Desktop.pdb | Bin 21168 -> 21696 bytes .../obj/Debug/net8.0/ref/Presence.Desktop.dll | Bin 10752 -> 11776 bytes .../refint/Avalonia/Presence.Desktop.dll | Bin 10752 -> 11776 bytes .../Debug/net8.0/refint/Presence.Desktop.dll | Bin 11776 -> 12288 bytes .../Debug/net8.0/console_ui.AssemblyInfo.cs | 2 +- .../console_ui.AssemblyInfoInputs.cache | 2 +- .../console_ui.csproj.AssemblyReference.cache | Bin 12346 -> 11860 bytes data/Repository/UserRepositorylmpl.cs | 5 +- data/bin/Debug/net8.0/data.dll | Bin 25088 -> 25088 bytes data/bin/Debug/net8.0/data.pdb | Bin 17368 -> 17392 bytes data/obj/Debug/net8.0/data.AssemblyInfo.cs | 2 +- .../net8.0/data.AssemblyInfoInputs.cache | 2 +- data/obj/Debug/net8.0/data.dll | Bin 25088 -> 25088 bytes data/obj/Debug/net8.0/data.pdb | Bin 17368 -> 17392 bytes data/obj/Debug/net8.0/ref/data.dll | Bin 11776 -> 11776 bytes data/obj/Debug/net8.0/refint/data.dll | Bin 11776 -> 11776 bytes domain/bin/Debug/net8.0/data.dll | Bin 25088 -> 25088 bytes domain/bin/Debug/net8.0/data.pdb | Bin 17368 -> 17392 bytes domain/bin/Debug/net8.0/domain.dll | Bin 30720 -> 30720 bytes domain/bin/Debug/net8.0/domain.pdb | Bin 21508 -> 21508 bytes .../obj/Debug/net8.0/domain.AssemblyInfo.cs | 2 +- .../net8.0/domain.AssemblyInfoInputs.cache | 2 +- .../domain.csproj.AssemblyReference.cache | Bin 11106 -> 11106 bytes domain/obj/Debug/net8.0/domain.dll | Bin 30720 -> 30720 bytes domain/obj/Debug/net8.0/domain.pdb | Bin 21508 -> 21508 bytes domain/obj/Debug/net8.0/ref/domain.dll | Bin 14848 -> 14848 bytes domain/obj/Debug/net8.0/refint/domain.dll | Bin 14848 -> 14848 bytes .../Debug/net8.0/presence_api.AssemblyInfo.cs | 2 +- .../presence_api.AssemblyInfoInputs.cache | 2 +- ...resence_api.csproj.AssemblyReference.cache | Bin 11011 -> 11011 bytes ui/obj/Debug/net8.0/ui.AssemblyInfo.cs | 2 +- .../Debug/net8.0/ui.AssemblyInfoInputs.cache | 2 +- .../net8.0/ui.csproj.AssemblyReference.cache | Bin 11319 -> 10668 bytes 51 files changed, 149 insertions(+), 60 deletions(-) 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 @@ -