DataOutputViewModel.cs 3.8 KB

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