InfoUsersDateViewModel.cs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using Avalonia.Media.Imaging;
  2. using CommunityToolkit.Mvvm.ComponentModel;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using TestRepeat.Models;
  8. using TestRepeat.Views;
  9. namespace TestRepeat.ViewModels
  10. {
  11. public partial class InfoUsersDateViewModel : ViewModelBase
  12. {
  13. [ObservableProperty] List<UserCustom> listUsers;
  14. [ObservableProperty] List<Gender> listGenders;
  15. [ObservableProperty] string textFolderContent;
  16. [ObservableProperty] Gender selectedGender;
  17. List<UserCustom> copyUsers;
  18. public InfoUsersDateViewModel() { }
  19. public InfoUsersDateViewModel(List<User> users)
  20. {
  21. listUsers = ConvertList(users);
  22. copyUsers = listUsers;
  23. listGenders = [
  24. new Gender() { Gender1 = "Ñáðîñèòü ôèëüòð", IdGender = 0 },
  25. .. users.Select(gender => gender.IdGenderNavigation).Distinct().OrderBy(gender => gender.IdGender).ToList()
  26. ];
  27. }
  28. public void DateSort(int idParam)
  29. {
  30. switch (idParam)
  31. {
  32. case 1:
  33. ListUsers = ListUsers.OrderByDescending(x => x.BirthDate).ToList();
  34. break;
  35. case 2:
  36. ListUsers = ListUsers.OrderBy(x => x.BirthDate).ToList();
  37. break;
  38. }
  39. }
  40. private List<UserCustom> ConvertList(List<User> users)
  41. {
  42. List<UserCustom> listUsers = new List<UserCustom>();
  43. foreach (var user in users)
  44. {
  45. UserCustom tempUser =
  46. new UserCustom
  47. {
  48. Name = user.Name,
  49. IdGender = user.IdGender,
  50. IdGenderNavigation = user.IdGenderNavigation,
  51. IdUser = user.IdUser,
  52. IdUserNavigation = user.IdUserNavigation,
  53. BirthDate = user.BirthDate,
  54. ConvertImgUser = new Bitmap(new MemoryStream(user.ImgUser)),
  55. IdThreats = user.IdThreats
  56. };
  57. if (user.IdUser == InfoUsersDate.CurrentUser.IdUser)
  58. {
  59. tempUser.IsCanseled = false;
  60. }
  61. listUsers.Add(tempUser);
  62. }
  63. return listUsers;
  64. }
  65. partial void OnSelectedGenderChanged(Gender value) => Filters();
  66. partial void OnTextFolderContentChanged(string value) => Filters();
  67. void Filters()
  68. {
  69. ListUsers = copyUsers;
  70. if (!string.IsNullOrEmpty(textFolderContent)) ListUsers = ListUsers.Where(user => user.Name.Contains(textFolderContent)).ToList();
  71. if (selectedGender != null && selectedGender.IdGender != 0) ListUsers = ListUsers.Where(user => user.IdGenderNavigation == selectedGender).ToList();
  72. }
  73. public void ChangeUserDate(int idUser)
  74. {
  75. User changeableUser = MainWindowViewModel.Db_context.Users.FirstOrDefault(user => user.IdUser == idUser);
  76. MainWindowViewModel.Instance.Uc = new DateUser(changeableUser);
  77. }
  78. public void DeleteUser(int idUser)
  79. {
  80. MainWindowViewModel.Db_context
  81. .Remove(
  82. MainWindowViewModel.Db_context.Users.FirstOrDefault(u => u.IdUser == idUser));
  83. MainWindowViewModel.Db_context
  84. .Remove(
  85. MainWindowViewModel.Db_context.Logineds.FirstOrDefault(l => l.Id == idUser));
  86. MainWindowViewModel.Db_context.SaveChanges();
  87. MainWindowViewModel.Instance.Uc = new InfoUsersDate(MainWindowViewModel.Db_context.Users.ToList());
  88. }
  89. public void AddNewUser()
  90. {
  91. MainWindowViewModel.Instance.Uc = new NewUser();
  92. }
  93. }
  94. }