AppRoot.partials.FragmentWriterViewModels.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using NTMiner.Vms;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Windows.Input;
  6. namespace NTMiner {
  7. public static partial class AppRoot {
  8. public class FragmentWriterViewModels : ViewModelBase {
  9. public static FragmentWriterViewModels Instance { get; private set; } = new FragmentWriterViewModels();
  10. private readonly Dictionary<Guid, FragmentWriterViewModel> _dicById = new Dictionary<Guid, FragmentWriterViewModel>();
  11. public ICommand Add { get; private set; }
  12. private FragmentWriterViewModels() {
  13. if (WpfUtil.IsInDesignMode) {
  14. return;
  15. }
  16. this.Add = new DelegateCommand(() => {
  17. new FragmentWriterViewModel(Guid.NewGuid()).Edit.Execute(FormType.Add);
  18. });
  19. VirtualRoot.BuildEventPath<ServerContextReInitedEvent>("刷新VM内存", LogEnum.DevConsole, location: this.GetType(), PathPriority.Normal,
  20. path: message => {
  21. _dicById.Clear();
  22. Init();
  23. });
  24. VirtualRoot.BuildEventPath<ServerContextReInitedEvent>("刷新视图界面", LogEnum.DevConsole, location: this.GetType(), PathPriority.BelowNormal,
  25. path: message => {
  26. OnPropertyChangeds();
  27. });
  28. BuildEventPath<FragmentWriterAddedEvent>("调整VM内存", LogEnum.DevConsole, location: this.GetType(), PathPriority.Normal,
  29. path: (message) => {
  30. if (!_dicById.ContainsKey(message.Source.GetId())) {
  31. FragmentWriterViewModel vm = new FragmentWriterViewModel(message.Source);
  32. _dicById.Add(message.Source.GetId(), vm);
  33. OnPropertyChangeds();
  34. }
  35. });
  36. BuildEventPath<FragmentWriterUpdatedEvent>("调整VM内存", LogEnum.DevConsole, location: this.GetType(), PathPriority.Normal,
  37. path: (message) => {
  38. if (_dicById.TryGetValue(message.Source.GetId(), out FragmentWriterViewModel vm)) {
  39. vm.Update(message.Source);
  40. }
  41. });
  42. BuildEventPath<FragmentWriterRemovedEvent>("调整VM内存", LogEnum.DevConsole, location: this.GetType(), PathPriority.Normal,
  43. path: (message) => {
  44. _dicById.Remove(message.Source.GetId());
  45. OnPropertyChangeds();
  46. });
  47. Init();
  48. }
  49. private void Init() {
  50. foreach (var item in NTMinerContext.Instance.ServerContext.FragmentWriterSet.AsEnumerable().ToArray()) {
  51. FragmentWriterViewModel groupVm = new FragmentWriterViewModel(item);
  52. _dicById.Add(item.GetId(), groupVm);
  53. }
  54. }
  55. private void OnPropertyChangeds() {
  56. OnPropertyChanged(nameof(List));
  57. }
  58. public bool TryGetFragmentWriterVm(Guid groupId, out FragmentWriterViewModel groupVm) {
  59. return _dicById.TryGetValue(groupId, out groupVm);
  60. }
  61. public List<FragmentWriterViewModel> List {
  62. get {
  63. return _dicById.Values.ToList();
  64. }
  65. }
  66. }
  67. }
  68. }