Added the possibility to set the notification's displayed hour format (12H/24H)

This commit is contained in:
Anatole SCHRAMM 2023-10-25 13:39:06 +02:00
parent 6b06e28394
commit 06c02f76ba
2 changed files with 33 additions and 6 deletions

View File

@ -16,7 +16,7 @@ void _notification_list_debug(NotificationDataList_t notificationList);
void _display_message_notification(NotificationScreen_t * const notificationScreen, NotificationData_t *notification);
void _notification_popup_destroy(NotificationScreen_t * const notificationScreen);
const char *_notification_type_to_char(NotificationType_e notificationType);
const char *_notification_timestamp_to_date(time_t timestamp);
const char *_notification_timestamp_to_date(time_t timestamp, bool hour_24H_format);
static void notification_scrolled_event_cb(lv_event_t *e)
{
@ -68,6 +68,17 @@ void notification_screen_register_on_state_change_cb(NotificationScreen_t * cons
notificationScreen->notificationOnStateChangeCb = notificationOnStateChangeCb;
}
void notification_screen_set_displayed_hour_format(NotificationScreen_t * const notificationScreen, bool hour_24H_format)
{
if(!notificationScreen)
{
LV_LOG_ERROR("NULL pointer given !");
return;
}
notificationScreen->notification_hour_24H_format = hour_24H_format;
}
void notification_screen_notify(NotificationScreen_t * const notificationScreen, uint32_t handle, time_t dateOfArrival, NotificationType_e notificationType, char * title, char * body)
{
if(!notificationScreen)
@ -324,7 +335,7 @@ void _display_message_notification(NotificationScreen_t * const notificationScre
lv_obj_set_style_pad_right(notificationScreen->date_label, 10, LV_PART_MAIN);
lv_obj_set_style_text_color(notificationScreen->date_label, lv_color_white(), LV_PART_MAIN);
lv_obj_align(notificationScreen->date_label, LV_ALIGN_TOP_RIGHT, 0, 0);
lv_label_set_text_static(notificationScreen->date_label, _notification_timestamp_to_date(notification->dateOfArrival));
lv_label_set_text_static(notificationScreen->date_label, _notification_timestamp_to_date(notification->dateOfArrival, notificationScreen->notification_hour_24H_format));
//Create the sub-area in the notification
lv_obj_t *sub_area = lv_obj_create(main_notification);
@ -359,7 +370,7 @@ void _display_message_notification(NotificationScreen_t * const notificationScre
//We just have to update the notification content
lv_label_set_text_static(notificationScreen->type_label, _notification_type_to_char(notification->type));
lv_label_set_text_static(notificationScreen->title_label, notification->title);
lv_label_set_text_static(notificationScreen->date_label, _notification_timestamp_to_date(notification->dateOfArrival));
lv_label_set_text_static(notificationScreen->date_label, _notification_timestamp_to_date(notification->dateOfArrival, notificationScreen->notification_hour_24H_format));
lv_label_set_text_static(notificationScreen->body_label, notification->body);
}
}
@ -392,12 +403,24 @@ const char *_notification_type_to_char(NotificationType_e notificationType)
}
}
const char *_notification_timestamp_to_date(time_t timestamp)
const char *_notification_timestamp_to_date(time_t timestamp, bool hour_24H_format)
{
static char date[9]; //Ex 7:23PM
struct tm *time = gmtime(&timestamp);
sprintf(date, "%s%d:%s%d", time->tm_hour < 10 ? "0" : "", time->tm_hour, time->tm_min < 10 ? "0" : "", time->tm_min);
if(hour_24H_format)
sprintf(date, "%s%d:%s%d", time->tm_hour < 10 ? "0" : "", time->tm_hour, time->tm_min < 10 ? "0" : "", time->tm_min);
else
{
// Intermediate hour var to set it on a 12H format
uint8_t hour = time->tm_hour;
if(hour > 12)hour -= 12;
sprintf(date, "%d:%s%d%s", hour,
time->tm_min < 10 ? "0" : "", time->tm_min,
time->tm_hour < 13 ? "AM" : "PM");
}
return date;
}

View File

@ -57,13 +57,17 @@ typedef struct NotificationScreen
NotificationDataList_t notificationList; // Actual notifications
NotificationDataList_t freeNotificationList; // Free notification object pool
// Miscellaneous
bool new_notification_available;
bool new_notification_available : 1;
bool notification_hour_24H_format : 1;
} NotificationScreen_t;
void notification_screen_init(NotificationScreen_t * const notificationScreen);
void notification_screen_register_on_state_change_cb(NotificationScreen_t * const notificationScreen, NotificationOnStateChangeCb_t notificationOnStateChangeCb);
void notification_screen_set_displayed_hour_format(NotificationScreen_t * const notificationScreen, bool hour_24H_format);
void notification_screen_notify(NotificationScreen_t * const notificationScreen, uint32_t handle, time_t dateOfArrival, NotificationType_e notificationType, char * title, char * body);
bool notification_screen_new_notification_available(NotificationScreen_t * const notificationScreen);