123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- using CommunityToolkit.Mvvm.Input;
- using IViewModel.Tools;
- using SukiUI.Dialogs;
- using SukiUI.Toasts;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Diagnostics.CodeAnalysis;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Input;
- namespace IViewModel.ViewModels
- {
- public abstract class DisplayViewModelBase<TModel> :ViewModelBase<TModel>,IDisplayViewModel where TModel : IModel.IModel
- {
- protected void SetSaveClose(bool enabled) => SaveIsEnabled = enabled;
- public virtual string IconKey { get; } = string.Empty;
- public virtual bool ShowTop { get; } = false;
- public virtual bool AppendSeparator { get; }
- public virtual string MenuKey { get; } = string.Empty;
- public virtual string MenuParentKey { get; } = string.Empty;
- public DisplayViewModelBase() : base() { }
- public DisplayViewModelBase(TModel model) : base(model) { }
- private protected virtual string Caption => LanguageValueViewModel.Instance.PromptTitle;
- private protected virtual string Yes => LanguageValueViewModel.Instance.PromptYes;
- private protected virtual string No => LanguageValueViewModel.Instance.PromptNo;
- public virtual double Width => double.NaN;
- public virtual double Height => double.NaN;
- public virtual bool CanResize => true;
- [AllowNull]
- public Action CloseWindowAction { get; set; }
- public ISukiToastManager ToastManager { get; } = new SukiToastManager();
- public ISukiDialogManager DialogManager { get; } = new SukiDialogManager();
- [AllowNull]
- public virtual Type Content
- {
- get => content;
- set => SetProperty(ref content, value);
- }
- private string title = string.Empty;
- private bool saveIsEnabled = false;
- public bool SaveIsEnabled
- {
- get { return saveIsEnabled; }
- set { SetProperty(ref saveIsEnabled, value); }
- }
- [AllowNull]
- public virtual bool CanCancel { get; } = typeof(TModel).IsAnsiClass && !typeof(TModel).IsAbstract;
- public bool ShowError(string msg)
- {
- return DispatherInovke.Inovke(() => DialogManager.CreateDialog()
- .OfType(Avalonia.Controls.Notifications.NotificationType.Error)
- .WithTitle(Caption)
- .WithContent(msg)
- .WithActionButton
- (Yes, _ => { }, true).TryShow());
- }
- public bool ShowAsk(string msg, Action? yesaction = null, Action? noaction = null)
- {
- return DispatherInovke.Inovke(() => DialogManager.CreateDialog()
- .OfType(Avalonia.Controls.Notifications.NotificationType.Information)
- .WithTitle(Caption)
- .WithContent(msg)
- .WithActionButton(Yes, _ => yesaction?.Invoke(), true)
- .WithActionButton(No, _ => noaction?.Invoke(), true)
- .TryShow());
- }
- public bool ShowInfo(string msg)
- {
- return DispatherInovke.Inovke(() => DialogManager.CreateDialog()
- .OfType(Avalonia.Controls.Notifications.NotificationType.Information)
- .WithTitle(Caption)
- .WithContent(msg)
- .WithActionButton
- (Yes, _ => { }, true).TryShow());
- }
- public bool ShowSuccess(string msg)
- {
- return DispatherInovke.Inovke(() => DialogManager.CreateDialog()
- .OfType(Avalonia.Controls.Notifications.NotificationType.Success)
- .WithTitle(Caption)
- .WithContent(msg)
- .WithActionButton
- (Yes, _ => { }, true).TryShow());
- }
- public bool ShowWarning(string msg)
- {
- return DispatherInovke.Inovke(() => DialogManager.CreateDialog()
- .OfType(Avalonia.Controls.Notifications.NotificationType.Warning)
- .WithTitle(Caption)
- .WithContent(msg)
- .WithActionButton
- (Yes, _ => { }, true).TryShow());
- }
- public void ShowToast(string msg, Avalonia.Controls.Notifications.NotificationType type = Avalonia.Controls.Notifications.NotificationType.Information)
- {
- if (ToastManager == null) return;
- DispatherInovke.Inovke(() => ToastManager.CreateToast()
- .WithTitle(Caption)
- .WithContent(msg)
- .OfType(type)
- .Dismiss().After(TimeSpan.FromSeconds(3))
- .Dismiss().ByClicking()
- .Queue());
- }
- private protected bool cansave = false;
- public virtual string OKContent => nameof(LanguageValueViewModel.Save);
- public virtual string CancelContent => nameof(LanguageValueViewModel.Close);
- public bool SaveClose { get => saveClose; set => SetProperty(ref saveClose, value); }
- public ICommand SaveCommand => new RelayCommand(async () =>
- {
- if (!cansave) return;
- Save();
- if (!savecanclose)
- {
- cansave = true;
- SaveClose = false;
- await Task.Delay(2);
- await Task.Run(() =>
- {
- DispatherInovke.Inovke(() => SaveClose = true);
- });
- UpLastModel();
- return;
- }
- cansave = false;
- });
- protected void UpLastModel()
- {
- lastModel = (TModel)Model.Clone();
- SaveIsEnabled = false;
- }
- public ICommand CancelCommand => new RelayCommand(Cancel);
- public virtual string Title { get => title; set => SetProperty(ref title, value); }
- public bool ButtonVisibily { get => buttonVisibily; set => SetProperty(ref buttonVisibily, value); }
- protected bool savecanclose = true;
- protected virtual void Save()
- {
- lastModel = default;
- savecanclose = true;
- }
- [AllowNull]
- private Type content;
- public virtual void InitData()
- {
- SaveClose = true;
- cansave = true;
- if (CanCancel)
- {
- lastModel = (TModel)Model.Clone();
- SaveIsEnabled = false;
- }
- else
- {
- //lastModel = default;
- }
- }
- protected virtual bool SkipProperty(string? propertyName)
- {
- return false;
- }
- protected override void OnPropertyChanged(PropertyChangedEventArgs e)
- {
- if (e.PropertyName != nameof(SaveClose)) SaveClose = true;
- base.OnPropertyChanged(e);
- if (e.PropertyName == nameof(Content)
- || e.PropertyName == nameof(Model)
- || e.PropertyName == nameof(ButtonVisibily)
- || e.PropertyName == nameof(SaveCommand)
- || e.PropertyName == nameof(Title)
- || e.PropertyName == nameof(SaveIsEnabled)
- || e.PropertyName == nameof(CancelCommand))
- return;
- if (SkipProperty(e.PropertyName)) return;
- SaveIsEnabled = true;
- }
- private bool buttonVisibily = true;
- private bool saveClose = true;
- protected virtual void Cancel()
- {
- if (CanCancel && lastModel != null)
- {
- UpDateModel(lastModel);
- lastModel = default;
- }
- }
- }
- }
|