2020-11-15 09:05:51 -05:00
|
|
|
#include "NotificationManager.h"
|
2020-03-28 14:05:28 -04:00
|
|
|
#include <cstring>
|
2020-10-20 14:57:39 -04:00
|
|
|
#include <algorithm>
|
2020-03-28 14:05:28 -04:00
|
|
|
|
|
|
|
using namespace Pinetime::Controllers;
|
|
|
|
|
2020-10-21 11:31:56 -04:00
|
|
|
constexpr uint8_t NotificationManager::MessageSize;
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
void NotificationManager::Push(NotificationManager::Notification&& notif) {
|
2020-10-20 14:57:39 -04:00
|
|
|
notif.id = GetNextId();
|
|
|
|
notif.valid = true;
|
2020-10-22 04:43:42 -04:00
|
|
|
notifications[writeIndex] = std::move(notif);
|
2020-03-28 14:05:28 -04:00
|
|
|
writeIndex = (writeIndex + 1 < TotalNbNotifications) ? writeIndex + 1 : 0;
|
2021-04-18 13:28:14 -04:00
|
|
|
if (!empty)
|
2020-10-20 14:57:39 -04:00
|
|
|
readIndex = (readIndex + 1 < TotalNbNotifications) ? readIndex + 1 : 0;
|
2021-04-18 13:28:14 -04:00
|
|
|
else
|
|
|
|
empty = false;
|
2020-10-20 14:57:39 -04:00
|
|
|
|
|
|
|
newNotification = true;
|
2020-03-28 14:05:28 -04:00
|
|
|
}
|
|
|
|
|
2020-10-20 14:57:39 -04:00
|
|
|
NotificationManager::Notification NotificationManager::GetLastNotification() {
|
2020-03-28 14:05:28 -04:00
|
|
|
NotificationManager::Notification notification = notifications[readIndex];
|
2020-10-21 16:15:02 -04:00
|
|
|
notification.index = 1;
|
2020-10-20 14:57:39 -04:00
|
|
|
return notification;
|
|
|
|
}
|
2020-03-28 14:05:28 -04:00
|
|
|
|
2020-10-20 14:57:39 -04:00
|
|
|
NotificationManager::Notification::Id NotificationManager::GetNextId() {
|
|
|
|
return nextId++;
|
|
|
|
}
|
2020-03-28 14:05:28 -04:00
|
|
|
|
2020-10-20 14:57:39 -04:00
|
|
|
NotificationManager::Notification NotificationManager::GetNext(NotificationManager::Notification::Id id) {
|
2021-04-18 13:28:14 -04:00
|
|
|
auto currentIterator = std::find_if(notifications.begin(), notifications.end(), [id](const Notification& n) {
|
|
|
|
return n.valid && n.id == id;
|
|
|
|
});
|
|
|
|
if (currentIterator == notifications.end() || currentIterator->id != id)
|
|
|
|
return Notification {};
|
2020-10-20 14:57:39 -04:00
|
|
|
|
|
|
|
auto& lastNotification = notifications[readIndex];
|
|
|
|
|
|
|
|
NotificationManager::Notification result;
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
if (currentIterator == (notifications.end() - 1))
|
2020-10-20 14:57:39 -04:00
|
|
|
result = *(notifications.begin());
|
|
|
|
else
|
2021-04-18 13:28:14 -04:00
|
|
|
result = *(currentIterator + 1);
|
2020-10-20 14:57:39 -04:00
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
if (result.id <= id)
|
|
|
|
return {};
|
2020-10-20 14:57:39 -04:00
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
result.index = (lastNotification.id - result.id) + 1;
|
2020-10-20 14:57:39 -04:00
|
|
|
return result;
|
2020-03-28 14:05:28 -04:00
|
|
|
}
|
2020-10-20 14:57:39 -04:00
|
|
|
|
|
|
|
NotificationManager::Notification NotificationManager::GetPrevious(NotificationManager::Notification::Id id) {
|
2021-04-18 13:28:14 -04:00
|
|
|
auto currentIterator = std::find_if(notifications.begin(), notifications.end(), [id](const Notification& n) {
|
|
|
|
return n.valid && n.id == id;
|
|
|
|
});
|
|
|
|
if (currentIterator == notifications.end() || currentIterator->id != id)
|
|
|
|
return Notification {};
|
2020-10-20 14:57:39 -04:00
|
|
|
|
|
|
|
auto& lastNotification = notifications[readIndex];
|
|
|
|
|
|
|
|
NotificationManager::Notification result;
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
if (currentIterator == notifications.begin())
|
|
|
|
result = *(notifications.end() - 1);
|
2020-10-20 14:57:39 -04:00
|
|
|
else
|
2021-04-18 13:28:14 -04:00
|
|
|
result = *(currentIterator - 1);
|
2020-10-20 14:57:39 -04:00
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
if (result.id >= id)
|
|
|
|
return {};
|
2020-10-20 14:57:39 -04:00
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
result.index = (lastNotification.id - result.id) + 1;
|
2020-10-20 14:57:39 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NotificationManager::AreNewNotificationsAvailable() {
|
|
|
|
return newNotification;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NotificationManager::ClearNewNotificationFlag() {
|
|
|
|
return newNotification.exchange(false);
|
|
|
|
}
|
|
|
|
|
2020-10-21 16:15:02 -04:00
|
|
|
size_t NotificationManager::NbNotifications() const {
|
2021-04-18 13:28:14 -04:00
|
|
|
return std::count_if(notifications.begin(), notifications.end(), [](const Notification& n) {
|
|
|
|
return n.valid;
|
|
|
|
});
|
2020-10-21 16:15:02 -04:00
|
|
|
}
|
|
|
|
|
2021-04-04 06:10:47 -04:00
|
|
|
const char* NotificationManager::Notification::Message() const {
|
2021-04-18 13:28:14 -04:00
|
|
|
const char* itField = std::find(message.begin(), message.begin() + size - 1, '\0');
|
|
|
|
if (itField != message.begin() + size - 1) {
|
|
|
|
const char* ptr = (itField) + 1;
|
2021-04-04 06:10:47 -04:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
return const_cast<char*>(message.data());
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* NotificationManager::Notification::Title() const {
|
2021-04-18 13:28:14 -04:00
|
|
|
const char* itField = std::find(message.begin(), message.begin() + size - 1, '\0');
|
|
|
|
if (itField != message.begin() + size - 1) {
|
2021-04-04 06:10:47 -04:00
|
|
|
return message.data();
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|