-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathNotificationManager.cpp
More file actions
180 lines (159 loc) · 5.48 KB
/
Copy pathNotificationManager.cpp
File metadata and controls
180 lines (159 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "components/ble/NotificationManager.h"
#include <cstring>
#include <algorithm>
#include <cassert>
using namespace Pinetime::Controllers;
constexpr uint8_t NotificationManager::MessageSize;
void NotificationManager::Push(NotificationManager::Notification&& notif) {
notif.id = GetNextId();
notif.valid = true;
newNotification = true;
if (beginIdx > 0) {
--beginIdx;
} else {
beginIdx = notifications.size() - 1;
}
notifications[beginIdx] = std::move(notif);
if (size < notifications.size()) {
size++;
}
}
NotificationManager::Notification::Id NotificationManager::GetNextId() {
return nextId++;
}
NotificationManager::Notification NotificationManager::GetLastNotification() const {
if (this->IsEmpty()) {
return {};
}
return this->At(0);
}
const NotificationManager::Notification& NotificationManager::At(NotificationManager::Notification::Idx idx) const {
if (idx >= notifications.size()) {
assert(false);
return notifications.at(beginIdx); // this should not happen
}
size_t read_idx = (beginIdx + idx) % notifications.size();
return notifications.at(read_idx);
}
NotificationManager::Notification& NotificationManager::At(NotificationManager::Notification::Idx idx) {
if (idx >= notifications.size()) {
assert(false);
return notifications.at(beginIdx); // this should not happen
}
size_t read_idx = (beginIdx + idx) % notifications.size();
return notifications.at(read_idx);
}
NotificationManager::Notification::Idx NotificationManager::IndexOf(NotificationManager::Notification::Id id) const {
for (NotificationManager::Notification::Idx idx = 0; idx < this->size; idx++) {
const NotificationManager::Notification& notification = this->At(idx);
if (notification.id == id) {
return idx;
}
}
return size;
}
NotificationManager::Notification NotificationManager::Get(NotificationManager::Notification::Id id) const {
NotificationManager::Notification::Idx idx = this->IndexOf(id);
if (idx == this->size) {
return {};
}
return this->At(idx);
}
NotificationManager::Notification NotificationManager::GetNext(NotificationManager::Notification::Id id) const {
NotificationManager::Notification::Idx idx = this->IndexOf(id);
if (idx == this->size) {
return {};
}
if (idx == 0 || idx > notifications.size()) {
return {};
}
return this->At(idx - 1);
}
NotificationManager::Notification NotificationManager::GetPrevious(NotificationManager::Notification::Id id) const {
NotificationManager::Notification::Idx idx = this->IndexOf(id);
if (idx == this->size) {
return {};
}
if (static_cast<size_t>(idx + 1) >= notifications.size()) {
return {};
}
return this->At(idx + 1);
}
void NotificationManager::DismissIdx(NotificationManager::Notification::Idx idx) {
if (this->IsEmpty()) {
return;
}
if (idx >= size) {
assert(false);
return; // this should not happen
}
if (idx == 0) { // just remove the first element, don't need to change the other elements
notifications.at(beginIdx).valid = false;
beginIdx = (beginIdx + 1) % notifications.size();
} else {
// overwrite the specified entry by moving all later messages one index to the front
for (size_t i = idx; i < size - 1; ++i) {
this->At(i) = this->At(i + 1);
}
this->At(size - 1).valid = false;
}
--size;
}
void NotificationManager::Dismiss(NotificationManager::Notification::Id id) {
NotificationManager::Notification::Idx idx = this->IndexOf(id);
if (idx == this->size) {
return;
}
this->DismissIdx(idx);
}
bool NotificationManager::AreNewNotificationsAvailable() const {
return newNotification;
}
bool NotificationManager::ClearNewNotificationFlag() {
return newNotification.exchange(false);
}
size_t NotificationManager::NbNotifications() const {
return size;
}
NotificationManager::Notification::Notification() {
}
/**
* Make a Notification from const char* buffer.
*
* @param message the const char* buffer with the message
* @param size the size of complete message, including the final 0x00 byte
*/
NotificationManager::Notification::Notification(const char* message, uint8_t size) {
uint8_t effectiveSize = std::min(std::max(size, uint8_t {1}), NotificationManager::MessageSize);
memcpy(this->message.data(), message, effectiveSize - 1);
this->message[effectiveSize - 1] = '\0';
this->size = effectiveSize;
}
/**
* Make a Notification from a value in const struct os_mbuf* buffer.
*
* @param om the const struct os_mbuf* buffer containing the message
* @param off the offset of the message - passed to os_mbuf_copydata()
* @param size the size of complete message, including the final 0x00 byte
*/
NotificationManager::Notification::Notification(const struct os_mbuf* om, int off, uint8_t size) {
uint8_t effectiveSize = std::min(std::max(size, uint8_t {1}), NotificationManager::MessageSize);
os_mbuf_copydata(om, off, effectiveSize - 1, this->message.data());
this->message[effectiveSize - 1] = '\0';
this->size = effectiveSize;
}
const char* NotificationManager::Notification::Message() const {
const char* itField = std::find(message.begin(), message.begin() + size - 1, '\0');
if (itField != message.begin() + size - 1) {
const char* ptr = (itField) + 1;
return ptr;
}
return const_cast<char*>(message.data());
}
const char* NotificationManager::Notification::Title() const {
const char* itField = std::find(message.begin(), message.begin() + size - 1, '\0');
if (itField != message.begin() + size - 1) {
return message.data();
}
return {};
}