DisplayViewModelBase{TModel}.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using CommunityToolkit.Mvvm.Input;
  2. using IViewModel.Tools;
  3. using SukiUI.Dialogs;
  4. using SukiUI.Toasts;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Diagnostics.CodeAnalysis;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Input;
  13. namespace IViewModel.ViewModels
  14. {
  15. public abstract class DisplayViewModelBase<TModel> :ViewModelBase<TModel>,IDisplayViewModel where TModel : IModel.IModel
  16. {
  17. protected void SetSaveClose(bool enabled) => SaveIsEnabled = enabled;
  18. public virtual string IconKey { get; } = string.Empty;
  19. public virtual bool ShowTop { get; } = false;
  20. public virtual bool AppendSeparator { get; }
  21. public virtual string MenuKey { get; } = string.Empty;
  22. public virtual string MenuParentKey { get; } = string.Empty;
  23. public DisplayViewModelBase() : base() { }
  24. public DisplayViewModelBase(TModel model) : base(model) { }
  25. private protected virtual string Caption => LanguageValueViewModel.Instance.PromptTitle;
  26. private protected virtual string Yes => LanguageValueViewModel.Instance.PromptYes;
  27. private protected virtual string No => LanguageValueViewModel.Instance.PromptNo;
  28. public virtual double Width => double.NaN;
  29. public virtual double Height => double.NaN;
  30. public virtual bool CanResize => true;
  31. [AllowNull]
  32. public Action CloseWindowAction { get; set; }
  33. public ISukiToastManager ToastManager { get; } = new SukiToastManager();
  34. public ISukiDialogManager DialogManager { get; } = new SukiDialogManager();
  35. [AllowNull]
  36. public virtual Type Content
  37. {
  38. get => content;
  39. set => SetProperty(ref content, value);
  40. }
  41. private string title = string.Empty;
  42. private bool saveIsEnabled = false;
  43. public bool SaveIsEnabled
  44. {
  45. get { return saveIsEnabled; }
  46. set { SetProperty(ref saveIsEnabled, value); }
  47. }
  48. [AllowNull]
  49. public virtual bool CanCancel { get; } = typeof(TModel).IsAnsiClass && !typeof(TModel).IsAbstract;
  50. public bool ShowError(string msg)
  51. {
  52. return DispatherInovke.Inovke(() => DialogManager.CreateDialog()
  53. .OfType(Avalonia.Controls.Notifications.NotificationType.Error)
  54. .WithTitle(Caption)
  55. .WithContent(msg)
  56. .WithActionButton
  57. (Yes, _ => { }, true).TryShow());
  58. }
  59. public bool ShowAsk(string msg, Action? yesaction = null, Action? noaction = null)
  60. {
  61. return DispatherInovke.Inovke(() => DialogManager.CreateDialog()
  62. .OfType(Avalonia.Controls.Notifications.NotificationType.Information)
  63. .WithTitle(Caption)
  64. .WithContent(msg)
  65. .WithActionButton(Yes, _ => yesaction?.Invoke(), true)
  66. .WithActionButton(No, _ => noaction?.Invoke(), true)
  67. .TryShow());
  68. }
  69. public bool ShowInfo(string msg)
  70. {
  71. return DispatherInovke.Inovke(() => DialogManager.CreateDialog()
  72. .OfType(Avalonia.Controls.Notifications.NotificationType.Information)
  73. .WithTitle(Caption)
  74. .WithContent(msg)
  75. .WithActionButton
  76. (Yes, _ => { }, true).TryShow());
  77. }
  78. public bool ShowSuccess(string msg)
  79. {
  80. return DispatherInovke.Inovke(() => DialogManager.CreateDialog()
  81. .OfType(Avalonia.Controls.Notifications.NotificationType.Success)
  82. .WithTitle(Caption)
  83. .WithContent(msg)
  84. .WithActionButton
  85. (Yes, _ => { }, true).TryShow());
  86. }
  87. public bool ShowWarning(string msg)
  88. {
  89. return DispatherInovke.Inovke(() => DialogManager.CreateDialog()
  90. .OfType(Avalonia.Controls.Notifications.NotificationType.Warning)
  91. .WithTitle(Caption)
  92. .WithContent(msg)
  93. .WithActionButton
  94. (Yes, _ => { }, true).TryShow());
  95. }
  96. public void ShowToast(string msg, Avalonia.Controls.Notifications.NotificationType type = Avalonia.Controls.Notifications.NotificationType.Information)
  97. {
  98. if (ToastManager == null) return;
  99. DispatherInovke.Inovke(() => ToastManager.CreateToast()
  100. .WithTitle(Caption)
  101. .WithContent(msg)
  102. .OfType(type)
  103. .Dismiss().After(TimeSpan.FromSeconds(3))
  104. .Dismiss().ByClicking()
  105. .Queue());
  106. }
  107. private protected bool cansave = false;
  108. public virtual string OKContent => nameof(LanguageValueViewModel.Save);
  109. public virtual string CancelContent => nameof(LanguageValueViewModel.Close);
  110. public bool SaveClose { get => saveClose; set => SetProperty(ref saveClose, value); }
  111. public ICommand SaveCommand => new RelayCommand(async () =>
  112. {
  113. if (!cansave) return;
  114. Save();
  115. if (!savecanclose)
  116. {
  117. cansave = true;
  118. SaveClose = false;
  119. await Task.Delay(2);
  120. await Task.Run(() =>
  121. {
  122. DispatherInovke.Inovke(() => SaveClose = true);
  123. });
  124. UpLastModel();
  125. return;
  126. }
  127. cansave = false;
  128. });
  129. protected void UpLastModel()
  130. {
  131. lastModel = (TModel)Model.Clone();
  132. SaveIsEnabled = false;
  133. }
  134. public ICommand CancelCommand => new RelayCommand(Cancel);
  135. public virtual string Title { get => title; set => SetProperty(ref title, value); }
  136. public bool ButtonVisibily { get => buttonVisibily; set => SetProperty(ref buttonVisibily, value); }
  137. protected bool savecanclose = true;
  138. protected virtual void Save()
  139. {
  140. lastModel = default;
  141. savecanclose = true;
  142. }
  143. [AllowNull]
  144. private Type content;
  145. public virtual void InitData()
  146. {
  147. SaveClose = true;
  148. cansave = true;
  149. if (CanCancel)
  150. {
  151. lastModel = (TModel)Model.Clone();
  152. SaveIsEnabled = false;
  153. }
  154. else
  155. {
  156. //lastModel = default;
  157. }
  158. }
  159. protected virtual bool SkipProperty(string? propertyName)
  160. {
  161. return false;
  162. }
  163. protected override void OnPropertyChanged(PropertyChangedEventArgs e)
  164. {
  165. if (e.PropertyName != nameof(SaveClose)) SaveClose = true;
  166. base.OnPropertyChanged(e);
  167. if (e.PropertyName == nameof(Content)
  168. || e.PropertyName == nameof(Model)
  169. || e.PropertyName == nameof(ButtonVisibily)
  170. || e.PropertyName == nameof(SaveCommand)
  171. || e.PropertyName == nameof(Title)
  172. || e.PropertyName == nameof(SaveIsEnabled)
  173. || e.PropertyName == nameof(CancelCommand))
  174. return;
  175. if (SkipProperty(e.PropertyName)) return;
  176. SaveIsEnabled = true;
  177. }
  178. private bool buttonVisibily = true;
  179. private bool saveClose = true;
  180. protected virtual void Cancel()
  181. {
  182. if (CanCancel && lastModel != null)
  183. {
  184. UpDateModel(lastModel);
  185. lastModel = default;
  186. }
  187. }
  188. }
  189. }