Skip to content

Commit

Permalink
Dismiss notifications by swiping right
Browse files Browse the repository at this point in the history
Rework notification ring buffer to have a begin-idx and a size_
internally. And add  a new interface to access the notifications by
index. The `0` index is always the latest notification.

The second new function is a `Dismiss` function to remove the
notification with a specified idx from the buffer.

Fixes: #176

Co-authored-by: Simon Willshire <me@simonwillshire.com>
Co-authored-by: NeroBurner <pyro4hell@gmail.com>
  • Loading branch information
Simon Willshire and NeroBurner committed Jun 5, 2022
1 parent f95147c commit 3cf9e98
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 105 deletions.
113 changes: 58 additions & 55 deletions src/components/ble/NotificationManager.cpp
Original file line number Diff line number Diff line change
@@ -1,81 +1,86 @@
#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;
notifications[writeIndex] = std::move(notif);
writeIndex = (writeIndex + 1 < TotalNbNotifications) ? writeIndex + 1 : 0;
if (!empty)
readIndex = (readIndex + 1 < TotalNbNotifications) ? readIndex + 1 : 0;
else
empty = false;

newNotification = true;
if (begin_idx > 0) {
--begin_idx;
} else {
begin_idx = notifications.size() - 1;
}
notifications[begin_idx] = std::move(notif);
if (size_ < notifications.size()) {
size_++;
}
}

NotificationManager::Notification NotificationManager::GetLastNotification() {
NotificationManager::Notification notification = notifications[readIndex];
notification.index = 1;
return notification;
if (this->IsEmpty()) {
return {};
}
return this->At(0);
}

NotificationManager::Notification::Id NotificationManager::GetNextId() {
return nextId++;
const NotificationManager::Notification& NotificationManager::At(NotificationManager::Notification::Id idx) const {
if (idx >= notifications.size()) {
assert(false);
return notifications.at(begin_idx); // this should not happen
}
size_t read_idx = (begin_idx + idx) % notifications.size();
return notifications.at(read_idx);
}

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);
NotificationManager::Notification& NotificationManager::At(NotificationManager::Notification::Id idx) {
if (idx >= notifications.size()) {
assert(false);
return notifications.at(begin_idx); // this should not happen
}
size_t read_idx = (begin_idx + idx) % notifications.size();
return notifications.at(read_idx);
}

if (result.id <= id)
NotificationManager::Notification NotificationManager::GetNext(NotificationManager::Notification::Id idx) const {
if (idx == 0 || idx > notifications.size()) {
return {};

result.index = (lastNotification.id - result.id) + 1;
return result;
}
return this->At(idx - 1);
}

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)
NotificationManager::Notification NotificationManager::GetPrevious(NotificationManager::Notification::Id idx) const {
if (static_cast<size_t>(idx + 1) >= notifications.size()) {
return {};
}
return this->At(idx + 1);
}

result.index = (lastNotification.id - result.id) + 1;
return result;
void NotificationManager::Dismiss(NotificationManager::Notification::Id 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(begin_idx).valid = false;
begin_idx = (begin_idx + 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_;
}

bool NotificationManager::AreNewNotificationsAvailable() {
bool NotificationManager::AreNewNotificationsAvailable() const {
return newNotification;
}

Expand All @@ -84,9 +89,7 @@ bool NotificationManager::ClearNewNotificationFlag() {
}

size_t NotificationManager::NbNotifications() const {
return std::count_if(notifications.begin(), notifications.end(), [](const Notification& n) {
return n.valid;
});
return size_;
}

const char* NotificationManager::Notification::Message() const {
Expand Down
22 changes: 12 additions & 10 deletions src/components/ble/NotificationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,39 @@ namespace Pinetime {

struct Notification {
using Id = uint8_t;
Id id;
bool valid = false;
uint8_t index;
uint8_t size;
std::array<char, MessageSize + 1> message;
Categories category = Categories::Unknown;

const char* Message() const;
const char* Title() const;
};
Notification::Id nextId {0};

void Push(Notification&& notif);
Notification GetLastNotification();
Notification GetNext(Notification::Id id);
Notification GetPrevious(Notification::Id id);
const Notification& At(Notification::Id id) const;
Notification& At(Notification::Id id);
Notification GetNext(Notification::Id id) const;
Notification GetPrevious(Notification::Id id) const;
bool ClearNewNotificationFlag();
bool AreNewNotificationsAvailable();
bool AreNewNotificationsAvailable() const;
void Dismiss(Notification::Id id);

static constexpr size_t MaximumMessageSize() {
return MessageSize;
};
bool IsEmpty() const {
return size_ == 0;
}
size_t NbNotifications() const;

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;
size_t begin_idx = TotalNbNotifications - 1; // index of the newest notification
size_t size_ = 0; // number of valid notifications in buffer

std::atomic<bool> newNotification {false};
};
}
Expand Down
Loading

0 comments on commit 3cf9e98

Please sign in to comment.