AdminPageViewModel.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reactive.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Avalonia.Media.Imaging;
  9. using Avalonia.Media.TextFormatting;
  10. using AvaloniaApplicationTestNew.Models;
  11. using AvaloniaApplicationTestNew.Views;
  12. using Microsoft.EntityFrameworkCore;
  13. using ReactiveUI;
  14. using Tmds.DBus.Protocol;
  15. namespace AvaloniaApplicationTestNew.ViewModels
  16. {
  17. public class AdminPageViewModel : ViewModelBase
  18. {
  19. List<User> users;
  20. List<User> users0;
  21. List<Localuser> localusers;
  22. List<Gender> genders;
  23. int idUser;
  24. string findText;
  25. Gender filtGendr;
  26. List<Bitmap> imageUser;
  27. Dictionary<User, Bitmap> ConvertImage = new Dictionary<User, Bitmap>();
  28. List<Dictionary<User, Bitmap>> listDictionaries = new List<Dictionary<User, Bitmap>>();
  29. List<Localuser> localUser;
  30. public List<User> Users { get => users; set => this.RaiseAndSetIfChanged(ref users, value); }
  31. public List<User> Users0 { get => users0; set => this.RaiseAndSetIfChanged(ref users0, value); }
  32. public List<Localuser> LocalListusers { get => localusers; set => this.RaiseAndSetIfChanged(ref localusers, value); }
  33. public List<Gender> Genders { get => genders; set => this.RaiseAndSetIfChanged(ref genders, value); }
  34. public string FindText { get => findText; set => this.RaiseAndSetIfChanged(ref findText, value); }
  35. public Gender FiltGendr { get => filtGendr; set => this.RaiseAndSetIfChanged(ref filtGendr, value); }
  36. public int IdUser { get => idUser; set => this.RaiseAndSetIfChanged(ref idUser, value); }
  37. public List<Bitmap> ImageUser { get => imageUser; set => this.RaiseAndSetIfChanged(ref imageUser, value); }
  38. public List<Dictionary<User, Bitmap>> ListDictionaries { get => listDictionaries; set => this.RaiseAndSetIfChanged(ref listDictionaries, value); }
  39. public AdminPageViewModel()
  40. {
  41. Users = myContext.Users.Include(x => x.IdUserNavigation.IdRoleNavigation).Include(x => x.IdGenderNavigation).Include(x => x.IdTraits).ToList();
  42. int count = 0;
  43. LocalListusers = RemakeList(users);
  44. localUser = LocalListusers;
  45. genders = new List<Gender>
  46. {
  47. new Gender() {Gender1 = "Сбросить фильтр", IdGender = 0}
  48. }.Concat(myContext.Genders).ToList(); FiltGendr = genders[0];
  49. users0 = users;
  50. this.WhenAnyValue(x => x.FindText, x => x.FiltGendr).Subscribe(_ => AllFilters());
  51. }
  52. public List<Localuser> RemakeList(List<User> users)
  53. {
  54. List<Localuser> LocalListusers = new List<Localuser>();
  55. foreach (var user in users)
  56. {
  57. Localuser temp =
  58. new Localuser
  59. {
  60. Name = user.Name,
  61. IdGender = user.IdGender,
  62. IdGenderNavigation = user.IdGenderNavigation,
  63. IdUser = user.IdUser,
  64. IdUserNavigation = user.IdUserNavigation,
  65. BirthDate = user.BirthDate,
  66. IdTraits = user.IdTraits,
  67. ConvertImage = user.Image != null ? new Bitmap(new MemoryStream(user.Image)) : new Bitmap("Assets\\ava.png")
  68. };
  69. LocalListusers.Add(temp);
  70. }
  71. return LocalListusers;
  72. }
  73. public void OnTextFindChanged(string text) => AllFilters();
  74. public void SortDate(int id)
  75. {
  76. switch (id)
  77. {
  78. case 1:
  79. LocalListusers = LocalListusers.OrderBy(x => x.BirthDate).ToList();
  80. break;
  81. case 2:
  82. LocalListusers = LocalListusers.OrderByDescending(x => x.BirthDate).ToList();
  83. break;
  84. }
  85. }
  86. public void OnFilterGenderChanged(Gender meaning) => AllFilters();
  87. void AllFilters()
  88. {
  89. LocalListusers = localUser;
  90. if (!string.IsNullOrEmpty(findText)) LocalListusers = LocalListusers.Where(x => x.Name.Contains(findText)).ToList();
  91. if (filtGendr != null && filtGendr.IdGender != 0) LocalListusers = LocalListusers.Where(x => x.IdGenderNavigation == filtGendr).ToList();
  92. }
  93. public void InfoUsers(int id)
  94. {
  95. IdUser = id;
  96. MainWindowViewModel.Self.UserPageVM = new UserPageViewModel(id);
  97. MainWindowViewModel.Self.Page = new UserPage();
  98. }
  99. public void AddUser()
  100. {
  101. MainWindowViewModel.Self.AddUserPageVM = new AddUserPageViewModel();
  102. MainWindowViewModel.Self.Page = new AddUserPage();
  103. }
  104. public void Exit()
  105. {
  106. MainWindowViewModel.Self.AuthorizationVM = new AuthorizationViewModel();
  107. MainWindowViewModel.Self.Page = new Authorization();
  108. }
  109. }
  110. }