AppRoot.partials.FileWriterViewModels.cs 3.3 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 FileWriterViewModels : ViewModelBase {
  9. public static FileWriterViewModels Instance { get; private set; } = new FileWriterViewModels();
  10. private readonly Dictionary<Guid, FileWriterViewModel> _dicById = new Dictionary<Guid, FileWriterViewModel>();
  11. public ICommand Add { get; private set; }
  12. private FileWriterViewModels() {
  13. if (WpfUtil.IsInDesignMode) {
  14. return;
  15. }
  16. this.Add = new DelegateCommand(() => {
  17. new FileWriterViewModel(Guid.NewGuid()).Edit.Execute(FormType.Add);
  18. });
  19. VirtualRoot.BuildEventPath<ServerContextReInitedEvent>("刷新VM内存", LogEnum.DevConsole,
  20. path: message => {
  21. _dicById.Clear();
  22. Init();
  23. }, location: this.GetType());
  24. VirtualRoot.BuildEventPath<ServerContextReInitedEventHandledEvent>("刷新视图界面", LogEnum.DevConsole,
  25. path: message => {
  26. OnPropertyChangeds();
  27. }, location: this.GetType());
  28. BuildEventPath<FileWriterAddedEvent>("调整VM内存", LogEnum.DevConsole,
  29. path: (message) => {
  30. if (!_dicById.ContainsKey(message.Source.GetId())) {
  31. FileWriterViewModel groupVm = new FileWriterViewModel(message.Source);
  32. _dicById.Add(message.Source.GetId(), groupVm);
  33. OnPropertyChangeds();
  34. }
  35. }, location: this.GetType());
  36. BuildEventPath<FileWriterUpdatedEvent>("调整VM内存", LogEnum.DevConsole,
  37. path: (message) => {
  38. if (_dicById.TryGetValue(message.Source.GetId(), out FileWriterViewModel vm)) {
  39. vm.Update(message.Source);
  40. }
  41. }, location: this.GetType());
  42. BuildEventPath<FileWriterRemovedEvent>("调整VM内存", LogEnum.DevConsole,
  43. path: (message) => {
  44. _dicById.Remove(message.Source.GetId());
  45. OnPropertyChangeds();
  46. }, location: this.GetType());
  47. Init();
  48. }
  49. private void Init() {
  50. foreach (var item in NTMinerContext.Instance.ServerContext.FileWriterSet.AsEnumerable()) {
  51. FileWriterViewModel groupVm = new FileWriterViewModel(item);
  52. _dicById.Add(item.GetId(), groupVm);
  53. }
  54. }
  55. private void OnPropertyChangeds() {
  56. OnPropertyChanged(nameof(List));
  57. }
  58. public bool TryGetFileWriterVm(Guid groupId, out FileWriterViewModel groupVm) {
  59. return _dicById.TryGetValue(groupId, out groupVm);
  60. }
  61. public List<FileWriterViewModel> List {
  62. get {
  63. return _dicById.Values.ToList();
  64. }
  65. }
  66. }
  67. }
  68. }