DataOutputViewModel.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using Avalonia.Media;
  6. using LoginGenerator.Models;
  7. using ReactiveUI;
  8. namespace LoginGenerator.ViewModels
  9. {
  10. public class DataOutputViewModel : ReactiveObject
  11. {
  12. public string[] AllData;
  13. private List<Users>? users;
  14. public List<Users>? Userss { get => users; set => this.RaiseAndSetIfChanged(ref users, value); }
  15. public string[] OneUser;
  16. public Users u;
  17. public DataOutputViewModel()
  18. {
  19. Userss = new List<Users>();
  20. FillingUsersList();
  21. Sort = new List<string>() { "Äàòà ïî óáûâàíèþ", "Äàòà ïî âîçðàñòàíèþ" };
  22. SelectedSort = Sort[0];
  23. }
  24. private SolidColorBrush background2 = new SolidColorBrush(Colors.White);
  25. public SolidColorBrush Background2
  26. {
  27. get => background2;
  28. set => this.RaiseAndSetIfChanged(ref background2, value);
  29. }
  30. private SolidColorBrush background = new SolidColorBrush(Colors.White);
  31. public SolidColorBrush Background
  32. {
  33. get => background;
  34. set => this.RaiseAndSetIfChanged(ref background, value);
  35. }
  36. public void FillingUsersList()
  37. {
  38. if (!File.Exists(FilePath.PATH))
  39. {
  40. var file = File.Create(FilePath.PATH);
  41. file.Close();
  42. }
  43. AllData = File.ReadAllLines(FilePath.PATH);
  44. List<Users>? newUsers = new List<Users>();
  45. foreach (string user in AllData)
  46. {
  47. if (!string.IsNullOrEmpty(user))
  48. {
  49. u = new Users();
  50. OneUser = user.Split(' ');
  51. u.Surname = OneUser[0].ToString();
  52. u.Name = OneUser[1].ToString();
  53. u.Patronymic = OneUser[2].ToString();
  54. u.Login = OneUser[3].ToString();
  55. u.Password = OneUser[4].ToString();
  56. u.DateReg = Convert.ToDateTime(OneUser[5]);
  57. newUsers.Add(u);
  58. }
  59. }
  60. Userss = newUsers;
  61. }
  62. private string? _findPass;
  63. public string? FindPass
  64. {
  65. get => _findPass;
  66. set
  67. {
  68. _findPass = value;
  69. Filter();
  70. }
  71. }
  72. private string? _findLogin;
  73. public string? FindLogin
  74. {
  75. get => _findLogin;
  76. set
  77. {
  78. _findLogin = value;
  79. Filter();
  80. }
  81. }
  82. private string? _findFio;
  83. public string? FindFio
  84. {
  85. get => _findFio;
  86. set
  87. {
  88. _findFio = value;
  89. Filter();
  90. }
  91. }
  92. private static List<string>? _sort;
  93. public static List<string>? Sort { get => _sort; set => _sort = value; }
  94. private string? _selectedSort;
  95. public string? SelectedSort
  96. {
  97. get
  98. {
  99. return _selectedSort;
  100. }
  101. set
  102. {
  103. _selectedSort = value;
  104. Filter();
  105. }
  106. }
  107. public void Filter()
  108. {
  109. FillingUsersList();
  110. switch (SelectedSort)
  111. {
  112. case "Äàòà ïî óáûâàíèþ":
  113. Userss = Userss.OrderByDescending(x => x.DateReg).ToList();
  114. break;
  115. case "Äàòà ïî âîçðàñòàíèþ":
  116. Userss = Userss.OrderBy(x => x.DateReg).ToList();
  117. break;
  118. }
  119. if (FindFio != null && FindFio != "")
  120. {
  121. Userss = Userss.Where(x => x.Name.ToUpper().Contains(FindFio.ToString().ToUpper()) || x.Surname.ToUpper().Contains(FindFio.ToString().ToUpper()) ||
  122. x.Patronymic.ToUpper().Contains(FindFio.ToString().ToUpper())).ToList();
  123. }
  124. if (FindLogin != null && FindLogin != "")
  125. {
  126. Userss = Userss.Where(x => x.Login.Contains(FindLogin.ToString())).ToList();
  127. }
  128. if (FindPass != null && FindPass != "")
  129. {
  130. Userss = Userss.Where(x => x.Password.Contains(FindPass.ToString())).ToList();
  131. }
  132. }
  133. }
  134. }