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.
This commit is contained in:
Reinhold Gschweicher 2022-01-29 23:30:03 +01:00 committed by JF
parent 29f0bce46b
commit a29e30c187
2 changed files with 11 additions and 12 deletions

View file

@ -198,15 +198,18 @@ Notifications::NotificationItem::NotificationItem(const char* title,
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)
title = "Notification";
char* pchar;
pchar = strchr(title, '\n');
while (pchar != nullptr) {
*pchar = ' ';
pchar = strchr(pchar + 1, '\n');
if(title == nullptr) {
lv_label_set_text_static(alert_type, "Notification");
} else {
// copy title to label and replace newlines with spaces
lv_label_set_text(alert_type, title);
char *pchar = strchr(lv_label_get_text(alert_type), '\n');
while (pchar != nullptr) {
*pchar = ' ';
pchar = strchr(pchar + 1, '\n');
}
lv_label_refr_text(alert_type);
}
lv_label_set_text(alert_type, title);
lv_label_set_long_mode(alert_type, LV_LABEL_LONG_SROLL_CIRC);
lv_obj_set_width(alert_type, 180);
lv_obj_align(alert_type, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 16);

View file

@ -62,10 +62,6 @@ namespace Pinetime {
};
private:
struct NotificationData {
const char* title;
const char* text;
};
Pinetime::Controllers::NotificationManager& notificationManager;
Pinetime::Controllers::AlertNotificationService& alertNotificationService;
Pinetime::Controllers::MotorController& motorController;