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; /// /// Travel list ViewModel. /// public class RuleListViewModel : VMBase { private const int PAGESIZE = 25; private RelayCommand editRuleCommand; private RelayCommand addRuleCommand; private RelayCommand refreshRuleListCommand; private RelayCommand removeRuleCommand; private RelayCommand viewRuleCommand; private RelayCommand nextPageCommand; private RelayCommand previousPageCommand; private ObservableCollection ruleList; private ObservableCollection 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(message); } } private void InitializeMockedData() { var alertRules = AlertRuleRepository.GetAlertRules(); //var travels = this.sampleDataService.GetTravelRequests(); RuleList = new ObservableCollection(alertRules); var pages = GetPagesList(); PagesList = new ObservableCollection(pages); } private void SubscribeCommands() { this.editRuleCommand = new RelayCommand(EditRuleCommandExecute); this.addRuleCommand = new RelayCommand(AddRuleCommandExecute); this.viewRuleCommand = new RelayCommand(ViewRuleCommandExecute); this.nextPageCommand = new RelayCommand(NextPageCommandExecute); this.previousPageCommand = new RelayCommand(PreviousPageCommandExecute); this.refreshRuleListCommand = new RelayCommand(RefreshRuleListCommandExecute); this.removeRuleCommand = new RelayCommand(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 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(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 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 GetPagesList() { var pagesList = new List(); 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 CalculatePaginator() { var pages = new List(); 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(travels); PagesList = new ObservableCollection(CalculatePaginator()); } catch (Exception ex) { CustomDialogMessage message = new CustomDialogMessage(() => { }, StringResources.UnexpectedError, Visibility.Collapsed); Messenger.Default.Send(message); } finally { Messenger.Default.Send(new LoadingMessage(false)); } } #endregion } }