Skip to content

Commit a29e30c

Browse files
NeroBurnerJF002
authored andcommitted
Notifications: replace newlines in label-copy because of const char* title
The variable `title` is defined as `const char*`, which means, that `strchr()` returns a `const char*` as well according to https://www.cplusplus.com/reference/cstring/strchr/ But in the same line the return value is assigned to a non-const `char*`, which shouldn't be allowed (error with `-pedantic`). Because the `lv_label` creates an internal copy of the title sting, just modify that one instead and replace newline in the copied string.
1 parent 29f0bce commit a29e30c

2 files changed

Lines changed: 11 additions & 12 deletions

File tree

src/displayapp/screens/Notifications.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,18 @@ Notifications::NotificationItem::NotificationItem(const char* title,
198198

199199
lv_obj_t* alert_type = lv_label_create(lv_scr_act(), nullptr);
200200
lv_obj_set_style_local_text_color(alert_type, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x888888));
201-
if (title == nullptr)
202-
title = "Notification";
203-
char* pchar;
204-
pchar = strchr(title, '\n');
205-
while (pchar != nullptr) {
206-
*pchar = ' ';
207-
pchar = strchr(pchar + 1, '\n');
201+
if(title == nullptr) {
202+
lv_label_set_text_static(alert_type, "Notification");
203+
} else {
204+
// copy title to label and replace newlines with spaces
205+
lv_label_set_text(alert_type, title);
206+
char *pchar = strchr(lv_label_get_text(alert_type), '\n');
207+
while (pchar != nullptr) {
208+
*pchar = ' ';
209+
pchar = strchr(pchar + 1, '\n');
210+
}
211+
lv_label_refr_text(alert_type);
208212
}
209-
lv_label_set_text(alert_type, title);
210213
lv_label_set_long_mode(alert_type, LV_LABEL_LONG_SROLL_CIRC);
211214
lv_obj_set_width(alert_type, 180);
212215
lv_obj_align(alert_type, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 16);

src/displayapp/screens/Notifications.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@ namespace Pinetime {
6262
};
6363

6464
private:
65-
struct NotificationData {
66-
const char* title;
67-
const char* text;
68-
};
6965
Pinetime::Controllers::NotificationManager& notificationManager;
7066
Pinetime::Controllers::AlertNotificationService& alertNotificationService;
7167
Pinetime::Controllers::MotorController& motorController;

0 commit comments

Comments
 (0)