Dismiss notifications by swiping right

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: https://github.com/InfiniTimeOrg/InfiniTime/issues/176

Co-authored-by: Simon Willshire <me@simonwillshire.com>
Co-authored-by: NeroBurner <pyro4hell@gmail.com>
This commit is contained in:
Simon Willshire 2022-05-19 13:59:09 -04:00 committed by Reinhold Gschweicher
parent 955dda60c5
commit b93438302e
4 changed files with 204 additions and 106 deletions

View File

@ -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;
}
NotificationManager::Notification::Id NotificationManager::GetNextId() {
return nextId++;
}
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)
if (this->IsEmpty()) {
return {};
result.index = (lastNotification.id - result.id) + 1;
return result;
}
return this->At(0);
}
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 {};
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);
}
auto& lastNotification = notifications[readIndex];
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);
}
NotificationManager::Notification result;
if (currentIterator == notifications.begin())
result = *(notifications.end() - 1);
else
result = *(currentIterator - 1);
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);
}
bool NotificationManager::AreNewNotificationsAvailable() {
NotificationManager::Notification NotificationManager::GetPrevious(NotificationManager::Notification::Id idx) const {
if (static_cast<size_t>(idx + 1) >= notifications.size()) {
return {};
}
return this->At(idx + 1);
}
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() const {
return newNotification;
}
@ -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 {

View File

@ -26,9 +26,7 @@ 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;
@ -36,27 +34,31 @@ namespace Pinetime {
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};
};
}

View File

