|
1 | 1 | #include <cstring> |
| 2 | +#include <algorithm> |
2 | 3 | #include "NotificationManager.h" |
3 | 4 |
|
4 | 5 | using namespace Pinetime::Controllers; |
5 | 6 |
|
6 | | -void NotificationManager::Push(Pinetime::Controllers::NotificationManager::Categories category, |
7 | | - const char *message, uint8_t currentMessageSize) { |
8 | | - // TODO handle edge cases on read/write index |
9 | | - auto checkedSize = std::min(currentMessageSize, uint8_t{18}); |
10 | | - auto& notif = notifications[writeIndex]; |
11 | | - std::memcpy(notif.message.data(), message, checkedSize); |
12 | | - notif.message[checkedSize] = '\0'; |
13 | | - notif.category = category; |
| 7 | +constexpr uint8_t NotificationManager::MessageSize; |
14 | 8 |
|
| 9 | + |
| 10 | +void NotificationManager::Push(NotificationManager::Notification &¬if) { |
| 11 | + notif.id = GetNextId(); |
| 12 | + notif.valid = true; |
| 13 | + notifications[writeIndex] = std::move(notif); |
15 | 14 | writeIndex = (writeIndex + 1 < TotalNbNotifications) ? writeIndex + 1 : 0; |
16 | | - if(!empty && writeIndex == readIndex) |
17 | | - readIndex = writeIndex + 1; |
| 15 | + if(!empty) |
| 16 | + readIndex = (readIndex + 1 < TotalNbNotifications) ? readIndex + 1 : 0; |
| 17 | + else empty = false; |
| 18 | + |
| 19 | + newNotification = true; |
18 | 20 | } |
19 | 21 |
|
20 | | -NotificationManager::Notification Pinetime::Controllers::NotificationManager::Pop() { |
21 | | -// TODO handle edge cases on read/write index |
| 22 | +NotificationManager::Notification NotificationManager::GetLastNotification() { |
22 | 23 | NotificationManager::Notification notification = notifications[readIndex]; |
| 24 | + notification.index = 1; |
| 25 | + return notification; |
| 26 | +} |
23 | 27 |
|
24 | | - if(readIndex != writeIndex) { |
25 | | - readIndex = (readIndex + 1 < TotalNbNotifications) ? readIndex + 1 : 0; |
26 | | - } |
| 28 | +NotificationManager::Notification::Id NotificationManager::GetNextId() { |
| 29 | + return nextId++; |
| 30 | +} |
27 | 31 |
|
28 | | - // TODO Check move optimization on return |
29 | | - return notification; |
| 32 | +NotificationManager::Notification NotificationManager::GetNext(NotificationManager::Notification::Id id) { |
| 33 | + auto currentIterator = std::find_if(notifications.begin(), notifications.end(), [id](const Notification& n){return n.valid && n.id == id;}); |
| 34 | + if(currentIterator == notifications.end() || currentIterator->id != id) return Notification{}; |
| 35 | + |
| 36 | + auto& lastNotification = notifications[readIndex]; |
| 37 | + |
| 38 | + NotificationManager::Notification result; |
| 39 | + |
| 40 | + if(currentIterator == (notifications.end()-1)) |
| 41 | + result = *(notifications.begin()); |
| 42 | + else |
| 43 | + result = *(currentIterator+1); |
| 44 | + |
| 45 | + if(result.id <= id) return {}; |
| 46 | + |
| 47 | + result.index = (lastNotification.id - result.id)+1; |
| 48 | + return result; |
30 | 49 | } |
| 50 | + |
| 51 | +NotificationManager::Notification NotificationManager::GetPrevious(NotificationManager::Notification::Id id) { |
| 52 | + auto currentIterator = std::find_if(notifications.begin(), notifications.end(), [id](const Notification& n){return n.valid && n.id == id;}); |
| 53 | + if(currentIterator == notifications.end() || currentIterator->id != id) return Notification{}; |
| 54 | + |
| 55 | + auto& lastNotification = notifications[readIndex]; |
| 56 | + |
| 57 | + NotificationManager::Notification result; |
| 58 | + |
| 59 | + if(currentIterator == notifications.begin()) |
| 60 | + result = *(notifications.end()-1); |
| 61 | + else |
| 62 | + result = *(currentIterator-1); |
| 63 | + |
| 64 | + if(result.id >= id) return {}; |
| 65 | + |
| 66 | + result.index = (lastNotification.id - result.id)+1; |
| 67 | + return result; |
| 68 | +} |
| 69 | + |
| 70 | +bool NotificationManager::AreNewNotificationsAvailable() { |
| 71 | + return newNotification; |
| 72 | +} |
| 73 | + |
| 74 | +bool NotificationManager::ClearNewNotificationFlag() { |
| 75 | + return newNotification.exchange(false); |
| 76 | +} |
| 77 | + |
| 78 | +size_t NotificationManager::NbNotifications() const { |
| 79 | + return std::count_if(notifications.begin(), notifications.end(), [](const Notification& n){ return n.valid;}); |
| 80 | +} |
| 81 | + |
0 commit comments