RealTimeViewModel.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using SWRIS.Dtos;
  2. using SWRIS.Extensions;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Windows;
  7. namespace SWRIS.Models.ViewModel
  8. {
  9. public class RealTimeViewModel : INotifyPropertyChanged
  10. {
  11. private TrolleyActiveModel activedTrolly;
  12. private ObservableCollection<SimpleRecordDto> records;
  13. private ObservableCollection<FaultsMessageDto> messages;
  14. private ObservableCollection<TrolleyActiveModel> trolleys;
  15. private ObservableCollection<EquipmentDataModel> equipments;
  16. public RealTimeViewModel(ObservableCollection<SimpleRecordDto> records)
  17. {
  18. Equipments = App.DataCenter.Equipments.ConvertToObservableCollection();
  19. Messages = App.DataCenter.Messages;
  20. Records = records;
  21. Trolleys = App.Config.Trolleys.Select(x => new TrolleyActiveModel
  22. {
  23. Id = x.Id,
  24. Name = x.Name,
  25. IsActived = false
  26. }).ConvertToObservableCollection();
  27. if (Trolleys.Any())
  28. {
  29. Trolleys[0].IsActived = true;
  30. ActivedTrolly = Trolleys[0];
  31. }
  32. }
  33. public ObservableCollection<SimpleRecordDto> Records
  34. {
  35. get => records;
  36. set
  37. {
  38. if (records != value)
  39. {
  40. records = value;
  41. OnPropertyChanged(nameof(Records));
  42. }
  43. }
  44. }
  45. public ObservableCollection<FaultsMessageDto> Messages
  46. {
  47. get => messages;
  48. set
  49. {
  50. if (messages != value)
  51. {
  52. messages = value;
  53. OnPropertyChanged(nameof(Messages));
  54. }
  55. }
  56. }
  57. public TrolleyActiveModel ActivedTrolly
  58. {
  59. get => activedTrolly;
  60. set
  61. {
  62. if (activedTrolly != value)
  63. {
  64. // 取消旧的选中项
  65. if (activedTrolly != null)
  66. activedTrolly.IsActived = false;
  67. activedTrolly = value;
  68. // 激活新的选中项
  69. if (activedTrolly != null)
  70. activedTrolly.IsActived = true;
  71. OnPropertyChanged(nameof(ActivedTrolly));
  72. }
  73. }
  74. }
  75. public ObservableCollection<TrolleyActiveModel> Trolleys
  76. {
  77. get => trolleys;
  78. set
  79. {
  80. trolleys = value;
  81. OnPropertyChanged(nameof(Trolleys));
  82. }
  83. }
  84. public ObservableCollection<EquipmentDataModel> Equipments
  85. {
  86. get => equipments;
  87. set
  88. {
  89. equipments = value;
  90. OnPropertyChanged(nameof(Equipments));
  91. }
  92. }
  93. public void AddRecord(SimpleRecordDto record)
  94. {
  95. record.IsNew = true;
  96. SafeCollectionInsert(Records, 0, record);
  97. // 延迟重置 IsNew 状态,避免重复触发动画
  98. var timer = new System.Windows.Threading.DispatcherTimer
  99. {
  100. Interval = System.TimeSpan.FromSeconds(3)
  101. };
  102. timer.Tick += (s, e) =>
  103. {
  104. record.IsNew = false;
  105. timer.Stop();
  106. };
  107. timer.Start();
  108. }
  109. protected void SafeCollectionInsert<T>(ObservableCollection<T> collection, int index, T item)
  110. {
  111. if (Application.Current.Dispatcher.CheckAccess())
  112. {
  113. collection.Insert(index, item);
  114. }
  115. else
  116. {
  117. Application.Current.Dispatcher.Invoke(() => collection.Insert(index, item));
  118. }
  119. }
  120. public event PropertyChangedEventHandler PropertyChanged;
  121. protected internal virtual void OnPropertyChanged(string propertyName)
  122. {
  123. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  124. }
  125. }
  126. }