DisplayViewModelBase{T}.cs 6.3 KB

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