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 :ViewModelBase,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; } } } }