ConfigFileViewModel.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. namespace HistoryView.ViewModels.Regions.SettingSubs;
  2. internal partial class ConfigFileViewModel : ObservableObject
  3. {
  4. public ConfigFileViewModel(MessageBoxHelper messageBox, IDialogService dialogService, TemperatureConfigs temperatureConfigs, UserInformation user, HubSender sender)
  5. {
  6. this._sender = sender;
  7. this._messageBox = messageBox;
  8. this._dialogService = dialogService;
  9. this.TemperatureConfigs = temperatureConfigs;
  10. this.UserInfo = user;
  11. this.IsEditable = true;
  12. }
  13. private readonly HubSender _sender;
  14. private readonly MessageBoxHelper _messageBox;
  15. private readonly IDialogService _dialogService;
  16. [ObservableProperty]
  17. private TemperatureConfigs _temperatureConfigs;
  18. [ObservableProperty]
  19. private TempConfig? _Selected;
  20. [ObservableProperty]
  21. private UserInformation? _UserInfo;
  22. [ObservableProperty]
  23. private bool _IsEditable;
  24. [RelayCommand]
  25. private void Load()
  26. {
  27. this._dialogService.Show("File", new DialogParameters(), GetPara);
  28. }
  29. partial void OnSelectedChanged(TempConfig? value)
  30. {
  31. if (value is null)
  32. return;
  33. if (this.TemperatureConfigs.CurrentConfigFile is null)
  34. return;
  35. this.IsEditable = (value != this.TemperatureConfigs.CurrentConfigFile.ConfigFile);
  36. }
  37. [RelayCommand]
  38. private async Task ReadCurrentAndSave()
  39. {
  40. if (!this._sender.QueryMini8Data(out TempConfig? config) || config is null)
  41. {
  42. await _messageBox.ShowAsync("Error", icon: MessageBoxImage.Error);
  43. return;
  44. }
  45. config.ConfigName += "-Saved";
  46. IDialogParameters para = new DialogParameters
  47. {
  48. { "ConfigEditor", config }
  49. };
  50. this._dialogService.Show("ConfigEditor", para, null);
  51. }
  52. [RelayCommand]
  53. private void SelectCurrent()
  54. {
  55. if (this.TemperatureConfigs is null || this.TemperatureConfigs.CurrentConfigFile is null)
  56. return;
  57. this.Selected = this.TemperatureConfigs.CurrentConfigFile.ConfigFile;
  58. }
  59. [RelayCommand]
  60. private void Edit()
  61. {
  62. if (this.Selected is null)
  63. return;
  64. IDialogParameters para = new DialogParameters
  65. {
  66. { "ConfigEditor", this.Selected }
  67. };
  68. this._dialogService.Show("ConfigEditor", para, null);
  69. }
  70. [RelayCommand]
  71. private void Remove()
  72. {
  73. if (this.Selected is null)
  74. return;
  75. if (string.IsNullOrEmpty(this.Selected.ConfigName))
  76. return;
  77. var t = this._messageBox.ShowAsync($"{Selected.ConfigName} ?", MessageBoxButton.YesNo, MessageBoxImage.Question).Result;
  78. if (t.Result != ButtonResult.Yes)
  79. return;
  80. _sender.RemoveFile(this.Selected.ConfigName);
  81. }
  82. [RelayCommand]
  83. private void SetAsCurrent()
  84. {
  85. if (this.Selected is null)
  86. return;
  87. if (_messageBox is null)
  88. return;
  89. var t = this._messageBox.ShowAsync($"{Selected.ConfigName} {App.Current.FindResource("SetConfigAsCurrent")}", MessageBoxButton.YesNo, MessageBoxImage.Question).Result;
  90. if (t.Result != ButtonResult.Yes)
  91. return;
  92. this._sender.SetConfigFile(this.Selected.ConfigName);
  93. }
  94. [RelayCommand]
  95. private void Compare()
  96. {
  97. IDialogParameters para = new DialogParameters
  98. {
  99. { "Source", this.Selected! }
  100. };
  101. this._dialogService.Show("Compare", para, GetPara);
  102. }
  103. private void GetPara(IDialogResult dialogResult)
  104. {
  105. if (!dialogResult.Parameters.TryGetValue<FileInfo>("File", out FileInfo? file) || file is null)
  106. return;
  107. _sender.LoadNewFile(file.FullName);
  108. }
  109. }