2022-03-02 00:21:29 +01:00
|
|
|
#include "notification_service.hpp"
|
|
|
|
|
|
|
|
namespace big
|
|
|
|
{
|
|
|
|
notification_service::notification_service()
|
|
|
|
{
|
|
|
|
g_notification_service = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
notification_service::~notification_service()
|
|
|
|
{
|
|
|
|
g_notification_service = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void notification_service::push(notification n)
|
|
|
|
{
|
2022-07-06 13:02:54 +02:00
|
|
|
this->notifications.emplace(std::hash<std::string>{}(n.message + n.title), n);
|
2022-03-02 00:21:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void notification_service::push(std::string title, std::string message)
|
|
|
|
{
|
|
|
|
this->push({ NotificationType::INFO, title, message, std::chrono::system_clock::now(), 3000.f , 1.f});
|
|
|
|
}
|
|
|
|
|
|
|
|
void notification_service::push_warning(std::string title, std::string message)
|
|
|
|
{
|
|
|
|
this->push({ NotificationType::WARNING, title, message, std::chrono::system_clock::now(), 3000.f , 1.f });
|
|
|
|
}
|
|
|
|
|
|
|
|
void notification_service::push_error(std::string title, std::string message)
|
|
|
|
{
|
|
|
|
this->push({ NotificationType::DANGER, title, message, std::chrono::system_clock::now(), 3000.f , 1.f });
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<notification> notification_service::get()
|
|
|
|
{
|
|
|
|
std::vector<notification> notifications_to_sent;
|
|
|
|
std::vector<std::size_t> to_remove;
|
|
|
|
for (auto& n : this->notifications) {
|
|
|
|
std::chrono::time_point<std::chrono::system_clock> curTime = std::chrono::system_clock::now();
|
|
|
|
const float time_diff = (float)std::chrono::duration_cast<std::chrono::milliseconds>(curTime - n.second.created_on).count();
|
|
|
|
n.second.alpha = 1;
|
|
|
|
if (n.second.destroy_in <= time_diff) {
|
|
|
|
n.second.alpha = 1.f - ((time_diff - n.second.destroy_in) / 600);
|
|
|
|
n.second.alpha = n.second.alpha < 0.f ? 0.f : n.second.alpha;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n.second.alpha > 0.f)
|
|
|
|
notifications_to_sent.push_back(n.second);
|
|
|
|
else to_remove.push_back(n.first);
|
|
|
|
}
|
|
|
|
for (std::size_t k : to_remove)
|
|
|
|
this->notifications.erase(k);
|
|
|
|
|
|
|
|
return notifications_to_sent;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|