Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 45 additions & 20 deletions Apps/System/notification.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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);

Expand All @@ -177,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));
Expand All @@ -187,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,
Expand All @@ -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)
Expand Down
34 changes: 34 additions & 0 deletions rcore/notification_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 2 additions & 0 deletions rcore/notification_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down