From dc1788c9d08680746c8c8e1fb9ebd81c967d24ef Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Sat, 2 May 2026 19:35:34 -0400 Subject: [PATCH] Wire up timeline pins --- package.json | 4 +++- src/c/main.c | 59 +++++++++++++++++++++++++++++++---------------- src/pkjs/index.js | 12 ++++++++++ 3 files changed, 54 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index a29f895..ad7fa20 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,9 @@ "CLAY_USER", "PKJS_READY", "PKJS_SLEEP_TIMESTAMP", - "PKJS_LAST_WATCHED" + "PKJS_LAST_WATCHED", + "PKJS_PIN_TIMESTAMP", + "PKJS_PIN_TITLE" ], "projectType": "native", "resources": { diff --git a/src/c/main.c b/src/c/main.c index 1aad13f..e879a8c 100644 --- a/src/c/main.c +++ b/src/c/main.c @@ -169,15 +169,47 @@ static void send_sleep_time_to_pkjs() { } } -static void soft_reload() { - window_stack_remove(s_main_window, false); - window_destroy(s_main_window); +static void send_pin_to_pkjs() { + DictionaryIterator *out; + AppMessageResult result = app_message_outbox_begin(&out); + if (result != APP_MSG_OK) { + text_layer_set_text(s_last_watched_layer, "outbox_begin failure"); + return; + } + if (s_sleep_session_found) { + dict_write_uint32(out, MESSAGE_KEY_PKJS_PIN_TIMESTAMP, s_sleep_timestamp); + } else { + dict_write_uint32(out, MESSAGE_KEY_PKJS_PIN_TIMESTAMP, time(NULL)); + } + dict_write_cstring(out, MESSAGE_KEY_PKJS_PIN_TITLE, text_layer_get_text(s_last_watched_layer)); + result = app_message_outbox_send(); + if (result != APP_MSG_OK) { + text_layer_set_text(s_last_watched_layer, "outbox_send failure"); + return; + } +} + +static void select_single_click_handler(ClickRecognizerRef recognizer, void *context) { + Window *window = (Window *)context; + send_pin_to_pkjs(); +} + +static void click_config_provider(Window *window) { + window_single_click_subscribe(BUTTON_ID_SELECT, select_single_click_handler); +} + +static void soft_reload(bool first_load) { + if (!first_load) { + window_stack_remove(s_main_window, false); + window_destroy(s_main_window); + } s_main_window = window_create(); window_set_background_color(s_main_window, GColorBlack); window_set_window_handlers( s_main_window, (WindowHandlers){.load = main_window_load, .unload = main_window_unload}); window_stack_push(s_main_window, false); + window_set_click_config_provider(s_main_window, (ClickConfigProvider)click_config_provider); send_sleep_time_to_pkjs(); } @@ -219,19 +251,13 @@ static void inbox_received_handler(DictionaryIterator *iter, void *context) { #else layer_mark_dirty(s_button_bar_layer); #endif - soft_reload(); // app needs "restarted" when settings are changed + soft_reload(false); // app needs "restarted" when settings are changed return; } Tuple *watched_tuple = dict_find(iter, MESSAGE_KEY_PKJS_LAST_WATCHED); if (watched_tuple) { - if (s_sleep_session_found) { - text_layer_set_text(s_last_watched_layer, watched_tuple->value->cstring); - } else { - static char last_watched_buffer[1024]; - snprintf(last_watched_buffer, sizeof(last_watched_buffer), "Last item played:\n%s", watched_tuple->value->cstring); - text_layer_set_text(s_last_watched_layer, last_watched_buffer); - } + text_layer_set_text(s_last_watched_layer, watched_tuple->value->cstring); #if PBL_DISPLAY_WIDTH >= 200 PropertyAnimation *sleep_bar_prop = property_animation_create_layer_frame(s_sleep_bar_layer, &s_sleep_bar_start, &GRect(0, -PBL_DISPLAY_HEIGHT + PBL_IF_ROUND_ELSE(53, 31), PBL_DISPLAY_WIDTH, PBL_DISPLAY_HEIGHT)); #else @@ -250,13 +276,6 @@ static void inbox_received_handler(DictionaryIterator *iter, void *context) { } static void init() { - // initialize window - s_main_window = window_create(); - window_set_background_color(s_main_window, GColorBlack); - window_set_window_handlers( - s_main_window, - (WindowHandlers){.load = main_window_load, .unload = main_window_unload}); - // set colors if is_jellyfin is locally set; // if it has not yet been set, it will be once // the user saves their settings @@ -284,7 +303,7 @@ static void init() { // initialize pkjs app_message_register_inbox_received(inbox_received_handler); - app_message_open(1024, 64); + app_message_open(1024, 1536); // get sleep time (UNIX timestamp) time_t end = time(NULL); @@ -299,7 +318,7 @@ static void init() { s_sleep_data_accessible = false; } - window_stack_push(s_main_window, true); + soft_reload(true); } static void deinit() { window_destroy(s_main_window); } diff --git a/src/pkjs/index.js b/src/pkjs/index.js index 78ff3e0..9284d9b 100644 --- a/src/pkjs/index.js +++ b/src/pkjs/index.js @@ -27,6 +27,18 @@ Pebble.addEventListener("appmessage", function (dict) { callAPI(cfg.CLAY_API_HOST + "/api/v2?apikey=" + cfg.CLAY_API_KEY + "&cmd=get_history&length=100", null, false, cfg.CLAY_USER, sleepTimestamp); } } + + if (dict.payload["PKJS_PIN_TIMESTAMP"] && dict.payload["PKJS_PIN_TITLE"]) { + var pin = { + id: dict.payload["PKJS_PIN_TIMESTAMP"], + time: new Date(dict.payload["PKJS_PIN_TIMESTAMP"] * 1000).toISOString(), + layout: { + type: "genericPin", + title: dict.payload["PKJS_PIN_TITLE"] + } + }; + Pebble.insertTimelinePin(JSON.stringify(pin)); + } }); function callAPI(fullURL, apiKey, isJellyfin, trackedUser, sleepTimestamp) {