CourcesListViewModel.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. using Microsoft.EntityFrameworkCore;
  7. using ReactiveUI;
  8. namespace prakt1314.ViewModels
  9. {
  10. public class CourcesListViewModel : ReactiveObject
  11. {
  12. public CourcesListViewModel()
  13. {
  14. using (AvaloniaPrakt13DbContext _db = new AvaloniaPrakt13DbContext())
  15. {
  16. _courcesBeforeFFS = _db.Cources.Include(a => a.TeacherCources).ToList();
  17. }
  18. FFS();
  19. }
  20. public ObservableCollection<Cource> Cources
  21. {
  22. get => _courcesAfterFFS;
  23. set => this.RaiseAndSetIfChanged(ref _courcesAfterFFS, value);
  24. }
  25. public int SelectedFilterIndex
  26. {
  27. get => _selectedFilterIndex;
  28. set
  29. {
  30. this.RaiseAndSetIfChanged(ref _selectedFilterIndex, value);
  31. FFS();
  32. }
  33. }
  34. public int SelectedSortIndex
  35. {
  36. get => _selectedSortIndex;
  37. set
  38. {
  39. this.RaiseAndSetIfChanged(ref _selectedSortIndex, value);
  40. FFS();
  41. }
  42. }
  43. public string SearchQuarry
  44. {
  45. get => _searchQuarry;
  46. set
  47. {
  48. this.RaiseAndSetIfChanged(ref _searchQuarry, value);
  49. FFS();
  50. }
  51. }
  52. public bool IsSearchByName
  53. {
  54. get => _isSearchByName;
  55. set
  56. {
  57. this.RaiseAndSetIfChanged(ref _isSearchByName, value);
  58. FFS();
  59. }
  60. }
  61. public bool IsSortingABS
  62. {
  63. get => _isSortingABS;
  64. set
  65. {
  66. this.RaiseAndSetIfChanged(ref _isSortingABS, value);
  67. FFS();
  68. }
  69. }
  70. private ObservableCollection<Cource> _courcesAfterFFS = new ObservableCollection<Cource>();
  71. private List<Cource> _courcesBeforeFFS = new List<Cource>();
  72. private int _selectedFilterIndex = 0;
  73. private int _selectedSortIndex = 0;
  74. private string _searchQuarry = "";
  75. private bool _isSearchByName = true;
  76. private bool _isSortingABS = true;
  77. private void FFS()
  78. {
  79. Cources.Clear();
  80. List<Cource> FilteredCource = new List<Cource>();
  81. switch (SelectedFilterIndex)
  82. {
  83. case 0:
  84. {
  85. foreach (Cource c in _courcesBeforeFFS)
  86. {
  87. FilteredCource.Add(c);
  88. }
  89. break;
  90. }
  91. case 1:
  92. {
  93. foreach (Cource c in _courcesBeforeFFS)
  94. {
  95. if (c.HoursNumber >= 0 && c.HoursNumber < 30)
  96. {
  97. FilteredCource.Add(c);
  98. }
  99. }
  100. break;
  101. }
  102. case 2:
  103. {
  104. foreach (Cource c in _courcesBeforeFFS)
  105. {
  106. if (c.HoursNumber >= 30 && c.HoursNumber < 60)
  107. {
  108. FilteredCource.Add(c);
  109. }
  110. }
  111. break;
  112. }
  113. case 3:
  114. {
  115. foreach (Cource c in _courcesBeforeFFS)
  116. {
  117. if (c.HoursNumber >= 60 && c.HoursNumber < 120)
  118. {
  119. FilteredCource.Add(c);
  120. }
  121. }
  122. break;
  123. }
  124. case 4:
  125. {
  126. foreach (Cource c in _courcesBeforeFFS)
  127. {
  128. if (c.HoursNumber >= 120)
  129. {
  130. FilteredCource.Add(c);
  131. }
  132. }
  133. break;
  134. }
  135. }
  136. List<Cource> SearchedCources = FilteredCource;
  137. if (SearchQuarry is not null && SearchQuarry.Length != 0)
  138. {
  139. SearchedCources = new List<Cource>();
  140. switch (_isSearchByName)
  141. {
  142. case true:
  143. {
  144. foreach (Cource c in FilteredCource)
  145. {
  146. if (Regex.IsMatch(c.Cource1, SearchQuarry)) SearchedCources.Add(c);
  147. }
  148. break;
  149. }
  150. case false:
  151. {
  152. foreach (Cource c in FilteredCource)
  153. {
  154. if (Regex.IsMatch(c.HoursNumber.ToString(), SearchQuarry)) SearchedCources.Add(c);
  155. }
  156. break;
  157. }
  158. }
  159. }
  160. else;
  161. switch (IsSortingABS)
  162. {
  163. case true:
  164. switch (SelectedSortIndex)
  165. {
  166. case 0:
  167. Cources = new ObservableCollection<Cource>(SearchedCources.OrderBy((a) => a.Cource1));
  168. return;
  169. case 1:
  170. Cources = new ObservableCollection<Cource>(SearchedCources.OrderBy((a) => a.HoursNumber));
  171. return;
  172. }
  173. return;
  174. case false:
  175. switch (SelectedSortIndex)
  176. {
  177. case 0:
  178. Cources = new ObservableCollection<Cource>(SearchedCources.OrderByDescending((a) => a.Cource1));
  179. return;
  180. case 1:
  181. Cources = new ObservableCollection<Cource>(SearchedCources.OrderByDescending((a) => a.HoursNumber));
  182. return;
  183. }
  184. return;
  185. }
  186. }
  187. }
  188. }