using Avalonia.Controls; using CommunityToolkit.Mvvm.Input; using Shaker.Models; using SukiUI.Dialogs; using SukiUI.Toasts; using System; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Threading.Tasks; using System.Windows.Input; namespace ShakerApp.ViewModels; public abstract class DisplayViewModelBase : ViewModelBase where TModel : IModel { public DisplayViewModelBase() : base() { } public DisplayViewModelBase(TModel model):base(model) { } private string Caption => (string)(App.Current?.FindResource("PromptTitle") ?? ""); private string Yes => (string)(App.Current?.FindResource("PromptYes") ?? ""); private string No => (string)(App.Current?.FindResource("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 Tools.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 Tools.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 Tools.DispatherInovke.Inovke(()=> DialogManager.CreateDialog() .OfType(Avalonia.Controls.Notifications.NotificationType.Information) .WithTitle(Caption) .WithContent(msg) .WithActionButton (Yes, _ => { }, true).TryShow()); } public bool ShowSuccess(string msg) { return Tools.DispatherInovke.Inovke(()=> DialogManager.CreateDialog() .OfType(Avalonia.Controls.Notifications.NotificationType.Success) .WithTitle(Caption) .WithContent(msg) .WithActionButton (Yes, _ => { }, true).TryShow()); } public bool ShowWarning(string msg) { return Tools.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; Tools.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 => "Save"; public virtual string CancelContent =>"Cancel"; 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(() => { Tools.DispatherInovke.Inovke(() => SaveClose = true); }); return; } cansave = 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); } private 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; } } }