RuleListViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. namespace GDNXFD.Alert.Config.ViewModel
  2. {
  3. using GalaSoft.MvvmLight.Command;
  4. using GalaSoft.MvvmLight.Messaging;
  5. using GDNXFD.Alert.Config.Model;
  6. using GDNXFD.Alert.Config.Services.Navigation;
  7. using GDNXFD.Alert.Config.ViewModel.Base;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Input;
  15. using System.Linq;
  16. using GDNXFD.Alert.Config.Resources.Strings;
  17. using Data;
  18. using Data.Repositories;
  19. /// <summary>
  20. /// Travel list ViewModel.
  21. /// </summary>
  22. public class RuleListViewModel : VMBase
  23. {
  24. private const int PAGESIZE = 25;
  25. private RelayCommand<AlertRule> editRuleCommand;
  26. private RelayCommand addRuleCommand;
  27. private RelayCommand refreshRuleListCommand;
  28. private RelayCommand<string> removeRuleCommand;
  29. private RelayCommand<AlertRule> viewRuleCommand;
  30. private RelayCommand nextPageCommand;
  31. private RelayCommand previousPageCommand;
  32. private ObservableCollection<AlertRule> ruleList;
  33. private ObservableCollection<PageItem> pagesList;
  34. private string filter;
  35. //private CancellationTokenSource searchCancellationTokenSource;
  36. private readonly INavigationService navService;
  37. private PaginationConfig pagConfig;
  38. private int rulesCount;
  39. #region 初始化
  40. public RuleListViewModel(INavigationService navService)
  41. {
  42. this.navService = navService;
  43. SubscribeCommands();
  44. //InitializeMockedData();
  45. }
  46. public void InitializeData()
  47. {
  48. try
  49. {
  50. Messenger.Default.Send(new LoadingMessage(true));
  51. RulesCount = AlertRuleRepository.GetAlertRuleTotals(filter);
  52. this.pagConfig = new PaginationConfig(this.rulesCount, PAGESIZE);
  53. NavigateToPage(this.pagConfig.NumberOfPageSelected);
  54. Messenger.Default.Send(new UserLoginMessage(true));
  55. }
  56. catch (Exception ex)
  57. {
  58. CustomDialogMessage message = new CustomDialogMessage(() => { }, StringResources.UnexpectedError, Visibility.Collapsed);
  59. Messenger.Default.Send<CustomDialogMessage>(message);
  60. }
  61. }
  62. private void InitializeMockedData()
  63. {
  64. var alertRules = AlertRuleRepository.GetAlertRules();
  65. //var travels = this.sampleDataService.GetTravelRequests();
  66. RuleList = new ObservableCollection<AlertRule>(alertRules);
  67. var pages = GetPagesList();
  68. PagesList = new ObservableCollection<PageItem>(pages);
  69. }
  70. private void SubscribeCommands()
  71. {
  72. this.editRuleCommand = new RelayCommand<AlertRule>(EditRuleCommandExecute);
  73. this.addRuleCommand = new RelayCommand(AddRuleCommandExecute);
  74. this.viewRuleCommand = new RelayCommand<AlertRule>(ViewRuleCommandExecute);
  75. this.nextPageCommand = new RelayCommand(NextPageCommandExecute);
  76. this.previousPageCommand = new RelayCommand(PreviousPageCommandExecute);
  77. this.refreshRuleListCommand = new RelayCommand(RefreshRuleListCommandExecute);
  78. this.removeRuleCommand = new RelayCommand<string>(RemoveRuleCommandExecute);
  79. }
  80. #endregion
  81. #region 表头及操作
  82. public string Filter
  83. {
  84. get { return this.filter; }
  85. set
  86. {
  87. this.filter = value;
  88. SearchTravels();
  89. RaisePropertyChanged(() => Filter);
  90. }
  91. }
  92. public int RulesCount
  93. {
  94. get { return this.rulesCount; }
  95. set
  96. {
  97. this.rulesCount = value;
  98. RaisePropertyChanged(() => RulesCount);
  99. }
  100. }
  101. public ICommand AddRuleCommand
  102. {
  103. get { return addRuleCommand; }
  104. }
  105. public ICommand RefreshRuleListCommand
  106. {
  107. get { return refreshRuleListCommand; }
  108. }
  109. private void SearchTravels()
  110. {
  111. InitializeData();
  112. }
  113. private void AddRuleCommandExecute()
  114. {
  115. AlertRule ar = new AlertRule();
  116. ar.Category = "1";
  117. ar.Rank = "3";
  118. navService.NavigateToRuleForm(ar, FormMode.Create);
  119. }
  120. private void RefreshRuleListCommandExecute()
  121. {
  122. InitializeData();
  123. }
  124. #endregion
  125. #region 规则列表及操作
  126. public ObservableCollection<AlertRule> RuleList
  127. {
  128. get
  129. {
  130. return this.ruleList;
  131. }
  132. set
  133. {
  134. this.ruleList = value;
  135. base.RaisePropertyChanged(() => RuleList);
  136. }
  137. }
  138. public ICommand EditRuleCommand
  139. {
  140. get { return editRuleCommand; }
  141. }
  142. public ICommand ViewRuleCommand
  143. {
  144. get { return this.viewRuleCommand; }
  145. }
  146. public ICommand RemoveRuleCommand
  147. {
  148. get { return this.removeRuleCommand; }
  149. }
  150. private void EditRuleCommandExecute(AlertRule rule)
  151. {
  152. navService.NavigateToRuleForm(rule, FormMode.Update);
  153. }
  154. private void RemoveRuleCommandExecute(string id)
  155. {
  156. // Get the travel request to remove.
  157. AlertRule rule = this.ruleList.FirstOrDefault(t => t.Id == id);
  158. CustomDialogMessage message = new CustomDialogMessage( () =>
  159. {
  160. AlertRuleRepository.DeleteAlertRule(id);
  161. RemoveFromList(rule);
  162. }, String.Format(Resources.Strings.StringResources.ConfirmDeleteMessage, rule.Name), Visibility.Visible);
  163. Messenger.Default.Send<CustomDialogMessage>(message);
  164. }
  165. private void RemoveFromList(AlertRule rule)
  166. {
  167. this.pagConfig.ItemsCounted--;
  168. RulesCount--;
  169. this.ruleList.Remove(rule);
  170. if (this.ruleList.Count != 0)
  171. {
  172. RaisePropertyChanged(() => RuleList);
  173. }
  174. else
  175. {
  176. if (this.pagConfig.CanGoNext)
  177. {
  178. NavigateToPage(this.pagConfig.NumberOfPageSelected);
  179. }
  180. else if (this.pagConfig.CanGoPrevious)
  181. {
  182. this.pagConfig.NumberOfPageSelected--;
  183. NavigateToPage(this.pagConfig.NumberOfPageSelected);
  184. }
  185. }
  186. }
  187. private void ViewRuleCommandExecute(AlertRule rule)
  188. {
  189. navService.NavigateToRuleForm(rule, FormMode.ReadOnly);
  190. }
  191. #endregion
  192. #region 分页操作
  193. public ObservableCollection<PageItem> PagesList
  194. {
  195. get { return this.pagesList; }
  196. set
  197. {
  198. this.pagesList = value;
  199. RaisePropertyChanged(() => PagesList);
  200. }
  201. }
  202. public ICommand NextPageCommand
  203. {
  204. get { return this.nextPageCommand; }
  205. }
  206. public ICommand PreviousPageCommand
  207. {
  208. get { return this.previousPageCommand; }
  209. }
  210. public List<PageItem> GetPagesList()
  211. {
  212. var pagesList = new List<PageItem>();
  213. pagesList.Add(new PageItem((1).ToString(), true, true));
  214. for (int i = 2; i <= 5; i++)
  215. {
  216. pagesList.Add(new PageItem((i).ToString(), false, true));
  217. }
  218. return pagesList;
  219. }
  220. private List<PageItem> CalculatePaginator()
  221. {
  222. var pages = new List<PageItem>();
  223. int firstPage = (int)Math.Floor((double)this.pagConfig.NumberOfPageSelected / 12) * 12;
  224. if (firstPage > 0)
  225. firstPage = firstPage - 2;
  226. for (int i = firstPage; i < (firstPage + Math.Min(12, this.pagConfig.NumberOfPages)) && (i < this.pagConfig.NumberOfPages); i++)
  227. {
  228. var isSelected = false;
  229. if (this.pagConfig.NumberOfPageSelected == i + 1)
  230. isSelected = true;
  231. PageItem page = new PageItem((i + 1).ToString(), isSelected, true);
  232. page.PageSelected += PageSelectedChanged;
  233. pages.Add(page);
  234. }
  235. if (this.pagConfig.NumberOfPages > this.pagConfig.PageSize && int.Parse(pages[pages.Count - 1].Page) != this.pagConfig.NumberOfPages)
  236. {
  237. pages.Add(new PageItem("...", false, false));
  238. var lastPage = new PageItem(this.pagConfig.NumberOfPages.ToString(), false, true);
  239. pages.Add(lastPage);
  240. lastPage.PageSelected += PageSelectedChanged;
  241. }
  242. return pages;
  243. }
  244. private void PageSelectedChanged(object sender, EventArgs args)
  245. {
  246. this.pagConfig.PageSelected = (PageItem)sender;
  247. NavigateToPage(this.pagConfig.NumberOfPageSelected);
  248. }
  249. private void ResetPaginator()
  250. {
  251. if (this.pagesList != null)
  252. {
  253. foreach (var page in this.pagesList)
  254. {
  255. page.PageSelected -= PageSelectedChanged;
  256. }
  257. }
  258. }
  259. private void NextPageCommandExecute()
  260. {
  261. if (this.pagConfig.CanGoNext)
  262. {
  263. this.pagConfig.NumberOfPageSelected = this.pagConfig.NumberOfPageSelected + 1;
  264. NavigateToPage(this.pagConfig.NumberOfPageSelected);
  265. }
  266. }
  267. private void PreviousPageCommandExecute()
  268. {
  269. if (this.pagConfig.CanGoPrevious)
  270. {
  271. this.pagConfig.NumberOfPageSelected = this.pagConfig.NumberOfPageSelected - 1;
  272. NavigateToPage(this.pagConfig.NumberOfPageSelected);
  273. }
  274. }
  275. private void NavigateToPage(int pageToNavigate)
  276. {
  277. try
  278. {
  279. Messenger.Default.Send(new LoadingMessage(true));
  280. ResetPaginator();
  281. var travels = AlertRuleRepository.GetAllAlertRules(filter, this.pagConfig.PageSize, pageToNavigate - 1);
  282. RuleList = new ObservableCollection<AlertRule>(travels);
  283. PagesList = new ObservableCollection<PageItem>(CalculatePaginator());
  284. }
  285. catch (Exception ex)
  286. {
  287. CustomDialogMessage message = new CustomDialogMessage(() => { }, StringResources.UnexpectedError, Visibility.Collapsed);
  288. Messenger.Default.Send<CustomDialogMessage>(message);
  289. }
  290. finally
  291. {
  292. Messenger.Default.Send(new LoadingMessage(false));
  293. }
  294. }
  295. #endregion
  296. }
  297. }