ViewModelBase.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using CommunityToolkit.Mvvm.ComponentModel;
  4. using CommunityToolkit.Mvvm.Input;
  5. using EventBus;
  6. using SukiUI.Dialogs;
  7. using SukiUI.Toasts;
  8. using System;
  9. using System.ComponentModel;
  10. using System.Diagnostics.CodeAnalysis;
  11. using System.Linq;
  12. using System.Windows.Input;
  13. namespace ShakerApp.ViewModels;
  14. public abstract class ViewModelBase<TModel> : ObservableObject where TModel : Shaker.Model.IModel
  15. {
  16. public virtual double Width => double.NaN;
  17. public virtual double Height => double.NaN;
  18. public virtual bool CanResize => true;
  19. [AllowNull]
  20. public Action CloseWindowAction { get; set; }
  21. public ISukiToastManager ToastManager { get; } = new SukiToastManager();
  22. public ISukiDialogManager DialogManager { get; } = new SukiDialogManager();
  23. [AllowNull]
  24. public virtual Type Content
  25. {
  26. get => content;
  27. set => SetProperty(ref content , value);
  28. }
  29. private string title = string.Empty;
  30. private bool saveIsEnabled= false;
  31. public bool SaveIsEnabled
  32. {
  33. get { return saveIsEnabled; }
  34. set {SetProperty(ref saveIsEnabled , value); }
  35. }
  36. [AllowNull]
  37. public DialogsViewModel Dialogs { get; }
  38. public virtual bool CanCancel { get; } = typeof(TModel).IsAnsiClass && !typeof(TModel).IsAbstract;
  39. public ViewModelBase()
  40. {
  41. Dialogs = new DialogsViewModel(DialogManager);
  42. if (typeof(TModel).IsAnsiClass && !typeof(TModel).IsAbstract)
  43. {
  44. Model = Activator.CreateInstance<TModel>();
  45. }
  46. }
  47. public ViewModelBase(TModel model)
  48. {
  49. UpDateModel(model);
  50. }
  51. public virtual void UpDateModel(TModel model)
  52. {
  53. Model = model;
  54. RefreshUI();
  55. }
  56. public virtual void Init()
  57. {
  58. }
  59. protected void RefreshUI()
  60. {
  61. GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).ToList().ForEach(x =>
  62. {
  63. OnPropertyChanged(x.Name);
  64. });
  65. }
  66. private bool cansave = false;
  67. public virtual string OKContent => "Save";
  68. public virtual string CancelContent =>"Cancel";
  69. [AllowNull]
  70. private TModel model = default;
  71. public virtual TModel Model { get => model; set => SetProperty(ref model, value); }
  72. [return: NotNull]
  73. protected EventBus.EventBroker.EventData<TData> GetEvent<TData>() => (EventBroker.EventData<TData>)EventBroker.Instance.GetEvent<TData>();
  74. [return: NotNull]
  75. protected EventBus.EventBroker.EventData<TData, T> GetEvent<TData, T>() => (EventBroker.EventData<TData, T>)EventBroker.Instance.GetEvent<TData, T>();
  76. [return: NotNull]
  77. public EventBus.EventBroker.AnonymousEventData GetEvent([NotNull] string eventName) => (EventBroker.AnonymousEventData)EventBroker.Instance.GetEvent(eventName);
  78. [return: NotNull]
  79. public EventBus.EventBroker.AnonymousEventData<T> GetEvent<T>([NotNull] string eventName) => (EventBroker.AnonymousEventData<T>)EventBroker.Instance.GetEvent<T>(eventName);
  80. public ICommand SaveCommand => new RelayCommand(()=>
  81. {
  82. if (!cansave) return;
  83. Save();
  84. cansave = false;
  85. });
  86. public ICommand CancelCommand => new RelayCommand(Cancel);
  87. public virtual string Title { get => title; set =>SetProperty(ref title , value); }
  88. public bool ButtonVisibily { get => buttonVisibily; set =>SetProperty(ref buttonVisibily , value); }
  89. protected virtual void Save()
  90. {
  91. CommunicationViewModel.Intance.LocalCommunication?.GetEvent<TModel>().Publish(this, Model);
  92. CommunicationViewModel.Intance.ServiceCommunication?.GetEvent<TModel>().Publish(this, Model);
  93. lastModel = default;
  94. }
  95. [AllowNull]
  96. protected TModel lastModel;
  97. [AllowNull]
  98. private Type content;
  99. public virtual void InitData()
  100. {
  101. cansave = true;
  102. if (CanCancel)
  103. {
  104. lastModel = (TModel)Model.Clone();
  105. SaveIsEnabled = false;
  106. }
  107. else
  108. {
  109. lastModel = default;
  110. }
  111. }
  112. protected virtual bool SkipProperty(string? propertyName)
  113. {
  114. return false;
  115. }
  116. protected override void OnPropertyChanged(PropertyChangedEventArgs e)
  117. {
  118. base.OnPropertyChanged(e);
  119. if (e.PropertyName == nameof(Content)
  120. || e.PropertyName == nameof(Model)
  121. || e.PropertyName == nameof(ButtonVisibily)
  122. || e.PropertyName == nameof(SaveCommand)
  123. || e.PropertyName == nameof(Title)
  124. || e.PropertyName == nameof(SaveIsEnabled)
  125. || e.PropertyName == nameof(CancelCommand))
  126. return;
  127. if (SkipProperty(e.PropertyName)) return;
  128. SaveIsEnabled = true;
  129. }
  130. private bool buttonVisibily = true;
  131. protected virtual void Cancel()
  132. {
  133. if (CanCancel && lastModel !=null)
  134. {
  135. UpDateModel(lastModel);
  136. lastModel = default;
  137. }
  138. }
  139. }