diff --git a/package.json b/package.json index 815653b..a8f928a 100644 --- a/package.json +++ b/package.json @@ -37,8 +37,8 @@ }, "messageKeys": [ "PKJS_READY", - "IN_VANITY_LIST", - "IN_ID_LIST", + "IN_VANITY_LIST[2048]", + "IN_ID_LIST[2048]", "OUT_MENU_NAV_SCOPE", "OUT_JFID", "OUT_SHUFFLE_MODE" diff --git a/src/c/jellyPod.c b/src/c/jellyPod.c index 6dfbf68..92a9928 100644 --- a/src/c/jellyPod.c +++ b/src/c/jellyPod.c @@ -10,20 +10,8 @@ typedef enum { NAV_SCOPE_ALBUM_TRACKS = 4, } NavScope; -// layer-age -static Window *s_main_window; -static CustomStatusBarLayer *s_status_bar_layer; -static Layer *s_divider_layer; -static MenuLayer *s_menu_layer; - -// globals -static bool s_pkjs_ready = false; -static NavScope s_current_menu_scope = NAV_SCOPE_MAIN; - -// resources -static GFont s_font_chicago; - // constants +#define MAX_MENU_ITEMS 2048 #define BAR_HEIGHT 20 #ifdef PBL_RECT #define MARGIN_X 4 @@ -39,22 +27,57 @@ static GFont s_font_chicago; static const GRect s_menu_grect = GRect(MARGIN_X, BAR_HEIGHT + MARGIN_Y, PBL_DISPLAY_WIDTH - (MARGIN_X * 2), PBL_DISPLAY_HEIGHT - (MARGIN_Y * 2)); static const char *s_main_menu_options[4] = {"Shuffle Songs", "Shuffle Artists", "> All Artists", "> Search Artists"}; +// layer-age +static Window *s_main_window; +static CustomStatusBarLayer *s_status_bar_layer; +static Layer *s_divider_layer; +static MenuLayer *s_menu_layer; + +// globals +static bool s_pkjs_ready = false; +static NavScope s_current_menu_scope = NAV_SCOPE_MAIN; +static const char *s_current_menu_vanity[MAX_MENU_ITEMS]; +// static uint16_t s_current_menu_seq_ids[MAX_MENU_ITEMS]; +static uint16_t s_menu_items_count = 0; + +// resources +static GFont s_font_chicago; + ////////////////////// PKJS ////////////////////// +static void outbox_failed_handler(DictionaryIterator *iter, AppMessageResult reason, void *context) { + APP_LOG(APP_LOG_LEVEL_ERROR, "outbox failed: %d", (int)reason); +} + static void inbox_received_handler(DictionaryIterator *iter, void *context) { - Tuple *ready_tuple = dict_find(iter, MESSAGE_KEY_PKJS_READY); - if (ready_tuple) { + Tuple *t_ready = dict_find(iter, MESSAGE_KEY_PKJS_READY); + if (t_ready) { s_pkjs_ready = true; return; } + + // handle incoming menu list items + for (uint16_t i = 0; i < MAX_MENU_ITEMS; i++) { + Tuple *t_vanity = dict_find(iter, MESSAGE_KEY_IN_VANITY_LIST + i); + if (t_vanity) { + s_current_menu_vanity[i] = t_vanity->value->cstring; + } else { + s_menu_items_count = i; + break; + } + } + s_current_menu_scope = NAV_SCOPE_SEARCH; + menu_layer_reload_data(s_menu_layer); + menu_layer_set_selected_index(s_menu_layer, MenuIndex(0, 0), MenuRowAlignTop, false); + return; } +// request a new menu scope from the PKJS side static void to_pkjs_scope_change(NavScope scope) { if (!s_pkjs_ready) { return; } - // send scope to return data for (PKJS side should track last scope and only send new data if the new scope differs) DictionaryIterator *out; AppMessageResult result = app_message_outbox_begin(&out); if (result != APP_MSG_OK) { @@ -72,12 +95,8 @@ static uint16_t menu_get_num_rows(MenuLayer *menu_layer, uint16_t section_index, switch (s_current_menu_scope) { case NAV_SCOPE_MAIN: return 4; - case NAV_SCOPE_SEARCH: - case NAV_SCOPE_ARTISTS: - case NAV_SCOPE_ALBUMS: - case NAV_SCOPE_ALBUM_TRACKS: default: - return 99; + return s_menu_items_count; } } @@ -86,12 +105,8 @@ static void menu_draw_row(GContext *ctx, const Layer *cell_layer, MenuIndex *cel case NAV_SCOPE_MAIN: custom_menu_cell_title_draw(ctx, cell_layer, s_main_menu_options[cell_index->row], NULL, s_font_chicago, MARGIN_X); break; - case NAV_SCOPE_SEARCH: - case NAV_SCOPE_ARTISTS: - case NAV_SCOPE_ALBUMS: - case NAV_SCOPE_ALBUM_TRACKS: default: - return; + custom_menu_cell_title_draw(ctx, cell_layer, s_current_menu_vanity[cell_index->row], NULL, s_font_chicago, MARGIN_X); } } @@ -163,7 +178,8 @@ static void main_window_unload() { static void init() { // initialize pkjs app_message_register_inbox_received(inbox_received_handler); - app_message_open(4096, 1024); // TODO adjust sizes + app_message_register_outbox_failed(outbox_failed_handler); + app_message_open(app_message_inbox_size_maximum(), 512); // load font s_font_chicago = fonts_load_custom_font(resource_get_handle(RESOURCE_ID_FONT_CHICAGO_16)); diff --git a/src/pkjs/index.js b/src/pkjs/index.js index 00cafe7..9dbe86a 100644 --- a/src/pkjs/index.js +++ b/src/pkjs/index.js @@ -1,8 +1,10 @@ var Clay = require('@rebble/clay'); var clayConfig = require('./config'); +var keys = require('message_keys'); new Clay(clayConfig); var cfg = JSON.parse(localStorage.getItem('clay-settings')); +var lookupMap = new Map(); Pebble.addEventListener("ready", function () { // store cache of library info from Jellyfin @@ -26,7 +28,6 @@ Pebble.addEventListener("ready", function () { // format response for jellyPod //// build lookupMap - const lookupMap = new Map(); let lastAlbumInfo = {AlbumArtist: "", AlbumName: "", AlbumYear: 0}; for (const track of libObj.Items) { if (!track.Name) continue; @@ -69,16 +70,29 @@ Pebble.addEventListener("ready", function () { //console.log(JSON.stringify(lookupMap, (k, v) => v instanceof Map ? Object.fromEntries(v) : v)); - Pebble.sendAppMessage({ PKJS_READY: 1 }); + const readyDict = {}; + readyDict[keys.PKJS_READY] = 1; + Pebble.sendAppMessage(readyDict); }); }); } }); Pebble.addEventListener("appmessage", function (dict) { - if (dict.payload["OUT_MENU_NAV_SCOPE"]) { - //const cfg = JSON.parse(localStorage.getItem('clay-settings')); - //callAPI(cfg.PKJS_PAVLOK_API_KEY, parseInt(dict.payload["PKJS_PAVLOK_ZAP"])); + if (dict.payload[keys.OUT_MENU_NAV_SCOPE]) { + switch (dict.payload[keys.OUT_MENU_NAV_SCOPE]) { + case 1: { // search + const vanityList = Array.from(lookupMap.keys()); + const outDict = {}; + for (let i = 0; i < vanityList.length; i++) { + outDict[keys.IN_VANITY_LIST + i] = vanityList[i]; + } + Pebble.sendAppMessage(outDict); + return; + } + default: + return; + } } });