From 84661969d9c62e3bf7fbdde56b1371efb6b84ab0 Mon Sep 17 00:00:00 2001 From: Clemens von Molo Date: Wed, 17 Nov 2021 14:59:50 +0100 Subject: [PATCH 1/3] first try at adding notification age --- src/components/ble/NotificationManager.cpp | 1 + src/components/ble/NotificationManager.h | 6 ++++ src/displayapp/DisplayApp.cpp | 4 +-- src/displayapp/screens/Notifications.cpp | 32 +++++++++++++++++++++- src/displayapp/screens/Notifications.h | 6 ++++ src/main.cpp | 6 ++-- 6 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/components/ble/NotificationManager.cpp b/src/components/ble/NotificationManager.cpp index 7ffed3006e..c86339a3b5 100644 --- a/src/components/ble/NotificationManager.cpp +++ b/src/components/ble/NotificationManager.cpp @@ -9,6 +9,7 @@ constexpr uint8_t NotificationManager::MessageSize; void NotificationManager::Push(NotificationManager::Notification&& notif) { notif.id = GetNextId(); notif.valid = true; + notif.timeArrived = std::chrono::system_clock::to_time_t(dateTimeController.CurrentDateTime()); notifications[writeIndex] = std::move(notif); writeIndex = (writeIndex + 1 < TotalNbNotifications) ? writeIndex + 1 : 0; if (!empty) diff --git a/src/components/ble/NotificationManager.h b/src/components/ble/NotificationManager.h index 40f174ea25..887cae3763 100644 --- a/src/components/ble/NotificationManager.h +++ b/src/components/ble/NotificationManager.h @@ -4,6 +4,8 @@ #include #include #include +#include +#include "components/datetime/DateTimeController.h" namespace Pinetime { namespace Controllers { @@ -30,6 +32,7 @@ namespace Pinetime { bool valid = false; uint8_t index; uint8_t size; + std::time_t timeArrived; std::array message; Categories category = Categories::Unknown; @@ -38,6 +41,8 @@ namespace Pinetime { }; Notification::Id nextId {0}; + NotificationManager(Controllers::DateTime& dateTimeController) : dateTimeController {dateTimeController} { + } void Push(Notification&& notif); Notification GetLastNotification(); Notification GetNext(Notification::Id id); @@ -51,6 +56,7 @@ namespace Pinetime { size_t NbNotifications() const; private: + Controllers::DateTime& dateTimeController; Notification::Id GetNextId(); static constexpr uint8_t TotalNbNotifications = 5; std::array notifications; diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index 13ee004501..d56055099c 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -347,12 +347,12 @@ void DisplayApp::LoadApp(Apps app, DisplayApp::FullRefreshDirections direction) case Apps::Notifications: currentScreen = std::make_unique( - this, notificationManager, systemTask->nimble().alertService(), motorController, Screens::Notifications::Modes::Normal); + this, notificationManager, systemTask->nimble().alertService(), motorController, dateTimeController, Screens::Notifications::Modes::Normal); ReturnApp(Apps::Clock, FullRefreshDirections::Up, TouchEvents::SwipeUp); break; case Apps::NotificationsPreview: currentScreen = std::make_unique( - this, notificationManager, systemTask->nimble().alertService(), motorController, Screens::Notifications::Modes::Preview); + this, notificationManager, systemTask->nimble().alertService(), motorController, dateTimeController, Screens::Notifications::Modes::Preview); ReturnApp(Apps::Clock, FullRefreshDirections::Up, TouchEvents::SwipeUp); break; case Apps::Timer: diff --git a/src/displayapp/screens/Notifications.cpp b/src/displayapp/screens/Notifications.cpp index 4f4758133c..317f15094a 100644 --- a/src/displayapp/screens/Notifications.cpp +++ b/src/displayapp/screens/Notifications.cpp @@ -12,8 +12,13 @@ Notifications::Notifications(DisplayApp* app, Pinetime::Controllers::NotificationManager& notificationManager, Pinetime::Controllers::AlertNotificationService& alertNotificationService, Pinetime::Controllers::MotorController& motorController, + Pinetime::Controllers::DateTime& dateTimeController, Modes mode) - : Screen(app), notificationManager {notificationManager}, alertNotificationService {alertNotificationService}, mode {mode} { + : Screen(app), + notificationManager {notificationManager}, + alertNotificationService {alertNotificationService}, + dateTimeController {dateTimeController}, + mode {mode} { notificationManager.ClearNewNotificationFlag(); auto notification = notificationManager.GetLastNotification(); if (notification.valid) { @@ -22,6 +27,8 @@ Notifications::Notifications(DisplayApp* app, notification.Message(), notification.index, notification.category, + notification.timeArrived, + std::chrono::system_clock::to_time_t(this->dateTimeController.CurrentDateTime()), notificationManager.NbNotifications(), mode, alertNotificationService); @@ -31,6 +38,8 @@ Notifications::Notifications(DisplayApp* app, "No notification to display", 0, notification.category, + 0, + 0, notificationManager.NbNotifications(), Modes::Preview, alertNotificationService); @@ -99,6 +108,8 @@ bool Notifications::OnTouchEvent(Pinetime::Applications::TouchEvents event) { previousNotification.Message(), previousNotification.index, previousNotification.category, + previousNotification.timeArrived, + std::chrono::system_clock::to_time_t(dateTimeController.CurrentDateTime()), notificationManager.NbNotifications(), mode, alertNotificationService); @@ -124,6 +135,8 @@ bool Notifications::OnTouchEvent(Pinetime::Applications::TouchEvents event) { nextNotification.Message(), nextNotification.index, nextNotification.category, + nextNotification.timeArrived, + std::chrono::system_clock::to_time_t(dateTimeController.CurrentDateTime()), notificationManager.NbNotifications(), mode, alertNotificationService); @@ -145,6 +158,8 @@ Notifications::NotificationItem::NotificationItem(const char* title, const char* msg, uint8_t notifNr, Controllers::NotificationManager::Categories category, + std::time_t timeArrived, + std::time_t timeNow, uint8_t notifNb, Modes mode, Pinetime::Controllers::AlertNotificationService& alertNotificationService) @@ -166,6 +181,21 @@ Notifications::NotificationItem::NotificationItem(const char* title, lv_label_set_text_fmt(alert_count, "%i/%i", notifNr, notifNb); lv_obj_align(alert_count, NULL, LV_ALIGN_IN_TOP_RIGHT, 0, 16); + auto diff = std::chrono::system_clock::from_time_t(timeNow) - std::chrono::system_clock::from_time_t(timeArrived); + std::chrono::minutes age = std::chrono::duration_cast(diff); + std::string ageString; + ageString.reserve(10); + if ((age.count() / (60 * 24)) >= 1) { + ageString = std::to_string(static_cast(age.count() / (60 * 24))) + "d ago"; + } else if ((age.count() / 60) >= 1) { + ageString = std::to_string(static_cast(age.count() / 60)) + "h ago"; + } else { + ageString = std::to_string(static_cast(age.count())) + "m ago"; + } + lv_obj_t* alert_age = lv_label_create(lv_scr_act(), nullptr); + lv_label_set_text(alert_age, ageString.c_str()); + lv_obj_align(alert_age, container1, LV_ALIGN_IN_BOTTOM_RIGHT, 0, 0); + lv_obj_t* alert_type = lv_label_create(lv_scr_act(), nullptr); lv_obj_set_style_local_text_color(alert_type, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x888888)); if (title == nullptr) diff --git a/src/displayapp/screens/Notifications.h b/src/displayapp/screens/Notifications.h index 0b5271e72f..6f7f4bff34 100644 --- a/src/displayapp/screens/Notifications.h +++ b/src/displayapp/screens/Notifications.h @@ -3,9 +3,11 @@ #include #include #include +#include #include "Screen.h" #include "components/ble/NotificationManager.h" #include "components/motor/MotorController.h" +#include "components/datetime/DateTimeController.h" namespace Pinetime { namespace Controllers { @@ -21,6 +23,7 @@ namespace Pinetime { Pinetime::Controllers::NotificationManager& notificationManager, Pinetime::Controllers::AlertNotificationService& alertNotificationService, Pinetime::Controllers::MotorController& motorController, + Pinetime::Controllers::DateTime& dateTimeController, Modes mode); ~Notifications() override; @@ -33,6 +36,8 @@ namespace Pinetime { const char* msg, uint8_t notifNr, Controllers::NotificationManager::Categories, + std::time_t timeArrived, + std::time_t timeNow, uint8_t notifNb, Modes mode, Pinetime::Controllers::AlertNotificationService& alertNotificationService); @@ -62,6 +67,7 @@ namespace Pinetime { }; Pinetime::Controllers::NotificationManager& notificationManager; Pinetime::Controllers::AlertNotificationService& alertNotificationService; + Pinetime::Controllers::DateTime& dateTimeController; Modes mode = Modes::Normal; std::unique_ptr currentItem; Controllers::NotificationManager::Notification::Id currentId; diff --git a/src/main.cpp b/src/main.cpp index 53f78ce8a1..ce7166d4a3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -104,7 +104,7 @@ Pinetime::Applications::HeartRateTask heartRateApp(heartRateSensor, heartRateCon Pinetime::Controllers::DateTime dateTimeController; Pinetime::Drivers::Watchdog watchdog; Pinetime::Drivers::WatchdogView watchdogView(watchdog); -Pinetime::Controllers::NotificationManager notificationManager; +Pinetime::Controllers::NotificationManager notificationManager {dateTimeController}; Pinetime::Controllers::MotionController motionController; Pinetime::Controllers::TimerController timerController; Pinetime::Controllers::AlarmController alarmController {dateTimeController}; @@ -156,7 +156,7 @@ Pinetime::System::SystemTask systemTask(spi, touchHandler, buttonHandler); -/* Variable Declarations for variables in noinit SRAM +/* Variable Declarations for variables in noinit SRAM Increment NoInit_MagicValue upon adding variables to this area */ extern uint32_t __start_noinit_data; @@ -324,7 +324,7 @@ int main(void) { // retrieve version stored by bootloader Pinetime::BootloaderVersion::SetVersion(NRF_TIMER2->CC[0]); - + if (NoInit_MagicWord == NoInit_MagicValue) { dateTimeController.SetCurrentTime(NoInit_BackUpTime); } else { From 1de1d44f057729c5ce15a8dbeea8fd4b270871b1 Mon Sep 17 00:00:00 2001 From: Clemens von Molo Date: Tue, 28 Dec 2021 22:58:48 +0100 Subject: [PATCH 2/3] minor changes to notification age logic --- src/displayapp/screens/Notifications.cpp | 33 ++++++++++++++---------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/displayapp/screens/Notifications.cpp b/src/displayapp/screens/Notifications.cpp index 317f15094a..8b82b73031 100644 --- a/src/displayapp/screens/Notifications.cpp +++ b/src/displayapp/screens/Notifications.cpp @@ -180,21 +180,26 @@ Notifications::NotificationItem::NotificationItem(const char* title, lv_obj_t* alert_count = lv_label_create(lv_scr_act(), 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); - - auto diff = std::chrono::system_clock::from_time_t(timeNow) - std::chrono::system_clock::from_time_t(timeArrived); - std::chrono::minutes age = std::chrono::duration_cast(diff); - std::string ageString; - ageString.reserve(10); - if ((age.count() / (60 * 24)) >= 1) { - ageString = std::to_string(static_cast(age.count() / (60 * 24))) + "d ago"; - } else if ((age.count() / 60) >= 1) { - ageString = std::to_string(static_cast(age.count() / 60)) + "h ago"; - } else { - ageString = std::to_string(static_cast(age.count())) + "m ago"; + // almost impossible to receive a real notification at time 0, so skip because it is the "no notifications" notification + if(timeNow != 0) { + auto diff = std::chrono::system_clock::from_time_t(timeNow) - std::chrono::system_clock::from_time_t(timeArrived); + std::chrono::minutes age = std::chrono::duration_cast(diff); + uint32_t ageInt = static_cast(age.count()); + char timeUnit; + if ((ageInt / (60 * 24)) >= 1) { + ageInt /= (60*24); + timeUnit = 'd'; + } else if ((ageInt / 60) >= 1) { + ageInt /= 60; + timeUnit = 'h'; + } else { + timeUnit = 'm'; + } + lv_obj_t* alert_age = lv_label_create(container1, nullptr); + lv_label_set_text_fmt(alert_age, "%d%c ago",ageInt, timeUnit); + // same format as alert_count + lv_obj_align(alert_age, container1, LV_ALIGN_IN_BOTTOM_RIGHT, 0, -16); } - lv_obj_t* alert_age = lv_label_create(lv_scr_act(), nullptr); - lv_label_set_text(alert_age, ageString.c_str()); - lv_obj_align(alert_age, container1, LV_ALIGN_IN_BOTTOM_RIGHT, 0, 0); lv_obj_t* alert_type = lv_label_create(lv_scr_act(), nullptr); lv_obj_set_style_local_text_color(alert_type, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x888888)); From afe96d533c530b42d4edb81e7e712ca04a6904c6 Mon Sep 17 00:00:00 2001 From: Clemens von Molo Date: Thu, 28 Jul 2022 18:53:12 +0200 Subject: [PATCH 3/3] fix outdated constructor call for NotificationItem --- src/displayapp/screens/Notifications.cpp | 38 +++++++++++++----------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/displayapp/screens/Notifications.cpp b/src/displayapp/screens/Notifications.cpp index d55ff7673a..a1c3ab5c54 100644 --- a/src/displayapp/screens/Notifications.cpp +++ b/src/displayapp/screens/Notifications.cpp @@ -109,6 +109,8 @@ void Notifications::Refresh() { notification.Message(), currentIdx + 1, notification.category, + notification.timeArrived, + std::chrono::system_clock::to_time_t(this->dateTimeController.CurrentDateTime()), notificationManager.NbNotifications(), alertNotificationService, motorController); @@ -283,24 +285,24 @@ Notifications::NotificationItem::NotificationItem(const char* title, lv_label_set_text_fmt(alert_count, "%i/%i", notifNr, notifNb); lv_obj_align(alert_count, NULL, LV_ALIGN_IN_TOP_RIGHT, 0, 16); // almost impossible to receive a real notification at time 0, so skip because it is the "no notifications" notification - if(timeNow != 0) { - auto diff = std::chrono::system_clock::from_time_t(timeNow) - std::chrono::system_clock::from_time_t(timeArrived); - std::chrono::minutes age = std::chrono::duration_cast(diff); - uint32_t ageInt = static_cast(age.count()); - char timeUnit; - if ((ageInt / (60 * 24)) >= 1) { - ageInt /= (60*24); - timeUnit = 'd'; - } else if ((ageInt / 60) >= 1) { - ageInt /= 60; - timeUnit = 'h'; - } else { - timeUnit = 'm'; - } - lv_obj_t* alert_age = lv_label_create(container1, nullptr); - lv_label_set_text_fmt(alert_age, "%d%c ago",ageInt, timeUnit); - // same format as alert_count - lv_obj_align(alert_age, container1, LV_ALIGN_IN_BOTTOM_RIGHT, 0, -16); + if (timeNow != 0) { + auto diff = std::chrono::system_clock::from_time_t(timeNow) - std::chrono::system_clock::from_time_t(timeArrived); + std::chrono::minutes age = std::chrono::duration_cast(diff); + uint32_t ageInt = static_cast(age.count()); + char timeUnit; + if ((ageInt / (60 * 24)) >= 1) { + ageInt /= (60 * 24); + timeUnit = 'd'; + } else if ((ageInt / 60) >= 1) { + ageInt /= 60; + timeUnit = 'h'; + } else { + timeUnit = 'm'; + } + lv_obj_t* alert_age = lv_label_create(container, nullptr); + lv_label_set_text_fmt(alert_age, "%d%c ago", ageInt, timeUnit); + // same format as alert_count + lv_obj_align(alert_age, container, LV_ALIGN_IN_BOTTOM_RIGHT, 0, -16); } lv_obj_t* alert_type = lv_label_create(container, nullptr);