summaryrefslogtreecommitdiff
path: root/src/components/ble/NotificationManager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/ble/NotificationManager.cpp')
-rw-r--r--src/components/ble/NotificationManager.cpp85
1 files changed, 68 insertions, 17 deletions
diff --git a/src/components/ble/NotificationManager.cpp b/src/components/ble/NotificationManager.cpp
index 0aea0697..67711723 100644
--- a/src/components/ble/NotificationManager.cpp
+++ b/src/components/ble/NotificationManager.cpp
@@ -1,30 +1,81 @@
#include <cstring>
+#include <algorithm>
#include "NotificationManager.h"
using namespace Pinetime::Controllers;
-void NotificationManager::Push(Pinetime::Controllers::NotificationManager::Categories category,
- const char *message, uint8_t currentMessageSize) {
- // TODO handle edge cases on read/write index
- auto checkedSize = std::min(currentMessageSize, uint8_t{18});
- auto& notif = notifications[writeIndex];
- std::memcpy(notif.message.data(), message, checkedSize);
- notif.message[checkedSize] = '\0';
- notif.category = category;
+constexpr uint8_t NotificationManager::MessageSize;
+
+void NotificationManager::Push(NotificationManager::Notification &&notif) {
+ notif.id = GetNextId();
+ notif.valid = true;
+ notifications[writeIndex] = std::move(notif);
writeIndex = (writeIndex + 1 < TotalNbNotifications) ? writeIndex + 1 : 0;
- if(!empty && writeIndex == readIndex)
- readIndex = writeIndex + 1;
+ if(!empty)
+ readIndex = (readIndex + 1 < TotalNbNotifications) ? readIndex + 1 : 0;
+ else empty = false;
+
+ newNotification = true;
}
-NotificationManager::Notification Pinetime::Controllers::NotificationManager::Pop() {
-// TODO handle edge cases on read/write index
+NotificationManager::Notification NotificationManager::GetLastNotification() {
NotificationManager::Notification notification = notifications[readIndex];
+ notification.index = 1;
+ return notification;
+}
- if(readIndex != writeIndex) {
- readIndex = (readIndex + 1 < TotalNbNotifications) ? readIndex + 1 : 0;
- }
+NotificationManager::Notification::Id NotificationManager::GetNextId() {
+ return nextId++;
+}
- // TODO Check move optimization on return
- return notification;
+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;
}
+
+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);
+}
+
+size_t NotificationManager::NbNotifications() const {
+ return std::count_if(notifications.begin(), notifications.end(), [](const Notification& n){ return n.valid;});
+}
+