2020-03-28 18:05:28 +00:00
|
|
|
#include <cstring>
|
2020-10-20 18:57:39 +00:00
|
|
|
#include <algorithm>
|
2020-03-28 18:05:28 +00:00
|
|
|
#include "NotificationManager.h"
|
|
|
|
|
|
|
|
using namespace Pinetime::Controllers;
|
|
|
|
|
2020-10-21 15:31:56 +00:00
|
|
|
constexpr uint8_t NotificationManager::MessageSize;
|
|
|
|
|
|
|
|
|
2020-03-28 18:05:28 +00:00
|
|
|
void NotificationManager::Push(Pinetime::Controllers::NotificationManager::Categories category,
|
2020-06-28 09:59:14 +00:00
|
|
|
const char *message, uint8_t currentMessageSize) {
|
2020-03-28 18:05:28 +00:00
|
|
|
// TODO handle edge cases on read/write index
|
2020-10-21 15:31:56 +00:00
|
|
|
auto checkedSize = std::min(currentMessageSize, MessageSize);
|
2020-03-28 18:05:28 +00:00
|
|
|
auto& notif = notifications[writeIndex];
|
2020-06-28 09:59:14 +00:00
|
|
|
std::memcpy(notif.message.data(), message, checkedSize);
|
|
|
|
notif.message[checkedSize] = '\0';
|
2020-03-28 18:05:28 +00:00
|
|
|
notif.category = category;
|
2020-10-20 18:57:39 +00:00
|
|
|
notif.id = GetNextId();
|
|
|
|
notif.valid = true;
|
2020-03-28 18:05:28 +00:00
|
|
|
|
|
|
|
writeIndex = (writeIndex + 1 < TotalNbNotifications) ? writeIndex + 1 : 0;
|
2020-10-20 18:57:39 +00:00
|
|
|
if(!empty)
|
|
|
|
readIndex = (readIndex + 1 < TotalNbNotifications) ? readIndex + 1 : 0;
|
|
|
|
else empty = false;
|
|
|
|
|
|
|
|
newNotification = true;
|
2020-03-28 18:05:28 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 18:57:39 +00:00
|
|
|
NotificationManager::Notification NotificationManager::GetLastNotification() {
|
2020-03-28 18:05:28 +00:00
|
|
|
NotificationManager::Notification notification = notifications[readIndex];
|
2020-10-21 20:15:02 +00:00
|
|
|
notification.index = 1;
|
2020-10-20 18:57:39 +00:00
|
|
|
return notification;
|
|
|
|
}
|
2020-03-28 18:05:28 +00:00
|
|
|
|
2020-10-20 18:57:39 +00:00
|
|
|
NotificationManager::Notification::Id NotificationManager::GetNextId() {
|
|
|
|
return nextId++;
|
|
|
|
}
|
2020-03-28 18:05:28 +00:00
|
|
|
|
2020-10-20 18:57:39 +00:00
|
|
|
NotificationManager::Notification NotificationManager::GetNext(NotificationManager::Notification::Id id) {
|
|
|
|
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{};
|
|
|
|
|
|
|
|
auto& lastNotification = notifications[readIndex];
|
|
|
|
|
|
|
|
NotificationManager::Notification result;
|
|
|
|
|
|
|
|
if(currentIterator == (notifications.end()-1))
|
|
|
|
result = *(notifications.begin());
|
|
|
|
else
|
|
|
|
result = *(currentIterator+1);
|
|
|
|
|
|
|
|
if(result.id <= id) return {};
|
|
|
|
|
|
|
|
result.index = (lastNotification.id - result.id)+1;
|
|
|
|
return result;
|
2020-03-28 18:05:28 +00:00
|
|
|
}
|
2020-10-20 18:57:39 +00:00
|
|
|
|
|
|
|
NotificationManager::Notification NotificationManager::GetPrevious(NotificationManager::Notification::Id id) {
|
|
|
|
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{};
|
|
|
|
|
|
|
|
auto& lastNotification = notifications[readIndex];
|
|
|
|
|
|
|
|
NotificationManager::Notification result;
|
|
|
|
|
|
|
|
if(currentIterator == notifications.begin())
|
|
|
|
result = *(notifications.end()-1);
|
|
|
|
else
|
|
|
|
result = *(currentIterator-1);
|
|
|
|
|
|
|
|
if(result.id >= id) return {};
|
|
|
|
|
|
|
|
result.index = (lastNotification.id - result.id)+1;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NotificationManager::AreNewNotificationsAvailable() {
|
|
|
|
return newNotification;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NotificationManager::ClearNewNotificationFlag() {
|
|
|
|
return newNotification.exchange(false);
|
|
|
|
}
|
|
|
|
|
2020-10-21 20:15:02 +00:00
|
|
|
size_t NotificationManager::NbNotifications() const {
|
|
|
|
return std::count_if(notifications.begin(), notifications.end(), [](const Notification& n){ return n.valid;});
|
|
|
|
}
|
|
|
|
|