diff --git a/Presence.Desktop/ViewModels/GroupViewModel.cs b/Presence.Desktop/ViewModels/GroupViewModel.cs index 963e21e..6b9d091 100644 --- a/Presence.Desktop/ViewModels/GroupViewModel.cs +++ b/Presence.Desktop/ViewModels/GroupViewModel.cs @@ -21,110 +21,153 @@ namespace Presence.Desktop.ViewModels public GroupPresenter? SelectedGroupItem { get => _selectedGroupItem; - set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value); } + set => this.RaiseAndSetIfChanged(ref _selectedGroupItem, value); + } private GroupPresenter? _selectedGroupItem; - public ReactiveCommand DeleteUsersByGroupCommand { get; } - - public ReactiveCommand ImportStudentsCommand { get; } - public ObservableCollection Users { get => _users;} public ObservableCollection _users; + private IGroupUseCase _groupUseCase; + private IUserUseCase _userUseCase; + + private ObservableCollection _secondComboBoxItems = new ObservableCollection { "no sort", "name sort", "name sort rev" }; + public ObservableCollection SecondComboBoxItems => _secondComboBoxItems; + private string? _selectedSecondItem; + + + public ReactiveCommand DeleteUsersByGroupCommand { get; } + public ReactiveCommand ImportStudentsCommand { get; } + + + public string? SelectedSecondItem + { + get => _selectedSecondItem; + set => this.RaiseAndSetIfChanged(ref _selectedSecondItem, value); + } + + private UserPresenter? _selectedUser; + public UserPresenter? SelectedUser + { + get => _selectedUser; + set => this.RaiseAndSetIfChanged(ref _selectedUser, value); + } + + private string? _newFIO; + public string? NewFIO + { + get => _newFIO; + set => this.RaiseAndSetIfChanged(ref _newFIO, value); + } + + public ReactiveCommand DeleteUserCommand { get; } + public ReactiveCommand UpdateUserFIOCommand { get; } + public GroupViewModel(IGroupUseCase groupUseCase, IUserUseCase userUseCase) { - foreach (var item in groupUseCase.GetAllGroupsWithUsers()) - { - GroupPresenter groupPresenter = new GroupPresenter + _groupUseCase = groupUseCase; + _userUseCase = userUseCase; + + _groups = new ObservableCollection(_groupPresentersDataSource); + _users = new ObservableCollection(); + + RefreshGroups(); + + this.WhenAnyValue(vm => vm.SelectedGroupItem) + .Subscribe(_ => { - Id = item.ID, - Name = item.Name, - users = item.Users?.Select(user => new UserPresenter + RefreshGroups(); + SetUsers(); + }); + + this.WhenAnyValue(vm => vm.SelectedSecondItem) + .Subscribe(_ => SetUsers()); + + SelectedSecondItem = _secondComboBoxItems.First(); + + DeleteUsersByGroupCommand = ReactiveCommand.Create(() => DeleteUsersByGroupID()); + ImportStudentsCommand = ReactiveCommand.CreateFromTask(async () => await ImportStudents()); + + DeleteUserCommand = ReactiveCommand.Create(DeleteUser); + UpdateUserFIOCommand = ReactiveCommand.Create(UpdateUserFIO); + } + + private void RefreshGroups() + { + _groupPresentersDataSource.Clear(); + + foreach (var group in _groupUseCase.GetAllGroupsWithUsers()) + { + var groupPresenter = new GroupPresenter + { + Id = group.ID, + Name = group.Name, + users = group.Users?.Select(user => new UserPresenter { Name = user.FIO, Guid = user.Guid, - Group = new GroupPresenter { Id = item.ID, Name = item.Name } - } - ).ToList() + Group = new GroupPresenter { Id = group.ID, Name = group.Name } + }).ToList() }; _groupPresentersDataSource.Add(groupPresenter); } + _groups = new ObservableCollection(_groupPresentersDataSource); - - _users = new ObservableCollection(); - - DeleteUsersByGroupCommand = ReactiveCommand.Create(() => DeleteUsersByGroupID(userUseCase)); - - this.WhenAnyValue(vm => vm.SelectedGroupItem) - .Subscribe(_ => SetUsers()); - - ImportStudentsCommand = ReactiveCommand.CreateFromTask(async () => - { - var openFileDialog = new OpenFileDialog - { - Filters = new List - { - new FileDialogFilter { Name = "CSV Files", Extensions = { "csv" } } - }, - AllowMultiple = false - }; - - var result = await openFileDialog.ShowAsync(new Window()); - if (result?.Length > 0) { - var filePath = result[0]; - ImportStudents(filePath, userUseCase); - } - }); } - private void ImportStudents(string filePath, IUserUseCase userUseCase) + private async System.Threading.Tasks.Task ImportStudents() { - if (SelectedGroupItem == null || SelectedGroupItem.Id == null) + if (SelectedGroupItem == null || SelectedGroupItem.Id == null) return; + + var openFileDialog = new OpenFileDialog { - return; - } - var lines = File.ReadAllLines(filePath); + Filters = new List + { + new FileDialogFilter { Name = "CSV Files", Extensions = { "csv" } } + }, + AllowMultiple = false + }; - foreach (var line in lines.Skip(1)) + var result = await openFileDialog.ShowAsync(new Window()); + if (result?.Length > 0) { - var columns = line.Split(','); - if (columns.Length < 1) continue; + var filePath = result[0]; + var lines = File.ReadAllLines(filePath); - var fio = columns[0]; - - var newUser = new User + foreach (var line in lines.Skip(1)) { - FIO = fio, - Group = new Group - { - Name = SelectedGroupItem.Name, - ID = SelectedGroupItem.Id - } - }; + var columns = line.Split(','); + if (columns.Length < 1) continue; - var result = userUseCase.CreateUser(newUser); - - if (result) - { - var userPresenter = new UserPresenter + var fio = columns[0]; + var newUser = new User { - Name = fio, - Guid = Guid.NewGuid(), - Group = SelectedGroupItem + FIO = fio, + Group = new Group + { + Name = SelectedGroupItem.Name, + ID = SelectedGroupItem.Id + } }; - _users.Add(userPresenter); + + var isCreated = _userUseCase.CreateUser(newUser); + if (isCreated) + { + RefreshGroups(); + SetUsers(); + } } } } - private void DeleteUsersByGroupID(IUserUseCase userUseCase) { + private void DeleteUsersByGroupID() { if (SelectedGroupItem == null) return; var groupID = SelectedGroupItem.Id; - bool res = userUseCase.RemoveUsersByGroupID(groupID); + bool res = _userUseCase.RemoveUsersByGroupID(groupID); if (res) { Users.Clear(); @@ -133,12 +176,58 @@ namespace Presence.Desktop.ViewModels private void SetUsers() { - if(SelectedGroupItem == null) return; - if (SelectedGroupItem.users == null) return; + if (SelectedGroupItem == null) return; + Users.Clear(); - foreach (var item in SelectedGroupItem.users) + + var group = _groups.FirstOrDefault(it => it.Id == SelectedGroupItem.Id); + if (group?.users == null) return; + + var sortedUsers = SelectedSecondItem switch { - Users.Add(item); + "name sort" => group.users.OrderBy(user => user.Name), + "name sort rev" => group.users.OrderByDescending(user => user.Name), + _ => group.users + }; + + foreach (var user in sortedUsers) + { + Users.Add(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) + { + 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); + + if (isUpdated != null) + { + NewFIO = string.Empty; + RefreshGroups(); + SetUsers(); } } diff --git a/Presence.Desktop/Views/GroupView.axaml b/Presence.Desktop/Views/GroupView.axaml index 26c48b3..7329c2d 100644 --- a/Presence.Desktop/Views/GroupView.axaml +++ b/Presence.Desktop/Views/GroupView.axaml @@ -27,12 +27,19 @@ + + + + + + +