From 6b6be62a1bbf5a16d513d5a9b5e1444c1878a4ee Mon Sep 17 00:00:00 2001 From: Francis Nguyen Date: Sun, 28 Feb 2021 11:30:53 -0700 Subject: [PATCH 1/2] Add Clear All to notification app --- Apps/System/notification.c | 63 +++++++++++++++++++++++++----------- rcore/notification_manager.c | 34 +++++++++++++++++++ rcore/notification_manager.h | 2 ++ 3 files changed, 80 insertions(+), 19 deletions(-) diff --git a/Apps/System/notification.c b/Apps/System/notification.c index 4be4f230..114b4dd4 100644 --- a/Apps/System/notification.c +++ b/Apps/System/notification.c @@ -39,6 +39,17 @@ static int _notif_count = 0; static void _notif_window_load(Window *window); static void _notif_window_unload(Window *window); +static void _notif_get_all() { + _notif_count = notifications_get_all(&_notif_list); + menu_layer_reload_data(s_menu_layer); +} + +static void _notif_clear_all() { + notifications_dismiss_all(&_notif_list); + _notif_count = 0; + menu_layer_reload_data(s_menu_layer); +} + void notif_init(void) { _notif_window = window_create(); @@ -64,12 +75,13 @@ static uint16_t _notif_menu_get_num_rows(MenuLayer *menu_layer, uint16_t section if (_notif_count == 0) return 1; else - return _notif_count; + /* Add additional row for "Clear All" */ + return _notif_count + 1; } -static rebble_notification *_noty_for_index(MenuIndex *cell_index) { +static rebble_notification *_noty_for_index(int notif_index) { /* Find the noty. */ - int wantidx = _notif_count - cell_index->row - 1; + int wantidx = _notif_count - notif_index - 1; int i = 0; struct rdb_select_result *res; rdb_select_result_foreach(res, &_notif_list) { @@ -91,7 +103,26 @@ static void _notif_menu_draw_row(GContext *ctx, const Layer *cell_layer, MenuInd return; } - rebble_notification *noty = _noty_for_index(cell_index); + if (cell_index->row == 0) { + GSize size = layer_get_frame(cell_layer).size; + n_GFont font = fonts_get_system_font(FONT_KEY_GOTHIC_24_BOLD); + uint8_t line_height = n_graphics_font_get_line_height(font); + + graphics_draw_text( + ctx, + "Clear All", + font, + GRect(0, (size.h - line_height - 2 * MENU_CELL_PADDING) / 2, size.w, line_height), + n_GTextOverflowModeFill, + GTextAlignmentCenter, + NULL); + + return; + } + + int noty_index = cell_index->row - 1; + rebble_notification *noty = _noty_for_index(noty_index); + if (!noty) { menu_cell_basic_draw(ctx, cell_layer, "Error", "Failed to load", NULL); return; @@ -150,6 +181,13 @@ static int16_t _notif_menu_get_cell_height(struct MenuLayer *menu_layer, MenuInd } static void _notif_menu_select_click(struct MenuLayer *menu_layer, MenuIndex *cell_index, void *context) { + if (cell_index->row == 0) { + _notif_clear_all(); + return; + } + + int noty_index = cell_index->row - 1; + /* Build the list for the notification window. */ Uuid *uuids = malloc(sizeof(Uuid) * _notif_count); if (!uuids) @@ -162,7 +200,7 @@ static void _notif_menu_select_click(struct MenuLayer *menu_layer, MenuIndex *ce i++; } - notification_window_set_notifications(&_notifdetail_window, uuids, _notif_count, cell_index->row); + notification_window_set_notifications(&_notifdetail_window, uuids, _notif_count, noty_index); free(uuids); @@ -200,20 +238,7 @@ static void _notif_window_load(Window *window) /* Load in the keys for all the notifications on the system. */ list_init_head(&_notif_list); - struct rdb_database *db = rdb_open(RDB_ID_NOTIFICATION); - struct rdb_iter it; - if (rdb_iter_start(db, &it)) { - struct rdb_selector selectors[] = { - { offsetof(timeline_item, uuid), FIELD_SIZEOF(timeline_item, uuid), RDB_OP_RESULT }, - { } - }; - _notif_count = rdb_select(&it, &_notif_list, selectors); - APP_LOG("noty", APP_LOG_LEVEL_INFO, "%d items from select", _notif_count); - } - - rdb_close(db); - - menu_layer_reload_data(s_menu_layer); + _notif_get_all(); } static void _notif_window_unload(Window *window) diff --git a/rcore/notification_manager.c b/rcore/notification_manager.c index 99b8f706..cd006bd9 100644 --- a/rcore/notification_manager.c +++ b/rcore/notification_manager.c @@ -273,3 +273,37 @@ void notification_show_progress(EventServiceCommand command, void *data, void *c overlay_window_create_with_context(_notification_window_creating, (void *)nmsg); } +int notifications_get_all(rdb_select_result_list *notif_list) +{ + struct rdb_database *db = rdb_open(RDB_ID_NOTIFICATION); + struct rdb_iter it; + + int notif_count = 0; + + if (rdb_iter_start(db, &it)) { + struct rdb_selector selectors[] = { + { offsetof(timeline_item, uuid), FIELD_SIZEOF(timeline_item, uuid), RDB_OP_RESULT }, + { } + }; + notif_count = rdb_select(&it, notif_list, selectors); + } + + rdb_close(db); + + return notif_count; +} + +void notifications_dismiss_all(rdb_select_result_list *notif_list) +{ + struct rdb_database *db = rdb_open(RDB_ID_NOTIFICATION); + struct rdb_select_result *res; + + rdb_select_result_foreach(res, notif_list) { + rdb_delete(&res->it); + } + + rdb_close(db); + + rdb_select_free_all(notif_list); + list_init_head(notif_list); +} \ No newline at end of file diff --git a/rcore/notification_manager.h b/rcore/notification_manager.h index 2bf41c0a..09e80c63 100644 --- a/rcore/notification_manager.h +++ b/rcore/notification_manager.h @@ -62,6 +62,8 @@ void call_window_overlay_destroy(OverlayWindow *overlay, Window *window); void progress_window_overlay_display(OverlayWindow *overlay, Window *window); void progress_window_overlay_destroy(OverlayWindow *overlay, Window *window); +int notifications_get_all(rdb_select_result_list *notif_list); +void notifications_dismiss_all(rdb_select_result_list *notif_list); typedef struct notification_data_t { OverlayCreateCallback create_callback; From c77021af540fc794a2c6b356dbd95b85fc031315 Mon Sep 17 00:00:00 2001 From: Francis Nguyen Date: Sun, 28 Feb 2021 13:00:11 -0700 Subject: [PATCH 2/2] Use consistent UI styling on notification app --- Apps/System/notification.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Apps/System/notification.c b/Apps/System/notification.c index 114b4dd4..c96da708 100644 --- a/Apps/System/notification.c +++ b/Apps/System/notification.c @@ -215,7 +215,6 @@ static void _notif_window_load(Window *window) _notif_window_status = status_bar_layer_create(); status_bar_layer_set_colors(_notif_window_status, GColorBlack, GColorWhite); - status_bar_layer_set_separator_mode(_notif_window_status, StatusBarLayerSeparatorModeDotted); #ifdef PBL_RECT s_menu_layer = menu_layer_create(GRect(0, 16, DISPLAY_COLS, DISPLAY_ROWS - 16)); @@ -225,6 +224,7 @@ static void _notif_window_load(Window *window) #endif menu_layer_set_click_config_onto_window(s_menu_layer, window); + menu_layer_set_highlight_colors(s_menu_layer, PBL_IF_COLOR_ELSE(GColorRed, GColorBlack), GColorWhite); menu_layer_set_callbacks(s_menu_layer, NULL, (MenuLayerCallbacks) { .get_num_rows = _notif_menu_get_num_rows, .draw_row = _notif_menu_draw_row,