DisplayViewModelBase.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 : ViewModelBase, IDisplayViewModel
  16. {
  17. public virtual string IconKey { get; } = string.Empty;
  18. public virtual bool ShowTop { get; } = false;
  19. public virtual bool AppendSeparator { get; }
  20. public virtual string MenuKey { get; } = string.Empty;
  21. public virtual string MenuParentKey { get; } = string.Empty;
  22. public DisplayViewModelBase() : base() { }
  23. private protected virtual string Caption => LanguageValueViewModel.Instance.PromptTitle;
  24. private protected virtual string Yes => LanguageValueViewModel.Instance.PromptYes;
  25. private protected virtual string No => LanguageValueViewModel.Instance.PromptNo;
  26. public virtual double Width => double.NaN;
  27. public virtual double Height => double.NaN;
  28. public virtual bool CanResize => true;
  29. [AllowNull]
  30. public Action CloseWindowAction { get; set; }
  31. public ISukiToastManager ToastManager { get; } = new SukiToastManager();
  32. public ISukiDialogManager DialogManager { get; } = new SukiDialogManager();
  33. [AllowNull]
  34. public virtual Type Content
  35. {
  36. get => content;
  37. set => SetProperty(ref content, value);
  38. }
  39. private string title = string.Empty;
  40. private bool saveIsEnabled = false;
  41. public bool SaveIsEnabled
  42. {
  43. get { return saveIsEnabled; }
  44. set { SetProperty(ref saveIsEnabled, value); }
  45. }
  46. [AllowNull]
  47. public virtual bool CanCancel { get; }
  48. public bool ShowError(string msg)
  49. {
  50. return DispatherInovke.Inovke(() => DialogManager.CreateDialog()
  51. .OfType(Avalonia.Controls.Notifications.NotificationType.Error)
  52. .WithTitle(Caption)
  53. .WithContent(msg)
  54. .WithActionButton
  55. (Yes, _ => { }, true).TryShow());
  56. }
  57. public bool ShowAsk(string msg, Action? yesaction = null, Action? noaction = null)
  58. {
  59. return DispatherInovke.Inovke(() => DialogManager.CreateDialog()
  60. .OfType(Avalonia.Controls.Notifications.NotificationType.Information)
  61. .WithTitle(Caption)
  62. .WithContent(msg)
  63. .WithActionButton(Yes, _ => yesaction?.Invoke(), true)
  64. .WithActionButton(No, _ => noaction?.Invoke(), true)
  65. .TryShow());
  66. }
  67. public bool ShowInfo(string msg)
  68. {
  69. return DispatherInovke.Inovke(() => DialogManager.CreateDialog()
  70. .OfType(Avalonia.Controls.Notifications.NotificationType.Information)
  71. .WithTitle(Caption)
  72. .WithContent(msg)
  73. .WithActionButton
  74. (Yes, _ => { }, true).TryShow());
  75. }
  76. public bool ShowSuccess(string msg)
  77. {
  78. return DispatherInovke.Inovke(() => DialogManager.CreateDialog()
  79. .OfType(Avalonia.Controls.Notifications.NotificationType.Success)
  80. .WithTitle(Caption)
  81. .WithContent(msg)
  82. .WithActionButton
  83. (Yes, _ => { }, true).TryShow());
  84. }
  85. public bool ShowWarning(string msg)
  86. {
  87. return DispatherInovke.Inovke(() => DialogManager.CreateDialog()
  88. .OfType(Avalonia.Controls.Notifications.NotificationType.Warning)
  89. .WithTitle(Caption)
  90. .WithContent(msg)
  91. .WithActionButton
  92. (Yes, _ => { }, true).TryShow());
  93. }
  94. public void ShowToast(string msg, Avalonia.Controls.Notifications.NotificationType type = Avalonia.Controls.Notifications.NotificationType.Information)
  95. {
  96. if (ToastManager == null) return;
  97. DispatherInovke.Inovke(() => ToastManager.CreateToast()
  98. .WithTitle(Caption)
  99. .WithContent(msg)
  100. .OfType(type)
  101. .Dismiss().After(TimeSpan.FromSeconds(3))
  102. .Dismiss().ByClicking()
  103. .Queue());
  104. }
  105. private protected bool cansave = false;
  106. public virtual string OKContent => "Save";
  107. public virtual string CancelContent => "Cancel";
  108. public bool SaveClose { get => saveClose; set => SetProperty(ref saveClose, value); }
  109. public ICommand SaveCommand => new RelayCommand(async () =>
  110. {
  111. Save();
  112. });
  113. protected virtual void Save()
  114. {
  115. }
  116. public virtual void InitData()
  117. {
  118. }
  119. public ICommand CancelCommand => new RelayCommand(Cancel);
  120. public virtual string Title { get => title; set => SetProperty(ref title, value); }
  121. public bool ButtonVisibily { get => buttonVisibily; set => SetProperty(ref buttonVisibily, value); }
  122. [AllowNull]
  123. private Type content;
  124. protected virtual bool SkipProperty(string? propertyName)
  125. {
  126. return false;
  127. }
  128. protected override void OnPropertyChanged(PropertyChangedEventArgs e)
  129. {
  130. if (e.PropertyName != nameof(SaveClose)) SaveClose = true;
  131. base.OnPropertyChanged(e);
  132. if (e.PropertyName == nameof(Content)
  133. || e.PropertyName == nameof(ButtonVisibily)
  134. || e.PropertyName == nameof(SaveCommand)
  135. || e.PropertyName == nameof(Title)
  136. || e.PropertyName == nameof(SaveIsEnabled)
  137. || e.PropertyName == nameof(CancelCommand))
  138. return;
  139. if (SkipProperty(e.PropertyName)) return;
  140. SaveIsEnabled = true;
  141. }
  142. private bool buttonVisibily = true;
  143. private bool saveClose = true;
  144. protected virtual void Cancel()
  145. {
  146. }
  147. }
  148. }