MinerStudioRoot.partials.NTMinerWalletViewModels.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 NTMinerWalletViewModels Instance { get; private set; } = 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 (WpfUtil.IsInDesignMode) {
  14. return;
  15. }
  16. Init(refresh: false);
  17. AppRoot.BuildEventPath<NTMinerWalletSetInitedEvent>("NTMiner钱包集初始化后", LogEnum.DevConsole,
  18. path: message => {
  19. Init(refresh: true);
  20. }, location: this.GetType());
  21. this.Add = new DelegateCommand(() => {
  22. new NTMinerWalletViewModel(Guid.NewGuid()).Edit.Execute(FormType.Add);
  23. });
  24. AppRoot.BuildEventPath<NTMinerWalletAddedEvent>("添加NTMiner钱包后刷新VM内存", LogEnum.DevConsole,
  25. path: message => {
  26. if (!_dicById.ContainsKey(message.Source.GetId())) {
  27. _dicById.Add(message.Source.GetId(), new NTMinerWalletViewModel(message.Source));
  28. if (AppRoot.CoinVms.TryGetCoinVm(message.Source.CoinCode, out CoinViewModel coinVm)) {
  29. coinVm.OnPropertyChanged(nameof(coinVm.NTMinerWallets));
  30. }
  31. }
  32. }, location: this.GetType());
  33. AppRoot.BuildEventPath<NTMinerWalletUpdatedEvent>("更新NTMiner钱包后刷新VM内存", LogEnum.DevConsole,
  34. path: message => {
  35. if (_dicById.TryGetValue(message.Source.GetId(), out NTMinerWalletViewModel vm)) {
  36. vm.Update(message.Source);
  37. }
  38. }, location: this.GetType());
  39. AppRoot.BuildEventPath<NTMinerWalletRemovedEvent>("删除NTMiner钱包后刷新VM内存", LogEnum.DevConsole,
  40. path: message => {
  41. _dicById.Remove(message.Source.GetId());
  42. if (AppRoot.CoinVms.TryGetCoinVm(message.Source.CoinCode, out CoinViewModel coinVm)) {
  43. coinVm.OnPropertyChanged(nameof(coinVm.NTMinerWallets));
  44. }
  45. }, location: this.GetType());
  46. }
  47. private void Init(bool refresh) {
  48. _dicById.Clear();
  49. foreach (var item in NTMinerContext.MinerStudioContext.NTMinerWalletSet.AsEnumerable()) {
  50. _dicById.Add(item.GetId(), new NTMinerWalletViewModel(item));
  51. }
  52. if (refresh) {
  53. foreach (var coinVm in AppRoot.CoinVms.AllCoins) {
  54. coinVm.OnPropertyChanged(nameof(coinVm.NTMinerWallets));
  55. }
  56. }
  57. }
  58. public bool TryGetMineWorkVm(Guid id, out NTMinerWalletViewModel ntminerWalletVm) {
  59. return _dicById.TryGetValue(id, out ntminerWalletVm);
  60. }
  61. public IEnumerable<NTMinerWalletViewModel> Items {
  62. get {
  63. return _dicById.Values;
  64. }
  65. }
  66. }
  67. }
  68. }