diff --git a/package.json b/package.json index 37549d0..25f369c 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "CLAY_API_IS_JELLYFIN", "CLAY_USER", "PKJS_READY", - "PKJS_SLEEP_TIMESTAMP" + "PKJS_SLEEP_TIMESTAMP", + "PKJS_LAST_WATCHED" ], "projectType": "native", "resources": { diff --git a/src/c/main.c b/src/c/main.c index 6e5ee4a..faa9762 100644 --- a/src/c/main.c +++ b/src/c/main.c @@ -2,15 +2,12 @@ #include #include -// declare pkjs statics -static bool s_js_ready; - // declare window statics static Window *s_main_window; static TextLayer *s_sleep_time_header_layer; static TextLayer *s_sleep_time_layer; -static TextLayer *s_last_episode_header_layer; -static TextLayer *s_last_episode_layer; +static TextLayer *s_last_watched_header_layer; +static TextLayer *s_last_watched_layer; static TextLayer *s_pin_notice_layer; // declare time tracking statics @@ -20,36 +17,32 @@ static void send_sleep_time_to_pkjs() { DictionaryIterator *out; AppMessageResult result = app_message_outbox_begin(&out); if (result != APP_MSG_OK) { - text_layer_set_text(s_last_episode_layer, "outbox_begin failure"); + text_layer_set_text(s_last_watched_layer, "outbox_begin failure"); return; } dict_write_uint32(out, MESSAGE_KEY_PKJS_SLEEP_TIMESTAMP, s_sleep_timestamp); result = app_message_outbox_send(); if (result != APP_MSG_OK) { - text_layer_set_text(s_last_episode_layer, "outbox_send failure"); + text_layer_set_text(s_last_watched_layer, "outbox_send failure"); return; } } -static void inbox_received_handler(DictionaryIterator *iter, void *context) { +static void inbox_received_handler_watched(DictionaryIterator *iter, void *context) { + Tuple *watched_tuple = dict_find(iter, MESSAGE_KEY_PKJS_LAST_WATCHED); + if (watched_tuple) { + APP_LOG(APP_LOG_LEVEL_INFO, "Received PKJS_LAST_WATCHED, displaying..."); + text_layer_set_text(s_last_watched_layer, watched_tuple->value->cstring); + } +} + +static void inbox_received_handler_ready(DictionaryIterator *iter, void *context) { Tuple *ready_tuple = dict_find(iter, MESSAGE_KEY_PKJS_READY); if (ready_tuple) { - s_js_ready = true; + APP_LOG(APP_LOG_LEVEL_INFO, "Received PKJS_READY, calling PKJS..."); + send_sleep_time_to_pkjs(); + app_message_register_inbox_received(inbox_received_handler_watched); } - APP_LOG(APP_LOG_LEVEL_INFO, "Received JS_READY, calling PKJS..."); - send_sleep_time_to_pkjs(); -} - -static void inbox_dropped_handler(AppMessageResult reason, void *context) { - APP_LOG(APP_LOG_LEVEL_DEBUG, "Inbox dropped: %d", reason); -} - -static void outbox_failed_handler(DictionaryIterator *iter, AppMessageResult reason, void *context) { - APP_LOG(APP_LOG_LEVEL_DEBUG, "Outbox send failed: %d", reason); -} - -static void outbox_sent_handler(DictionaryIterator *iter, void *context) { - APP_LOG(APP_LOG_LEVEL_DEBUG, "Outbox send success"); } static bool cb_update_sleep_time(HealthActivity activity, time_t start_time, time_t end_time, void *context) { @@ -79,14 +72,17 @@ static void main_window_load(Window *window) { } else { text_layer_set_text(s_sleep_time_layer, "N/A (rough night?)"); } - s_last_episode_header_layer = text_layer_create(GRect(6, 72, PBL_DISPLAY_WIDTH, 30)); - text_layer_set_background_color(s_last_episode_header_layer, GColorClear); - text_layer_set_font(s_last_episode_header_layer, fonts_get_system_font(FONT_KEY_GOTHIC_24_BOLD)); - text_layer_set_text(s_last_episode_header_layer, "Last Episode"); - s_last_episode_layer = text_layer_create(GRect(10, 96, PBL_DISPLAY_WIDTH, 30)); - text_layer_set_background_color(s_last_episode_layer, GColorClear); - text_layer_set_font(s_last_episode_layer, fonts_get_system_font(FONT_KEY_GOTHIC_24)); - text_layer_set_text(s_last_episode_layer, "N/A"); // default - will be updated before display + s_last_watched_header_layer = text_layer_create(GRect(6, 72, PBL_DISPLAY_WIDTH, 30)); + text_layer_set_background_color(s_last_watched_header_layer, GColorClear); + text_layer_set_font(s_last_watched_header_layer, fonts_get_system_font(FONT_KEY_GOTHIC_24_BOLD)); + text_layer_set_text(s_last_watched_header_layer, "Last Episode"); + s_last_watched_layer = text_layer_create(GRect(10, 96, PBL_DISPLAY_WIDTH, PBL_DISPLAY_HEIGHT)); +#if PBL_ROUND + text_layer_set_text_alignment(s_last_watched_layer, GTextAlignmentCenter); +#endif + text_layer_set_background_color(s_last_watched_layer, GColorClear); + text_layer_set_font(s_last_watched_layer, fonts_get_system_font(FONT_KEY_GOTHIC_24)); + text_layer_set_text(s_last_watched_layer, "N/A"); // default - will be updated before display s_pin_notice_layer = text_layer_create(GRect(0, PBL_DISPLAY_HEIGHT - 50, PBL_DISPLAY_WIDTH, 50)); text_layer_set_background_color(s_pin_notice_layer, GColorClear); text_layer_set_font(s_pin_notice_layer, fonts_get_system_font(FONT_KEY_GOTHIC_14)); @@ -96,16 +92,16 @@ static void main_window_load(Window *window) { Layer *window_layer = window_get_root_layer(window); layer_add_child(window_layer, text_layer_get_layer(s_sleep_time_header_layer)); layer_add_child(window_layer, text_layer_get_layer(s_sleep_time_layer)); - layer_add_child(window_layer, text_layer_get_layer(s_last_episode_header_layer)); - layer_add_child(window_layer, text_layer_get_layer(s_last_episode_layer)); + layer_add_child(window_layer, text_layer_get_layer(s_last_watched_header_layer)); + layer_add_child(window_layer, text_layer_get_layer(s_last_watched_layer)); layer_add_child(window_layer, text_layer_get_layer(s_pin_notice_layer)); } static void main_window_unload(Window *window) { text_layer_destroy(s_sleep_time_header_layer); text_layer_destroy(s_sleep_time_layer); - text_layer_destroy(s_last_episode_header_layer); - text_layer_destroy(s_last_episode_layer); + text_layer_destroy(s_last_watched_header_layer); + text_layer_destroy(s_last_watched_layer); text_layer_destroy(s_pin_notice_layer); } @@ -117,10 +113,7 @@ static void init() { (WindowHandlers){.load = main_window_load, .unload = main_window_unload}); // initialize pkjs - app_message_register_inbox_received(inbox_received_handler); - app_message_register_inbox_dropped(inbox_dropped_handler); - app_message_register_outbox_failed(outbox_failed_handler); - app_message_register_outbox_sent(outbox_sent_handler); + app_message_register_inbox_received(inbox_received_handler_ready); app_message_open(1024, 1024); // get sleep time (UNIX timestamp) diff --git a/src/pkjs/index.js b/src/pkjs/index.js index 1de5031..b49bad9 100644 --- a/src/pkjs/index.js +++ b/src/pkjs/index.js @@ -33,7 +33,7 @@ function callAPI(fullURL, apiKey, isJellyfin, trackedUser, sleepTimestamp) { if (resp && ((isJellyfin === true && Array.isArray(resp.Items)) || (isJellyfin === false && Array.isArray(resp.response.data.data)))) { let delta; let bestDelta = Infinity; - let lastWatched = null; + let lastWatched = ""; if (isJellyfin === true) { const re = /^(.+) is playing (.+) on .*$/; let arr; @@ -64,8 +64,10 @@ function callAPI(fullURL, apiKey, isJellyfin, trackedUser, sleepTimestamp) { } }); } - if (lastWatched != null) { + if (lastWatched != "") { console.log("Last watched: " + lastWatched); + var dict = { 'PKJS_LAST_WATCHED': lastWatched } + Pebble.sendAppMessage(dict); } } }