0
0
mirror of https://github.com/sp-tarkov/launcher.git synced 2025-02-13 09:50:43 -05:00

147 lines
3.8 KiB
C#
Raw Permalink Normal View History

2023-03-03 19:25:33 +00:00
/* NotificationQueue.cs
* License: NCSA Open Source License
*
2024-05-21 20:15:19 +01:00
* Copyright: SPT
2023-03-03 19:25:33 +00:00
* AUTHORS:
* waffle.lord
*/
2024-05-21 20:15:19 +01:00
using SPT.Launcher.Helpers;
2023-03-03 19:25:33 +00:00
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Timers;
using SPT.Launcher.Utilities;
2023-03-03 19:25:33 +00:00
2024-05-21 20:15:19 +01:00
namespace SPT.Launcher.Models.Launcher.Notifications
2023-03-03 19:25:33 +00:00
{
public class NotificationQueue : NotifyPropertyChangedBase, IDisposable
2023-03-03 19:25:33 +00:00
{
public Timer queueTimer = new Timer();
private Timer animateChangeTimer = new Timer(230);
private Timer animateCloseTimer = new Timer(230);
public ObservableCollection<NotificationItem> queue { get; set; } = [];
2023-03-03 19:25:33 +00:00
private bool _showBanner;
2023-03-03 19:25:33 +00:00
public bool ShowBanner
{
get => _showBanner;
set => SetProperty(ref _showBanner, value);
2023-03-03 19:25:33 +00:00
}
public NotificationQueue(int showTimeInMilliseconds)
2023-03-03 19:25:33 +00:00
{
ShowBanner = false;
queueTimer.Interval = showTimeInMilliseconds;
2023-03-03 19:25:33 +00:00
queueTimer.Elapsed += QueueTimer_Elapsed;
animateChangeTimer.Elapsed += AnimateChange_Elapsed;
animateCloseTimer.Elapsed += AnimateCloseTimer_Elapsed;
}
private void AnimateCloseTimer_Elapsed(object sender, ElapsedEventArgs e)
{
animateCloseTimer.Stop();
queue.Clear();
queueTimer.Stop();
}
public void CloseQueue()
{
ShowBanner = false;
animateCloseTimer.Start();
}
private void CheckAndShowNotifications()
{
if (!queueTimer.Enabled)
{
ShowBanner = true;
queueTimer.Start();
}
}
public void Enqueue(string message, bool autoNext = false, bool noDefaultButton = false)
2023-03-03 19:25:33 +00:00
{
if (queue.All(x => x.Message != message))
2023-03-03 19:25:33 +00:00
{
if (noDefaultButton)
2023-03-03 19:25:33 +00:00
{
queue.Add(new NotificationItem(message));
2023-03-03 19:25:33 +00:00
}
else
{
queue.Add(new NotificationItem(message, LocalizationProvider.Instance.ok, () => { }));
2023-03-03 19:25:33 +00:00
}
CheckAndShowNotifications();
if (autoNext && queue.Count == 2)
2023-03-03 19:25:33 +00:00
{
Next(true);
}
}
}
public void Enqueue(string message, string buttonText, Action buttonAction, bool allowNext = false)
2023-03-03 19:25:33 +00:00
{
if (queue.All(x=>x.Message != message && x.ButtonText != buttonText))
2023-03-03 19:25:33 +00:00
{
queue.Add(new NotificationItem(message, buttonText, buttonAction));
2023-03-03 19:25:33 +00:00
CheckAndShowNotifications();
if (allowNext && queue.Count == 2)
2023-03-03 19:25:33 +00:00
{
Next(true);
}
}
}
public void Next(bool resetTimer = false)
2023-03-03 19:25:33 +00:00
{
if (queue.Count - 1 <= 0)
{
CloseQueue();
return;
}
if (resetTimer)
2023-03-03 19:25:33 +00:00
{
queueTimer.Stop();
queueTimer.Start();
}
ShowBanner = false;
animateChangeTimer.Start();
}
private void QueueTimer_Elapsed(object sender, ElapsedEventArgs e)
{
Next();
}
private void AnimateChange_Elapsed(object sender, ElapsedEventArgs e)
{
animateChangeTimer.Stop();
if (queue.Count > 0)
{
queue.RemoveAt(0);
}
ShowBanner = true;
}
public void Dispose()
{
queueTimer.Dispose();
animateChangeTimer.Dispose();
animateCloseTimer.Dispose();
}
}
}