2022-03-02 00:21:29 +01:00
|
|
|
#include "notification_service.hpp"
|
2023-03-01 21:27:15 +00:00
|
|
|
|
2022-12-16 18:55:55 +01:00
|
|
|
#include "widgets/imgui_hotkey.hpp"
|
2022-03-02 00:21:29 +01:00
|
|
|
|
|
|
|
namespace big
|
|
|
|
{
|
|
|
|
notification_service::notification_service()
|
|
|
|
{
|
2023-03-01 21:27:15 +00:00
|
|
|
push("NOTIFICATION_WELCOME_TITLE"_T.data(),
|
|
|
|
std::vformat("NOTIFICATION_WELCOME_TEXT"_T, std::make_format_args(ImGui::key_names[g.settings.hotkeys.menu_toggle])));
|
2022-12-16 18:55:55 +01:00
|
|
|
|
2022-03-02 00:21:29 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-06-06 07:40:40 +00:00
|
|
|
void notification_service::push(const std::string& title, const std::string& message)
|
2022-03-02 00:21:29 +01:00
|
|
|
{
|
2023-03-01 21:27:15 +00:00
|
|
|
this->push({NotificationType::INFO, title, message, std::chrono::system_clock::now(), 5000.f, 1.f});
|
2022-03-02 00:21:29 +01:00
|
|
|
}
|
|
|
|
|
2023-06-06 07:40:40 +00:00
|
|
|
void notification_service::push_warning(const std::string& title, const std::string& message)
|
2022-03-02 00:21:29 +01:00
|
|
|
{
|
2023-03-01 21:27:15 +00:00
|
|
|
this->push({NotificationType::WARNING, title, message, std::chrono::system_clock::now(), 7000.f, 1.f});
|
2022-03-02 00:21:29 +01:00
|
|
|
}
|
|
|
|
|
2023-06-06 07:40:40 +00:00
|
|
|
void notification_service::push_error(const std::string& title, const std::string& message)
|
2022-03-02 00:21:29 +01:00
|
|
|
{
|
2023-03-01 21:27:15 +00:00
|
|
|
this->push({NotificationType::DANGER, title, message, std::chrono::system_clock::now(), 7000.f, 1.f});
|
2022-03-02 00:21:29 +01:00
|
|
|
}
|
|
|
|
|
2023-06-06 07:40:40 +00:00
|
|
|
void notification_service::push_success(const std::string& title, const std::string& message)
|
2023-06-05 15:46:20 -04:00
|
|
|
{
|
|
|
|
this->push({NotificationType::SUCCESS, title, message, std::chrono::system_clock::now(), 7000.f, 1.f});
|
|
|
|
}
|
|
|
|
|
2022-03-02 00:21:29 +01:00
|
|
|
std::vector<notification> notification_service::get()
|
|
|
|
{
|
|
|
|
std::vector<notification> notifications_to_sent;
|
|
|
|
std::vector<std::size_t> to_remove;
|
2023-03-01 21:27:15 +00:00
|
|
|
for (auto& n : this->notifications)
|
|
|
|
{
|
2022-03-02 00:21:29 +01:00
|
|
|
std::chrono::time_point<std::chrono::system_clock> curTime = std::chrono::system_clock::now();
|
2023-03-01 21:27:15 +00:00
|
|
|
const float time_diff =
|
|
|
|
(float)std::chrono::duration_cast<std::chrono::milliseconds>(curTime - n.second.created_on).count();
|
2022-03-02 00:21:29 +01:00
|
|
|
n.second.alpha = 1;
|
2023-03-01 21:27:15 +00:00
|
|
|
if (n.second.destroy_in <= time_diff)
|
|
|
|
{
|
2022-03-02 00:21:29 +01:00
|
|
|
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);
|
2023-03-01 21:27:15 +00:00
|
|
|
else
|
|
|
|
to_remove.push_back(n.first);
|
2022-03-02 00:21:29 +01:00
|
|
|
}
|
|
|
|
for (std::size_t k : to_remove)
|
|
|
|
this->notifications.erase(k);
|
|
|
|
|
|
|
|
return notifications_to_sent;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|