Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notification age (x minutes ago) on notifcation screen #835

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components/ble/NotificationManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions src/components/ble/NotificationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <chrono>
#include "components/datetime/DateTimeController.h"

namespace Pinetime {
namespace Controllers {
Expand All @@ -30,6 +32,7 @@ namespace Pinetime {
bool valid = false;
uint8_t index;
uint8_t size;
std::time_t timeArrived;
std::array<char, MessageSize + 1> message;
Categories category = Categories::Unknown;

Expand All @@ -38,6 +41,8 @@ namespace Pinetime {
};
Notification::Id nextId {0};

NotificationManager(Controllers::DateTime& dateTimeController) : dateTimeController {dateTimeController} {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can even store a const reference, to make it explicit, that a notification can't change the date-time, just get the current date and time

Suggested change
NotificationManager(Controllers::DateTime& dateTimeController) : dateTimeController {dateTimeController} {
NotificationManager(const Controllers::DateTime& dateTimeController) : dateTimeController {dateTimeController} {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At present it is not possible since DateTime controller updates it member variables during time retrieving.

std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> DateTime::CurrentDateTime() {
xSemaphoreTake(mutex, portMAX_DELAY);
UpdateTime(nrf_rtc_counter_get(portNRF_RTC_REG), false);
xSemaphoreGive(mutex);
return currentDateTime;
}
void DateTime::UpdateTime(uint32_t systickCounter, bool forceUpdate) {
// Handle systick counter overflow
uint32_t systickDelta = 0;
if (systickCounter < previousSystickCounter) {
systickDelta = static_cast<uint32_t>(portNRF_RTC_MAXTICKS) - previousSystickCounter;
systickDelta += systickCounter + 1;
} else {
systickDelta = systickCounter - previousSystickCounter;
}
auto correctedDelta = systickDelta / configTICK_RATE_HZ;
// If a second hasn't passed, there is nothing to do
// If the time has been changed, set forceUpdate to trigger internal state updates
if (correctedDelta == 0 && !forceUpdate) {
return;
}
auto rest = systickDelta % configTICK_RATE_HZ;
if (systickCounter >= rest) {
previousSystickCounter = systickCounter - rest;
} else {
previousSystickCounter = static_cast<uint32_t>(portNRF_RTC_MAXTICKS) - (rest - systickCounter - 1);
}
currentDateTime += std::chrono::seconds(correctedDelta);

It can be overcomed with mutable but I am not sure whether this is a good idea.

}
void Push(Notification&& notif);
Notification GetLastNotification();
Notification GetNext(Notification::Id id);
Expand All @@ -51,6 +56,7 @@ namespace Pinetime {
size_t NbNotifications() const;

private:
Controllers::DateTime& dateTimeController;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can even store a const reference, to make it explicit, that a notification can't change the date-time, just get the current date and time

Suggested change
Controllers::DateTime& dateTimeController;
const Controllers::DateTime& dateTimeController;

Notification::Id GetNextId();
static constexpr uint8_t TotalNbNotifications = 5;
std::array<Notification, TotalNbNotifications> notifications;
Expand Down
4 changes: 2 additions & 2 deletions src/displayapp/DisplayApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,12 +347,12 @@ void DisplayApp::LoadApp(Apps app, DisplayApp::FullRefreshDirections direction)

case Apps::Notifications:
currentScreen = std::make_unique<Screens::Notifications>(
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<Screens::Notifications>(
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:
Expand Down
32 changes: 31 additions & 1 deletion src/displayapp/screens/Notifications.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -31,6 +38,8 @@ Notifications::Notifications(DisplayApp* app,
"No notification to display",
0,
notification.category,
0,
0,
notificationManager.NbNotifications(),
Modes::Preview,
alertNotificationService);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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)
Expand All @@ -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<std::chrono::minutes>(diff);
std::string ageString;
ageString.reserve(10);
if ((age.count() / (60 * 24)) >= 1) {
ageString = std::to_string(static_cast<uint16_t>(age.count() / (60 * 24))) + "d ago";
} else if ((age.count() / 60) >= 1) {
ageString = std::to_string(static_cast<uint8_t>(age.count() / 60)) + "h ago";
} else {
ageString = std::to_string(static_cast<uint8_t>(age.count())) + "m ago";
}
Riksu9000 marked this conversation as resolved.
Show resolved Hide resolved
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);
Riksu9000 marked this conversation as resolved.
Show resolved Hide resolved

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)
Expand Down
6 changes: 6 additions & 0 deletions src/displayapp/screens/Notifications.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
#include <lvgl/lvgl.h>
#include <cstdint>
#include <memory>
#include <chrono>
#include "Screen.h"
#include "components/ble/NotificationManager.h"
#include "components/motor/MotorController.h"
#include "components/datetime/DateTimeController.h"

namespace Pinetime {
namespace Controllers {
Expand All @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -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<NotificationItem> currentItem;
Controllers::NotificationManager::Notification::Id currentId;
Expand Down
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down