MinerStudioRoot.partials.NTMinerWalletViewModels.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using NTMiner.MinerStudio.Vms;
  2. using NTMiner.Vms;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Windows.Input;
  6. namespace NTMiner.MinerStudio {
  7. public static partial class MinerStudioRoot {
  8. public class NTMinerWalletViewModels : ViewModelBase {
  9. public static readonly NTMinerWalletViewModels Instance = new NTMinerWalletViewModels();
  10. private readonly Dictionary<Guid, NTMinerWalletViewModel> _dicById = new Dictionary<Guid, NTMinerWalletViewModel>();
  11. public ICommand Add { get; private set; }
  12. private NTMinerWalletViewModels() {
  13. #if DEBUG
  14. NTStopwatch.Start();
  15. #endif
  16. if (WpfUtil.IsInDesignMode) {
  17. return;
  18. }
  19. Init(refresh: false);
  20. AppRoot.AddEventPath<NTMinerWalletSetInitedEvent>("NTMiner钱包集初始化后", LogEnum.DevConsole,
  21. action: message => {
  22. Init(refresh: true);
  23. }, location: this.GetType());
  24. this.Add = new DelegateCommand(() => {
  25. new NTMinerWalletViewModel(Guid.NewGuid()).Edit.Execute(FormType.Add);
  26. });
  27. AppRoot.AddEventPath<NTMinerWalletAddedEvent>("添加NTMiner钱包后刷新VM内存", LogEnum.DevConsole,
  28. action: message => {
  29. if (!_dicById.ContainsKey(message.Target.GetId())) {
  30. _dicById.Add(message.Target.GetId(), new NTMinerWalletViewModel(message.Target));
  31. if (AppRoot.CoinVms.TryGetCoinVm(message.Target.CoinId, out CoinViewModel coinVm)) {
  32. coinVm.OnPropertyChanged(nameof(coinVm.NTMinerWallets));
  33. }
  34. }
  35. }, location: this.GetType());
  36. AppRoot.AddEventPath<NTMinerWalletUpdatedEvent>("更新NTMiner钱包后刷新VM内存", LogEnum.DevConsole,
  37. action: message => {
  38. if (_dicById.TryGetValue(message.Target.GetId(), out NTMinerWalletViewModel vm)) {
  39. vm.Update(message.Target);
  40. }
  41. }, location: this.GetType());
  42. AppRoot.AddEventPath<NTMinerWalletRemovedEvent>("删除NTMiner钱包后刷新VM内存", LogEnum.DevConsole,
  43. action: message => {
  44. _dicById.Remove(message.Target.GetId());
  45. if (AppRoot.CoinVms.TryGetCoinVm(message.Target.CoinId, out CoinViewModel coinVm)) {
  46. coinVm.OnPropertyChanged(nameof(coinVm.NTMinerWallets));
  47. }
  48. }, location: this.GetType());
  49. #if DEBUG
  50. var elapsedMilliseconds = NTStopwatch.Stop();
  51. if (elapsedMilliseconds.ElapsedMilliseconds > NTStopwatch.ElapsedMilliseconds) {
  52. Write.DevTimeSpan($"耗时{elapsedMilliseconds} {this.GetType().Name}.ctor");
  53. }
  54. #endif
  55. }
  56. private void Init(bool refresh) {
  57. _dicById.Clear();
  58. foreach (var item in NTMinerContext.Instance.MinerStudioContext.NTMinerWalletSet.AsEnumerable()) {
  59. _dicById.Add(item.GetId(), new NTMinerWalletViewModel(item));
  60. }
  61. if (refresh) {
  62. foreach (var coinVm in AppRoot.CoinVms.AllCoins) {
  63. coinVm.OnPropertyChanged(nameof(coinVm.NTMinerWallets));
  64. }
  65. }
  66. }
  67. public bool TryGetMineWorkVm(Guid id, out NTMinerWalletViewModel ntminerWalletVm) {
  68. return _dicById.TryGetValue(id, out ntminerWalletVm);
  69. }
  70. public IEnumerable<NTMinerWalletViewModel> Items {
  71. get {
  72. return _dicById.Values;
  73. }
  74. }
  75. }
  76. }
  77. }