Skip to content

Commit 72af22c

Browse files
clemensvonmolovchigrin
authored andcommitted
first try at adding notification age
1 parent b8c51ab commit 72af22c

File tree

6 files changed

+44
-1
lines changed

6 files changed

+44
-1
lines changed

src/components/ble/NotificationManager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ constexpr uint8_t NotificationManager::MessageSize;
1010
void NotificationManager::Push(NotificationManager::Notification&& notif) {
1111
notif.id = GetNextId();
1212
notif.valid = true;
13+
notif.timeArrived = std::chrono::system_clock::to_time_t(dateTimeController.CurrentDateTime());
1314
newNotification = true;
1415
if (beginIdx > 0) {
1516
--beginIdx;

src/components/ble/NotificationManager.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <atomic>
55
#include <cstddef>
66
#include <cstdint>
7+
#include <chrono>
8+
#include "components/datetime/DateTimeController.h"
79

810
namespace Pinetime {
911
namespace Controllers {
@@ -28,6 +30,7 @@ namespace Pinetime {
2830
using Id = uint8_t;
2931
using Idx = uint8_t;
3032

33+
std::time_t timeArrived;
3134
std::array<char, MessageSize + 1> message;
3235
uint8_t size;
3336
Categories category = Categories::Unknown;
@@ -38,6 +41,9 @@ namespace Pinetime {
3841
const char* Title() const;
3942
};
4043

44+
NotificationManager(Controllers::DateTime& dateTimeController) : dateTimeController {dateTimeController} {
45+
}
46+
4147
void Push(Notification&& notif);
4248
Notification GetLastNotification() const;
4349
Notification Get(Notification::Id id) const;
@@ -61,6 +67,7 @@ namespace Pinetime {
6167

6268
private:
6369
Notification::Id nextId {0};
70+
Controllers::DateTime& dateTimeController;
6471
Notification::Id GetNextId();
6572
const Notification& At(Notification::Idx idx) const;
6673
Notification& At(Notification::Idx idx);

src/displayapp/DisplayApp.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ void DisplayApp::LoadScreen(Apps app, DisplayApp::FullRefreshDirections directio
562562
notificationManager,
563563
systemTask->nimble().alertService(),
564564
motorController,
565+
dateTimeController,
565566
*systemTask,
566567
Screens::Notifications::Modes::Normal);
567568
break;
@@ -570,6 +571,7 @@ void DisplayApp::LoadScreen(Apps app, DisplayApp::FullRefreshDirections directio
570571
notificationManager,
571572
systemTask->nimble().alertService(),
572573
motorController,
574+
dateTimeController,
573575
*systemTask,
574576
Screens::Notifications::Modes::Preview);
575577
break;

src/displayapp/screens/Notifications.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ Notifications::Notifications(DisplayApp* app,
1414
Pinetime::Controllers::NotificationManager& notificationManager,
1515
Pinetime::Controllers::AlertNotificationService& alertNotificationService,
1616
Pinetime::Controllers::MotorController& motorController,
17+
Pinetime::Controllers::DateTime& dateTimeController,
1718
System::SystemTask& systemTask,
1819
Modes mode)
1920
: app {app},
2021
notificationManager {notificationManager},
2122
alertNotificationService {alertNotificationService},
2223
motorController {motorController},
24+
dateTimeController {dateTimeController},
2325
wakeLock(systemTask),
2426
mode {mode} {
2527

@@ -31,6 +33,8 @@ Notifications::Notifications(DisplayApp* app,
3133
notification.Message(),
3234
1,
3335
notification.category,
36+
notification.timeArrived,
37+
std::chrono::system_clock::to_time_t(this->dateTimeController.CurrentDateTime()),
3438
notificationManager.NbNotifications(),
3539
alertNotificationService,
3640
motorController);
@@ -200,6 +204,8 @@ bool Notifications::OnTouchEvent(Pinetime::Applications::TouchEvents event) {
200204
previousNotification.Message(),
201205
currentIdx + 1,
202206
previousNotification.category,
207+
previousNotification.timeArrived,
208+
std::chrono::system_clock::to_time_t(dateTimeController.CurrentDateTime()),
203209
notificationManager.NbNotifications(),
204210
alertNotificationService,
205211
motorController);
@@ -227,6 +233,8 @@ bool Notifications::OnTouchEvent(Pinetime::Applications::TouchEvents event) {
227233
nextNotification.Message(),
228234
currentIdx + 1,
229235
nextNotification.category,
236+
nextNotification.timeArrived,
237+
std::chrono::system_clock::to_time_t(dateTimeController.CurrentDateTime()),
230238
notificationManager.NbNotifications(),
231239
alertNotificationService,
232240
motorController);
@@ -251,6 +259,8 @@ Notifications::NotificationItem::NotificationItem(Pinetime::Controllers::AlertNo
251259
0,
252260
Controllers::NotificationManager::Categories::Unknown,
253261
0,
262+
0,
263+
0,
254264
alertNotificationService,
255265
motorController) {
256266
}
@@ -259,6 +269,8 @@ Notifications::NotificationItem::NotificationItem(const char* title,
259269
const char* msg,
260270
uint8_t notifNr,
261271
Controllers::NotificationManager::Categories category,
272+
std::time_t timeArrived,
273+
std::time_t timeNow,
262274
uint8_t notifNb,
263275
Pinetime::Controllers::AlertNotificationService& alertNotificationService,
264276
Pinetime::Controllers::MotorController& motorController)
@@ -290,6 +302,21 @@ Notifications::NotificationItem::NotificationItem(const char* title,
290302
if (title == nullptr) {
291303
lv_label_set_text_static(alert_type, "Notification");
292304
} else {
305+
auto diff = std::chrono::system_clock::from_time_t(timeNow) - std::chrono::system_clock::from_time_t(timeArrived);
306+
std::chrono::minutes age = std::chrono::duration_cast<std::chrono::minutes>(diff);
307+
std::string ageString;
308+
ageString.reserve(10);
309+
if ((age.count() / (60 * 24)) >= 1) {
310+
ageString = std::to_string(static_cast<uint16_t>(age.count() / (60 * 24))) + "d ago";
311+
} else if ((age.count() / 60) >= 1) {
312+
ageString = std::to_string(static_cast<uint8_t>(age.count() / 60)) + "h ago";
313+
} else {
314+
ageString = std::to_string(static_cast<uint8_t>(age.count())) + "m ago";
315+
}
316+
lv_obj_t* alert_age = lv_label_create(lv_scr_act(), nullptr);
317+
lv_label_set_text(alert_age, ageString.c_str());
318+
lv_obj_align(alert_age, container1, LV_ALIGN_IN_BOTTOM_RIGHT, 0, 0);
319+
293320
// copy title to label and replace newlines with spaces
294321
lv_label_set_text(alert_type, title);
295322
char* pchar = strchr(lv_label_get_text(alert_type), '\n');

src/displayapp/screens/Notifications.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
#include <lvgl/lvgl.h>
44
#include <FreeRTOS.h>
5+
#include <chrono>
56
#include <cstdint>
67
#include <memory>
78
#include "displayapp/screens/Screen.h"
89
#include "components/ble/NotificationManager.h"
10+
#include "components/datetime/DateTimeController.h"
911
#include "components/motor/MotorController.h"
1012
#include "systemtask/SystemTask.h"
1113
#include "systemtask/WakeLock.h"
@@ -25,6 +27,7 @@ namespace Pinetime {
2527
Pinetime::Controllers::NotificationManager& notificationManager,
2628
Pinetime::Controllers::AlertNotificationService& alertNotificationService,
2729
Pinetime::Controllers::MotorController& motorController,
30+
Pinetime::Controllers::DateTime& dateTimeController,
2831
System::SystemTask& systemTask,
2932
Modes mode);
3033
~Notifications() override;
@@ -43,6 +46,8 @@ namespace Pinetime {
4346
const char* msg,
4447
uint8_t notifNr,
4548
Controllers::NotificationManager::Categories,
49+
std::time_t timeArrived,
50+
std::time_t timeNow,
4651
uint8_t notifNb,
4752
Pinetime::Controllers::AlertNotificationService& alertNotificationService,
4853
Pinetime::Controllers::MotorController& motorController);
@@ -74,6 +79,7 @@ namespace Pinetime {
7479
Pinetime::Controllers::NotificationManager& notificationManager;
7580
Pinetime::Controllers::AlertNotificationService& alertNotificationService;
7681
Pinetime::Controllers::MotorController& motorController;
82+
Pinetime::Controllers::DateTime& dateTimeController;
7783
System::WakeLock wakeLock;
7884
Modes mode = Modes::Normal;
7985
std::unique_ptr<NotificationItem> currentItem;

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ Pinetime::Controllers::MotorController motorController {};
102102

103103
Pinetime::Controllers::DateTime dateTimeController {settingsController};
104104
Pinetime::Drivers::Watchdog watchdog;
105-
Pinetime::Controllers::NotificationManager notificationManager;
105+
Pinetime::Controllers::NotificationManager notificationManager {dateTimeController};
106106
Pinetime::Controllers::MotionController motionController;
107107
Pinetime::Controllers::AlarmController alarmController {dateTimeController, fs};
108108
Pinetime::Controllers::TouchHandler touchHandler;

0 commit comments

Comments
 (0)