123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- namespace GDNXFD.Alert.Config.ViewModel
- {
- using GalaSoft.MvvmLight.Command;
- using GalaSoft.MvvmLight.Messaging;
- using GDNXFD.Alert.Config.Model;
- using GDNXFD.Alert.Config.Services.Navigation;
- using GDNXFD.Alert.Config.ViewModel.Base;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Input;
- using System.Linq;
- using GDNXFD.Alert.Config.Resources.Strings;
- using Data;
- using Data.Repositories;
- /// <summary>
- /// Travel list ViewModel.
- /// </summary>
- public class RuleListViewModel : VMBase
- {
- private const int PAGESIZE = 25;
- private RelayCommand<AlertRule> editRuleCommand;
- private RelayCommand addRuleCommand;
- private RelayCommand refreshRuleListCommand;
- private RelayCommand<string> removeRuleCommand;
- private RelayCommand<AlertRule> viewRuleCommand;
- private RelayCommand nextPageCommand;
- private RelayCommand previousPageCommand;
- private ObservableCollection<AlertRule> ruleList;
- private ObservableCollection<PageItem> pagesList;
- private string filter;
- //private CancellationTokenSource searchCancellationTokenSource;
- private readonly INavigationService navService;
- private PaginationConfig pagConfig;
- private int rulesCount;
- #region 初始化
- public RuleListViewModel(INavigationService navService)
- {
- this.navService = navService;
- SubscribeCommands();
- //InitializeMockedData();
- }
- public void InitializeData()
- {
- try
- {
- Messenger.Default.Send(new LoadingMessage(true));
- RulesCount = AlertRuleRepository.GetAlertRuleTotals(filter);
- this.pagConfig = new PaginationConfig(this.rulesCount, PAGESIZE);
- NavigateToPage(this.pagConfig.NumberOfPageSelected);
- Messenger.Default.Send(new UserLoginMessage(true));
- }
- catch (Exception ex)
- {
- CustomDialogMessage message = new CustomDialogMessage(() => { }, StringResources.UnexpectedError, Visibility.Collapsed);
- Messenger.Default.Send<CustomDialogMessage>(message);
- }
- }
- private void InitializeMockedData()
- {
- var alertRules = AlertRuleRepository.GetAlertRules();
- //var travels = this.sampleDataService.GetTravelRequests();
- RuleList = new ObservableCollection<AlertRule>(alertRules);
- var pages = GetPagesList();
- PagesList = new ObservableCollection<PageItem>(pages);
- }
- private void SubscribeCommands()
- {
- this.editRuleCommand = new RelayCommand<AlertRule>(EditRuleCommandExecute);
- this.addRuleCommand = new RelayCommand(AddRuleCommandExecute);
- this.viewRuleCommand = new RelayCommand<AlertRule>(ViewRuleCommandExecute);
- this.nextPageCommand = new RelayCommand(NextPageCommandExecute);
- this.previousPageCommand = new RelayCommand(PreviousPageCommandExecute);
- this.refreshRuleListCommand = new RelayCommand(RefreshRuleListCommandExecute);
- this.removeRuleCommand = new RelayCommand<string>(RemoveRuleCommandExecute);
- }
- #endregion
- #region 表头及操作
- public string Filter
- {
- get { return this.filter; }
- set
- {
- this.filter = value;
- SearchTravels();
- RaisePropertyChanged(() => Filter);
- }
- }
- public int RulesCount
- {
- get { return this.rulesCount; }
- set
- {
- this.rulesCount = value;
- RaisePropertyChanged(() => RulesCount);
- }
- }
- public ICommand AddRuleCommand
- {
- get { return addRuleCommand; }
- }
- public ICommand RefreshRuleListCommand
- {
- get { return refreshRuleListCommand; }
- }
- private void SearchTravels()
- {
- InitializeData();
- }
- private void AddRuleCommandExecute()
- {
- AlertRule ar = new AlertRule();
- ar.Category = "1";
- ar.Rank = "3";
- navService.NavigateToRuleForm(ar, FormMode.Create);
- }
- private void RefreshRuleListCommandExecute()
- {
- InitializeData();
- }
- #endregion
- #region 规则列表及操作
- public ObservableCollection<AlertRule> RuleList
- {
- get
- {
- return this.ruleList;
- }
- set
- {
- this.ruleList = value;
- base.RaisePropertyChanged(() => RuleList);
- }
- }
- public ICommand EditRuleCommand
- {
- get { return editRuleCommand; }
- }
- public ICommand ViewRuleCommand
- {
- get { return this.viewRuleCommand; }
- }
- public ICommand RemoveRuleCommand
- {
- get { return this.removeRuleCommand; }
- }
- private void EditRuleCommandExecute(AlertRule rule)
- {
- navService.NavigateToRuleForm(rule, FormMode.Update);
- }
-
- private void RemoveRuleCommandExecute(string id)
- {
- // Get the travel request to remove.
- AlertRule rule = this.ruleList.FirstOrDefault(t => t.Id == id);
- CustomDialogMessage message = new CustomDialogMessage( () =>
- {
- AlertRuleRepository.DeleteAlertRule(id);
- RemoveFromList(rule);
- }, String.Format(Resources.Strings.StringResources.ConfirmDeleteMessage, rule.Name), Visibility.Visible);
- Messenger.Default.Send<CustomDialogMessage>(message);
- }
- private void RemoveFromList(AlertRule rule)
- {
- this.pagConfig.ItemsCounted--;
- RulesCount--;
- this.ruleList.Remove(rule);
- if (this.ruleList.Count != 0)
- {
- RaisePropertyChanged(() => RuleList);
- }
- else
- {
- if (this.pagConfig.CanGoNext)
- {
- NavigateToPage(this.pagConfig.NumberOfPageSelected);
- }
- else if (this.pagConfig.CanGoPrevious)
- {
- this.pagConfig.NumberOfPageSelected--;
- NavigateToPage(this.pagConfig.NumberOfPageSelected);
- }
- }
- }
- private void ViewRuleCommandExecute(AlertRule rule)
- {
- navService.NavigateToRuleForm(rule, FormMode.ReadOnly);
- }
- #endregion
- #region 分页操作
- public ObservableCollection<PageItem> PagesList
- {
- get { return this.pagesList; }
- set
- {
- this.pagesList = value;
- RaisePropertyChanged(() => PagesList);
- }
- }
- public ICommand NextPageCommand
- {
- get { return this.nextPageCommand; }
- }
- public ICommand PreviousPageCommand
- {
- get { return this.previousPageCommand; }
- }
- public List<PageItem> GetPagesList()
- {
- var pagesList = new List<PageItem>();
- pagesList.Add(new PageItem((1).ToString(), true, true));
- for (int i = 2; i <= 5; i++)
- {
- pagesList.Add(new PageItem((i).ToString(), false, true));
- }
- return pagesList;
- }
- private List<PageItem> CalculatePaginator()
- {
- var pages = new List<PageItem>();
- int firstPage = (int)Math.Floor((double)this.pagConfig.NumberOfPageSelected / 12) * 12;
- if (firstPage > 0)
- firstPage = firstPage - 2;
- for (int i = firstPage; i < (firstPage + Math.Min(12, this.pagConfig.NumberOfPages)) && (i < this.pagConfig.NumberOfPages); i++)
- {
- var isSelected = false;
- if (this.pagConfig.NumberOfPageSelected == i + 1)
- isSelected = true;
- PageItem page = new PageItem((i + 1).ToString(), isSelected, true);
- page.PageSelected += PageSelectedChanged;
- pages.Add(page);
- }
- if (this.pagConfig.NumberOfPages > this.pagConfig.PageSize && int.Parse(pages[pages.Count - 1].Page) != this.pagConfig.NumberOfPages)
- {
- pages.Add(new PageItem("...", false, false));
- var lastPage = new PageItem(this.pagConfig.NumberOfPages.ToString(), false, true);
- pages.Add(lastPage);
- lastPage.PageSelected += PageSelectedChanged;
- }
- return pages;
- }
- private void PageSelectedChanged(object sender, EventArgs args)
- {
- this.pagConfig.PageSelected = (PageItem)sender;
- NavigateToPage(this.pagConfig.NumberOfPageSelected);
- }
- private void ResetPaginator()
- {
- if (this.pagesList != null)
- {
- foreach (var page in this.pagesList)
- {
- page.PageSelected -= PageSelectedChanged;
- }
- }
- }
- private void NextPageCommandExecute()
- {
- if (this.pagConfig.CanGoNext)
- {
- this.pagConfig.NumberOfPageSelected = this.pagConfig.NumberOfPageSelected + 1;
- NavigateToPage(this.pagConfig.NumberOfPageSelected);
- }
- }
- private void PreviousPageCommandExecute()
- {
- if (this.pagConfig.CanGoPrevious)
- {
- this.pagConfig.NumberOfPageSelected = this.pagConfig.NumberOfPageSelected - 1;
- NavigateToPage(this.pagConfig.NumberOfPageSelected);
- }
- }
- private void NavigateToPage(int pageToNavigate)
- {
- try
- {
- Messenger.Default.Send(new LoadingMessage(true));
- ResetPaginator();
- var travels = AlertRuleRepository.GetAllAlertRules(filter, this.pagConfig.PageSize, pageToNavigate - 1);
- RuleList = new ObservableCollection<AlertRule>(travels);
- PagesList = new ObservableCollection<PageItem>(CalculatePaginator());
- }
- catch (Exception ex)
- {
- CustomDialogMessage message = new CustomDialogMessage(() => { }, StringResources.UnexpectedError, Visibility.Collapsed);
- Messenger.Default.Send<CustomDialogMessage>(message);
- }
- finally
- {
- Messenger.Default.Send(new LoadingMessage(false));
- }
- }
- #endregion
- }
- }
|