69 lines
2 KiB
C#
69 lines
2 KiB
C#
|
|
using System.Windows.Threading;
|
||
|
|
|
||
|
|
namespace TeamsISO.App.ViewModels;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Lightweight transient-notification view-model. The main view holds a single
|
||
|
|
/// instance bound to a small overlay at the bottom of the content area.
|
||
|
|
/// <see cref="Show"/> displays a message for a fixed duration before auto-hiding;
|
||
|
|
/// successive Show calls reset the timer instead of stacking, so the user always
|
||
|
|
/// sees the most recent action's feedback.
|
||
|
|
/// </summary>
|
||
|
|
public sealed class ToastViewModel : ObservableObject
|
||
|
|
{
|
||
|
|
private readonly DispatcherTimer _hideTimer;
|
||
|
|
private string _message = string.Empty;
|
||
|
|
private bool _isVisible;
|
||
|
|
private string _accent = "Wd.Accent.Cyan";
|
||
|
|
|
||
|
|
public ToastViewModel(Dispatcher dispatcher)
|
||
|
|
{
|
||
|
|
_hideTimer = new DispatcherTimer(DispatcherPriority.Background, dispatcher)
|
||
|
|
{
|
||
|
|
Interval = TimeSpan.FromSeconds(3),
|
||
|
|
};
|
||
|
|
_hideTimer.Tick += (_, _) =>
|
||
|
|
{
|
||
|
|
_hideTimer.Stop();
|
||
|
|
IsVisible = false;
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
public string Message
|
||
|
|
{
|
||
|
|
get => _message;
|
||
|
|
private set => SetField(ref _message, value);
|
||
|
|
}
|
||
|
|
|
||
|
|
public bool IsVisible
|
||
|
|
{
|
||
|
|
get => _isVisible;
|
||
|
|
private set => SetField(ref _isVisible, value);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Brush resource key for the accent dot. "Wd.Accent.Cyan" for success-style
|
||
|
|
/// (default), "Wd.Accent.Coral" for warnings.
|
||
|
|
/// </summary>
|
||
|
|
public string Accent
|
||
|
|
{
|
||
|
|
get => _accent;
|
||
|
|
private set => SetField(ref _accent, value);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Show a success-style toast for ~3 seconds.</summary>
|
||
|
|
public void Show(string message) => ShowImpl(message, "Wd.Accent.Cyan");
|
||
|
|
|
||
|
|
/// <summary>Show a warning-style toast (coral accent) for ~3 seconds.</summary>
|
||
|
|
public void Warn(string message) => ShowImpl(message, "Wd.Accent.Coral");
|
||
|
|
|
||
|
|
private void ShowImpl(string message, string accentKey)
|
||
|
|
{
|
||
|
|
Message = message;
|
||
|
|
Accent = accentKey;
|
||
|
|
IsVisible = true;
|
||
|
|
_hideTimer.Stop();
|
||
|
|
_hideTimer.Start();
|
||
|
|
}
|
||
|
|
}
|