ConfigEditorViewModel.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. namespace HistoryView.ViewModels.Dialogs;
  2. public partial class ConfigEditorViewModel(HubSender hubSender) : ObservableObject, IDialogAware
  3. {
  4. public DialogCloseListener RequestClose { get; set; }
  5. [ObservableProperty]
  6. private TempConfig? _File;
  7. [ObservableProperty]
  8. private Visibility _Loading;
  9. public bool CanCloseDialog()
  10. {
  11. return true;
  12. }
  13. public void OnDialogClosed()
  14. {
  15. }
  16. public void OnDialogOpened(IDialogParameters parameters)
  17. {
  18. this.Loading = Visibility.Visible;
  19. if (!parameters.TryGetValue("ConfigEditor", out TempConfig? tempConfig) || tempConfig is null)
  20. {
  21. this.RequestClose.Invoke();
  22. return;
  23. }
  24. Task.Factory.StartNew(() =>
  25. {
  26. App.Current.Dispatcher.Invoke(() =>
  27. {
  28. this.File = new();
  29. tempConfig.Adapt(this.File);
  30. this.Loading = Visibility.Collapsed;
  31. });
  32. });
  33. }
  34. [RelayCommand]
  35. private void Save(string mode)
  36. {
  37. if (this.File is null)
  38. return;
  39. _ = mode switch
  40. {
  41. "save" => hubSender.SaveConfig(this.File.ConfigName, this.File, false),
  42. "saveas" => hubSender.SaveConfig(this.File.ConfigName, this.File, true),
  43. _ => false
  44. };
  45. this.RequestClose.Invoke();
  46. }
  47. [RelayCommand]
  48. private void Exit()
  49. {
  50. this.RequestClose.Invoke();
  51. }
  52. }