@ -3,6 +3,7 @@
#include "components/ble/MusicService.h"
#include "components/ble/AlertNotificationService.h"
#include "displayapp/screens/Symbols.h"
#include <algorithm>
using namespace Pinetime::Applications::Screens;
extern lv_font_t jetbrains_mono_extrabold_compressed;
@ -20,13 +21,14 @@ Notifications::Notifications(DisplayApp* app,
motorController {motorController},
systemTask {systemTask},
mode {mode} {
notificationManager.ClearNewNotificationFlag();
auto notification = notificationManager.GetLastNotification();
currentId = 0;
if (notification.valid) {
currentId = notification.id;
currentItem = std::make_unique<NotificationItem>(notification.Title(),
notification.Message(),
notification.index,
1,
notification.category,
notificationManager.NbNotifications(),
mode,
@ -34,14 +36,7 @@ Notifications::Notifications(DisplayApp* app,
motorController);
validDisplay = true;
} else {
currentItem = std::make_unique<NotificationItem>("Notification",
"No notification to display",
0,
notification.category,
notificationManager.NbNotifications(),
Modes::Preview,
alertNotificationService,
motorController);
currentItem = std::make_unique<NotificationItem>(notification.category, alertNotificationService, motorController);
}
if (mode == Modes::Preview) {
@ -77,7 +72,7 @@ Notifications::~Notifications() {
void Notifications::Refresh() {
if (mode == Modes::Preview && timeoutLine != nullptr) {
TickType_t tick = xTaskGetTickCount();
int32_t pos = 240 - ((tick - timeoutTickCountStart) / (timeoutLength / 240));
int32_t pos = LV_HOR_RES - ((tick - timeoutTickCountStart) / (timeoutLength / LV_HOR_RES));
if (pos <= 0) {
running = false;
} else {
@ -85,6 +80,36 @@ void Notifications::Refresh() {
lv_line_set_points(timeoutLine, timeoutLinePoints, 2);
}
}
if (currentItem != nullptr && currentItem->AnimationElapsed()) {
auto notification = notificationManager.At(currentId);
if (!notification.valid) {
notification = notificationManager.GetLastNotification();
currentId = 0;
}
if (!notification.valid) {
validDisplay = false;
}
currentItem.reset(nullptr);
app->SetFullRefresh(DisplayApp::FullRefreshDirections::Up);
if (validDisplay) {
currentItem = std::make_unique<NotificationItem>(notification.Title(),
notification.Message(),
currentId + 1,
notification.category,
notificationManager.NbNotifications(),
mode,
alertNotificationService,
motorController);
} else {
currentItem = std::make_unique<NotificationItem>(notification.category, alertNotificationService, motorController);
currentId = 0;
}
}
running = currentItem->IsRunning() && running;
}
@ -108,23 +133,39 @@ bool Notifications::OnTouchEvent(Pinetime::Applications::TouchEvents event) {
}
switch (event) {
case Pinetime::Applications::TouchEvents::SwipeRight:
if (validDisplay) {
notificationManager.Dismiss(currentId);
if (currentId > 0 && currentId == notificationManager.NbNotifications()) {
// dismissed last message (like 5/5), need to go one message down (like 4/4)
--currentId;
}
currentItem->AnimateDismiss();
return true;
}
return false;
case Pinetime::Applications::TouchEvents::SwipeDown: {
Controllers::NotificationManager::Notification previousNotification;
if (validDisplay)
if (validDisplay) {
previousNotification = notificationManager.GetPrevious(currentId);
else
if (previousNotification.valid) {
currentId = std::min(static_cast<uint8_t>(currentId + 1), static_cast<uint8_t>(notificationManager.NbNotifications() - 1));
}
} else {
previousNotification = notificationManager.GetLastNotification();
currentId = 0;
}
if (!previousNotification.valid)
if (!previousNotification.valid) {
return true;
}
validDisplay = true;
currentId = previousNotification.id;
currentItem.reset(nullptr);
app->SetFullRefresh(DisplayApp::FullRefreshDirections::Down);
currentItem = std::make_unique<NotificationItem>(previousNotification.Title(),
previousNotification.Message(),
previousNotification.index,
currentId + 1,
previousNotification.category,
notificationManager.NbNotifications(),
mode,
@ -145,12 +186,13 @@ bool Notifications::OnTouchEvent(Pinetime::Applications::TouchEvents event) {
}
validDisplay = true;
currentId = nextNotification.id;
if (currentId > 0)
--currentId;
currentItem.reset(nullptr);
app->SetFullRefresh(DisplayApp::FullRefreshDirections::Up);
currentItem = std::make_unique<NotificationItem>(nextNotification.Title(),
nextNotification.Message(),
nextNotification.index,
currentId + 1,
nextNotification.category,
notificationManager.NbNotifications(),
mode,
@ -170,6 +212,19 @@ namespace {
}
}
Notifications::NotificationItem::NotificationItem(Controllers::NotificationManager::Categories category,
Pinetime::Controllers::AlertNotificationService& alertNotificationService,
Pinetime::Controllers::MotorController& motorController)
: NotificationItem("Notification",
"No notification to display",
0,
category,
0,
Modes::Preview,
alertNotificationService,
motorController) {
}
Notifications::NotificationItem::NotificationItem(const char* title,
const char* msg,
uint8_t notifNr,
@ -179,24 +234,36 @@ Notifications::NotificationItem::NotificationItem(const char* title,
Pinetime::Controllers::AlertNotificationService& alertNotificationService,
Pinetime::Controllers::MotorController& motorController)
: mode {mode}, alertNotificationService {alertNotificationService}, motorController {motorController} {
lv_obj_t* container1 = lv_cont_create(lv_scr_act(), NULL);
lv_obj_set_style_local_bg_color(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x38, 0x38, 0x38));
lv_obj_set_style_local_pad_all(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 10);
lv_obj_set_style_local_pad_inner(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 5);
lv_obj_set_style_local_border_width(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
container = lv_cont_create(lv_scr_act(), nullptr);
lv_obj_set_size(container, LV_HOR_RES, LV_VER_RES);
lv_obj_set_style_local_bg_color(container, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
lv_obj_set_style_local_pad_all(container, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
lv_obj_set_style_local_pad_inner(container, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
lv_obj_set_style_local_border_width(container, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
lv_obj_set_pos(container1, 0, 50);
lv_obj_set_size(container1, LV_HOR_RES, 190);
subject_container = lv_cont_create(container, nullptr);
lv_obj_set_style_local_bg_color(subject_container, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x38, 0x38, 0x38));
lv_obj_set_style_local_pad_all(subject_container, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 10);
lv_obj_set_style_local_pad_inner(subject_container, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 5);
lv_obj_set_style_local_border_width(subject_container, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
lv_cont_set_layout(container1, LV_LAYOUT_COLUMN_LEFT);
lv_cont_set_fit(container1, LV_FIT_NONE);
lv_obj_set_pos(subject_container, 0, 50);
lv_obj_set_size(subject_container, LV_HOR_RES, LV_VER_RES - 50);
lv_cont_set_layout(subject_container, LV_LAYOUT_COLUMN_LEFT);
lv_cont_set_fit(subject_container, LV_FIT_NONE);
lv_obj_t* alert_count = lv_label_create(lv_scr_act(), nullptr);
lv_anim_init(&dismissAnim);
lv_anim_set_exec_cb(&dismissAnim, (lv_anim_exec_xcb_t) lv_obj_set_x);
lv_anim_set_var(&dismissAnim, container);
lv_anim_set_time(&dismissAnim, dismissAnimLength);
lv_anim_set_values(&dismissAnim, 0, LV_HOR_RES);
lv_obj_t* alert_count = lv_label_create(container, nullptr);
lv_label_set_text_fmt(alert_count, "%i/%i", notifNr, notifNb);
lv_obj_align(alert_count, NULL, LV_ALIGN_IN_TOP_RIGHT, 0, 16);
lv_obj_t* alert_type = lv_label_create(lv_scr_act(), nullptr);
lv_obj_t* alert_type = lv_label_create(container, nullptr);
lv_obj_set_style_local_text_color(alert_type, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xb0, 0xb0, 0xb0));
if (title == nullptr) {
lv_label_set_text_static(alert_type, "Notification");
@ -217,27 +284,27 @@ Notifications::NotificationItem::NotificationItem(const char* title,
/////////
switch (category) {
default: {
lv_obj_t* alert_subject = lv_label_create(container1, nullptr);
lv_obj_t* alert_subject = lv_label_create(subject_container, nullptr);
lv_obj_set_style_local_text_color(alert_subject, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xff, 0xb0, 0x0));
lv_label_set_long_mode(alert_subject, LV_LABEL_LONG_BREAK);
lv_obj_set_width(alert_subject, LV_HOR_RES - 20);
lv_label_set_text(alert_subject, msg);
} break;
case Controllers::NotificationManager::Categories::IncomingCall: {
lv_obj_set_height(container1, 108);
lv_obj_t* alert_subject = lv_label_create(container1, nullptr);
lv_obj_set_height(subject_container, 108);
lv_obj_t* alert_subject = lv_label_create(subject_container, nullptr);
lv_obj_set_style_local_text_color(alert_subject, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xff, 0xb0, 0x0));
lv_label_set_long_mode(alert_subject, LV_LABEL_LONG_BREAK);
lv_obj_set_width(alert_subject, LV_HOR_RES - 20);
lv_label_set_text_static(alert_subject, "Incoming call from");
lv_obj_t* alert_caller = lv_label_create(container1, nullptr);
lv_obj_t* alert_caller = lv_label_create(subject_container, nullptr);
lv_obj_align(alert_caller, alert_subject, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
lv_label_set_long_mode(alert_caller, LV_LABEL_LONG_BREAK);
lv_obj_set_width(alert_caller, LV_HOR_RES - 20);
lv_label_set_text(alert_caller, msg);
bt_accept = lv_btn_create(lv_scr_act(), nullptr);
bt_accept = lv_btn_create(container, nullptr);
bt_accept->user_data = this;
lv_obj_set_event_cb(bt_accept, CallEventHandler);
lv_obj_set_size(bt_accept, 76, 76);
@ -246,7 +313,7 @@ Notifications::NotificationItem::NotificationItem(const char* title,
lv_label_set_text_static(label_accept, Symbols::phone);
lv_obj_set_style_local_bg_color(bt_accept, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x0, 0xb0, 0x0));
bt_reject = lv_btn_create(lv_scr_act(), nullptr);
bt_reject = lv_btn_create(container, nullptr);
bt_reject->user_data = this;
lv_obj_set_event_cb(bt_reject, CallEventHandler);
lv_obj_set_size(bt_reject, 76, 76);
@ -255,7 +322,7 @@ Notifications::NotificationItem::NotificationItem(const char* title,
lv_label_set_text_static(label_reject, Symbols::phoneSlash);
lv_obj_set_style_local_bg_color(bt_reject, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_RED);
bt_mute = lv_btn_create(lv_scr_act(), nullptr);
bt_mute = lv_btn_create(container, nullptr);
bt_mute->user_data = this;
lv_obj_set_event_cb(bt_mute, CallEventHandler);
lv_obj_set_size(bt_mute, 76, 76);
@ -285,6 +352,20 @@ void Notifications::NotificationItem::OnCallButtonEvent(lv_obj_t* obj, lv_event_
running = false;
}
void Notifications::NotificationItem::AnimateDismiss() {
dismissAnimTickCount = xTaskGetTickCount();
lv_anim_start(&dismissAnim);
}
bool Notifications::NotificationItem::AnimationElapsed() {
bool elapsed = dismissAnimTickCount != 0 && xTaskGetTickCount() > dismissAnimTickCount + dismissAnimLength;
if (elapsed)
dismissAnimTickCount = 0;
return elapsed;
}
Notifications::NotificationItem::~NotificationItem() {
lv_obj_clean(lv_scr_act());
}

View File

@ -33,6 +33,9 @@ namespace Pinetime {
class NotificationItem {
public:
NotificationItem(Controllers::NotificationManager::Categories,
Pinetime::Controllers::AlertNotificationService& alertNotificationService,
Pinetime::Controllers::MotorController& motorController);
NotificationItem(const char* title,
const char* msg,
uint8_t notifNr,
@ -46,9 +49,12 @@ namespace Pinetime {
return running;
}
void OnCallButtonEvent(lv_obj_t*, lv_event_t event);
void AnimateDismiss();
bool AnimationElapsed();
private:
lv_obj_t* container1;
lv_obj_t* container;
lv_obj_t* subject_container;
lv_obj_t* bt_accept;
lv_obj_t* bt_mute;
lv_obj_t* bt_reject;
@ -58,6 +64,11 @@ namespace Pinetime {
Modes mode;
Pinetime::Controllers::AlertNotificationService& alertNotificationService;
Pinetime::Controllers::MotorController& motorController;
lv_anim_t dismissAnim;
TickType_t dismissAnimTickCount;
static const TickType_t dismissAnimLength = pdMS_TO_TICKS(300);
bool running = true;
};
@ -74,6 +85,7 @@ namespace Pinetime {
lv_point_t timeoutLinePoints[2] {{0, 1}, {239, 1}};
lv_obj_t* timeoutLine = nullptr;
TickType_t timeoutTickCountStart;
static const TickType_t timeoutLength = pdMS_TO_TICKS(7000);
bool interacted = true;