AgentsViewModel.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Data;
  8. using System.Windows.Input;
  9. using WPF.Framework.Commands.Base;
  10. using WPF.Framework.Models;
  11. using WPF.Framework.Models.Repository;
  12. using WPF.Framework.Models.Repository.Base;
  13. namespace WPF.Framework.ViewModels
  14. {
  15. class AgentsViewModel : ViewModel
  16. {
  17. public AgentsViewModel()
  18. {
  19. agentRepository = new AgentRepository(new AgentsDbContext());
  20. agentTypeRepository = new Repository<AgentType>(new AgentsDbContext());
  21. agentList = agentRepository.Items.ToList();
  22. agentsCollectionViewSource = new CollectionViewSource()
  23. {
  24. Source = agentList
  25. };
  26. SortingDirectionsDictionary = new Dictionary<ListSortDirection, string>()
  27. {
  28. { ListSortDirection.Ascending, "По возрастанию" },
  29. { ListSortDirection.Descending, "По убыванию" }
  30. };
  31. AgentTypesFilterDictionary = InitializeAgentTypesFilterDictionary();
  32. AgentsSortingDictionary = InitializeAgentsSortingDictionary();
  33. CurrentListSortDirection = ListSortDirection.Ascending;
  34. CurrentSortingAgentProperty = AgentsSortingDictionary.First().Key;
  35. CurrentAgentTypeFilter = AgentTypesFilterDictionary.First().Key;
  36. agentsCollectionViewSource.Filter += OnSearchFilter;
  37. agentsCollectionViewSource.Filter += OnAgentTypesFilter;
  38. ListDirectionCommand = new LambdaCommad(OnListDirectionCommandExecuted);
  39. }
  40. private IRepository<Agent> agentRepository;
  41. private IRepository<AgentType> agentTypeRepository;
  42. private List<Agent> agentList;
  43. private CollectionViewSource agentsCollectionViewSource;
  44. private string searchText = "";
  45. public string SearchText
  46. {
  47. get => searchText;
  48. set => Set(ref searchText, ref value);
  49. }
  50. private ListSortDirection currentListSortDirection;
  51. public ListSortDirection CurrentListSortDirection
  52. {
  53. get => currentListSortDirection;
  54. set
  55. {
  56. if (Set(ref currentListSortDirection, ref value))
  57. {
  58. agentsCollectionViewSource.SortDescriptions.Clear();
  59. agentsCollectionViewSource.SortDescriptions.Add(new SortDescription(CurrentSortingAgentProperty, value));
  60. AgentsCollectionView.Refresh();
  61. }
  62. }
  63. }
  64. private AgentType currentAgentTypeFilter;
  65. public AgentType CurrentAgentTypeFilter
  66. {
  67. get => currentAgentTypeFilter;
  68. set
  69. {
  70. if (Set(ref currentAgentTypeFilter, ref value))
  71. {
  72. AgentsCollectionView.Refresh();
  73. }
  74. }
  75. }
  76. private string currentSortingAgentProperty;
  77. public string CurrentSortingAgentProperty
  78. {
  79. get => currentSortingAgentProperty;
  80. set
  81. {
  82. if (Set(ref currentSortingAgentProperty, ref value))
  83. {
  84. agentsCollectionViewSource.SortDescriptions.Clear();
  85. agentsCollectionViewSource.SortDescriptions.Add(new SortDescription(value, CurrentListSortDirection));
  86. AgentsCollectionView.Refresh();
  87. }
  88. }
  89. }
  90. public Dictionary<ListSortDirection, string> SortingDirectionsDictionary { get; }
  91. public Dictionary<AgentType, string> AgentTypesFilterDictionary { get; }
  92. public Dictionary<string, string> AgentsSortingDictionary { get; }
  93. public ICollectionView AgentsCollectionView => agentsCollectionViewSource.View;
  94. private void OnAgentTypesFilter(object sender, FilterEventArgs e)
  95. {
  96. if (e.Item == null)
  97. {
  98. return;
  99. }
  100. Agent agent = (Agent)e.Item;
  101. if (agent.AgentType.ID != CurrentAgentTypeFilter.ID)
  102. {
  103. e.Accepted = false;
  104. }
  105. }
  106. private void OnSearchFilter(object sender, FilterEventArgs e)
  107. {
  108. if(e.Item == null)
  109. {
  110. return;
  111. }
  112. Agent agent = (Agent)e.Item;
  113. if(!agent.Title.Contains(SearchText) || !agent.Email.Contains(SearchText) || !agent.Phone.Contains(SearchText))
  114. {
  115. e.Accepted = false;
  116. }
  117. }
  118. public ICommand ListDirectionCommand { get; }
  119. private void OnListDirectionCommandExecuted(object obj)
  120. {
  121. if (CurrentListSortDirection == ListSortDirection.Descending)
  122. {
  123. CurrentListSortDirection = ListSortDirection.Ascending;
  124. }
  125. else
  126. {
  127. CurrentListSortDirection = ListSortDirection.Descending;
  128. }
  129. }
  130. private Dictionary<AgentType, string> InitializeAgentTypesFilterDictionary()
  131. {
  132. Dictionary<AgentType, string> dictionaty = new Dictionary<AgentType, string>();
  133. foreach (var item in agentTypeRepository.Items)
  134. {
  135. dictionaty.Add(item, item.Title);
  136. }
  137. return dictionaty;
  138. }
  139. private Dictionary<string, string> InitializeAgentsSortingDictionary()
  140. {
  141. Dictionary<string, string> dictionaty = new Dictionary<string, string>()
  142. {
  143. { nameof(Agent.Title), "Название" },
  144. { nameof(Agent.Discount), "Размер скидки" },
  145. { nameof(Agent.Priority), "Приоритет" },
  146. };
  147. return dictionaty;
  148. }
  149. }
  150. }