Skip to content

Commit 22b79c8

Browse files
clemensvonmolovchigrin
authored andcommitted
minor changes to notification age logic
1 parent 72af22c commit 22b79c8

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/displayapp/screens/Notifications.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -302,20 +302,26 @@ Notifications::NotificationItem::NotificationItem(const char* title,
302302
if (title == nullptr) {
303303
lv_label_set_text_static(alert_type, "Notification");
304304
} 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";
305+
// almost impossible to receive a real notification at time 0, so skip because it is the "no notifications" notification
306+
if(timeNow != 0) {
307+
auto diff = std::chrono::system_clock::from_time_t(timeNow) - std::chrono::system_clock::from_time_t(timeArrived);
308+
std::chrono::minutes age = std::chrono::duration_cast<std::chrono::minutes>(diff);
309+
uint32_t ageInt = static_cast<uint32_t>(age.count());
310+
char timeUnit;
311+
if ((ageInt / (60 * 24)) >= 1) {
312+
ageInt /= (60*24);
313+
timeUnit = 'd';
314+
} else if ((ageInt / 60) >= 1) {
315+
ageInt /= 60;
316+
timeUnit = 'h';
317+
} else {
318+
timeUnit = 'm';
319+
}
320+
lv_obj_t* alert_age = lv_label_create(container1, nullptr);
321+
lv_label_set_text_fmt(alert_age, "%d%c ago",ageInt, timeUnit);
322+
// same format as alert_count
323+
lv_obj_align(alert_age, container1, LV_ALIGN_IN_BOTTOM_RIGHT, 0, -16);
315324
}
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);
319325

320326
// copy title to label and replace newlines with spaces
321327
lv_label_set_text(alert_type, title);

0 commit comments

Comments
 (0)