Files
SteamTools/System.Application.SteamTools.Client.Desktop/Services/Static/ToastService.cs
T
2021-03-07 17:10:38 +08:00

73 lines
1.4 KiB
C#

using ReactiveUI;
using System.Reactive.Linq;
using System.Reactive.Subjects;
namespace System.Application.Services
{
/// <summary>
/// 提供对显示在主窗口底部的状态栏的访问。
/// </summary>
public class ToastService : ReactiveObject
{
#region static members
public static ToastService Current { get; } = new ToastService();
#endregion
private readonly Subject<string> notifier;
private string persisitentMessage = "";
private string notificationMessage;
#region Message 変更通知
/// <summary>
/// 获取指示当前状态的字符串。
/// </summary>
public string Message
{
get { return this.notificationMessage ?? this.persisitentMessage; }
set
{
if (this.persisitentMessage != value)
{
this.persisitentMessage = value;
this.RaisePropertyChanged();
}
}
}
#endregion
private ToastService()
{
this.notifier = new Subject<string>();
this.notifier
.Do(x =>
{
this.notificationMessage = x;
this.RaiseMessagePropertyChanged();
})
.Throttle(TimeSpan.FromMilliseconds(5000))
.Subscribe(_ =>
{
this.notificationMessage = null;
this.RaiseMessagePropertyChanged();
});
}
public void Set(string message)
{
this.Message = message;
}
public void Notify(string message)
{
this.notifier.OnNext(message);
}
private void RaiseMessagePropertyChanged()
{
this.RaisePropertyChanged(nameof(this.Message));
}
}
}