CommandListViewModel.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Input;
  9. using OHV.Common.Events;
  10. using OHV.Common.Model;
  11. using OHV.Common.Shareds;
  12. using OHV.Module.Interactivity;
  13. using OHV.SqliteDAL;
  14. using Prism.Commands;
  15. using Prism.Events;
  16. using Prism.Mvvm;
  17. namespace OHV.Module.ListViews.Views
  18. {
  19. class CommandListViewModel : BindableBase
  20. {
  21. #region Binding Properties
  22. private ObservableCollection<Command> _commandList;
  23. public ObservableCollection<Command> CommandList
  24. {
  25. get { return this._commandList; }
  26. set { SetProperty(ref this._commandList, value); }
  27. }
  28. private string targetID;
  29. public string TargetID
  30. {
  31. get { return targetID; }
  32. set { SetProperty(ref this.targetID, value); }
  33. }
  34. private eCommandType selectedCommandType;
  35. public eCommandType SelectedCommandType
  36. {
  37. get { return selectedCommandType; }
  38. set { SetProperty(ref this.selectedCommandType, value); }
  39. }
  40. private List<string> targetIDList;
  41. public List<string> TargetIDList
  42. {
  43. get { return targetIDList; }
  44. set { SetProperty(ref this.targetIDList, value); }
  45. }
  46. #endregion
  47. public ICommand SaveCommand { get; set; }
  48. public ICommand DeleteCommand { get; set; }
  49. IEventAggregator eventAggregator;
  50. SqliteManager sql;
  51. MessageController messageController;
  52. public CommandListViewModel(IEventAggregator ea, SqliteManager sql, MessageController msgController )
  53. {
  54. this.eventAggregator = ea;
  55. this.messageController = msgController;
  56. this.sql = sql;
  57. this.sql.CommandDAL.ChangedProperty += this.RefreshCommandLIst;
  58. this.CommandList = new ObservableCollection<Command>(sql.CommandDAL.All);
  59. this.TargetIDList = sql.RouteDal.All.Select(x => x.Name).ToList();
  60. this.TargetIDList.Add("None");
  61. this.SaveCommand = new DelegateCommand(ExecuteSaveCommand);
  62. this.DeleteCommand = new DelegateCommand(ExecuteDeleteCommand);
  63. this.eventAggregator.GetEvent<VehicleCommandListChanged>().Unsubscribe(UICallbackCommunication);
  64. this.eventAggregator.GetEvent<VehicleCommandListChanged>().Subscribe(UICallbackCommunication, ThreadOption.UIThread);
  65. }
  66. private void UICallbackCommunication(string obj)
  67. {
  68. //RefreshCommandLIst(null, null);
  69. }
  70. private void ExecuteDeleteCommand()
  71. {
  72. List<Command> deleteList = new List<Command>();
  73. foreach (var item in this.CommandList)
  74. {
  75. if (item.IsSelected)
  76. {
  77. deleteList.Add(item);
  78. }
  79. }
  80. deleteList.ForEach(x => { this.sql.CommandDAL.Delete(x); });
  81. //RefreshCommandLIst();
  82. }
  83. private void ExecuteSaveCommand()
  84. {
  85. Command cmd = new Command() { TargetID = TargetID, Type = SelectedCommandType };
  86. var route = sql.RouteDal.GetRoute( TargetID );
  87. if (route ==null )
  88. messageController.ShowNotificationView( "Not Found TargetID" );
  89. else
  90. sql.CommandDAL.Add(cmd);
  91. this.TargetID = "None";
  92. this.SelectedCommandType = eCommandType.Move;
  93. }
  94. void RefreshCommandLIst()
  95. {
  96. Application.Current.Dispatcher.Invoke(() =>
  97. {
  98. this.CommandList.Clear();
  99. this.CommandList.AddRange(sql.CommandDAL.All);
  100. });
  101. }
  102. }
  103. }