diff options
author | JF <jf@codingfield.com> | 2020-10-20 20:57:39 +0200 |
---|---|---|
committer | JF <jf@codingfield.com> | 2020-10-20 20:57:39 +0200 |
commit | ef5670c7e09a1a63fc5df4a066472ed7fe7550ff (patch) | |
tree | 3f7999c0dd4c4cfc42a04f7904205ffd914cc225 /src/components/ble | |
parent | 55427d83b87960903cd6213ac9c73e694ee547bd (diff) |
Integrate new notification UI with notifications coming from BLE
Diffstat (limited to 'src/components/ble')
-rw-r--r-- | src/components/ble/NotificationManager.cpp | 68 | ||||
-rw-r--r-- | src/components/ble/NotificationManager.h | 15 |
2 files changed, 73 insertions, 10 deletions
diff --git a/src/components/ble/NotificationManager.cpp b/src/components/ble/NotificationManager.cpp index 0aea0697..4297cde9 100644 --- a/src/components/ble/NotificationManager.cpp +++ b/src/components/ble/NotificationManager.cpp @@ -1,4 +1,5 @@ #include <cstring> +#include <algorithm> #include "NotificationManager.h" using namespace Pinetime::Controllers; @@ -11,20 +12,69 @@ void NotificationManager::Push(Pinetime::Controllers::NotificationManager::Categ std::memcpy(notif.message.data(), message, checkedSize); notif.message[checkedSize] = '\0'; notif.category = category; + notif.id = GetNextId(); + notif.valid = true; 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]; + 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); +} + diff --git a/src/components/ble/NotificationManager.h b/src/components/ble/NotificationManager.h index daa1571b..6bf689a8 100644 --- a/src/components/ble/NotificationManager.h +++ b/src/components/ble/NotificationManager.h @@ -1,6 +1,7 @@ #pragma once #include <array> +#include <atomic> namespace Pinetime { namespace Controllers { @@ -10,20 +11,32 @@ namespace Pinetime { static constexpr uint8_t MessageSize{18}; struct Notification { + using Id = uint8_t; + Id id; + bool valid = false; + uint8_t index; + uint8_t number = TotalNbNotifications; std::array<char, MessageSize+1> message; Categories category = Categories::Unknown; }; + Notification::Id nextId {0}; void Push(Categories category, const char* message, uint8_t messageSize); - Notification Pop(); + Notification GetLastNotification(); + Notification GetNext(Notification::Id id); + Notification GetPrevious(Notification::Id id); + bool ClearNewNotificationFlag(); + bool AreNewNotificationsAvailable(); private: + Notification::Id GetNextId(); static constexpr uint8_t TotalNbNotifications = 5; std::array<Notification, TotalNbNotifications> notifications; uint8_t readIndex = 0; uint8_t writeIndex = 0; bool empty = true; + std::atomic<bool> newNotification{false}; }; } }
\ No newline at end of file |