From e328fa8cfa5f0d2b117f3bfdabee35844845397d Mon Sep 17 00:00:00 2001 From: Keegan Date: Fri, 28 Oct 2016 23:51:55 -0700 Subject: [PATCH] [WIP] - Allow developers to specify if they want clay to open app message inbox automatically --- clay-test/src/clay-test.c | 3 ++- include/clay.h | 4 +++- src/c/clay.c | 42 +++++++++++++++++++++++---------------- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/clay-test/src/clay-test.c b/clay-test/src/clay-test.c index 9272501..802ce37 100644 --- a/clay-test/src/clay-test.c +++ b/clay-test/src/clay-test.c @@ -59,7 +59,8 @@ static void prv_init(void) { .settings_updated = prv_clay_updated_handler, }; - clay_init(CLAY_INBOX_SIZE, &clay_callbacks, NULL); + clay_register_callbacks(&clay_callbacks, NULL); + clay_init(CLAY_INBOX_SIZE, true); s_window = window_create(); window_set_window_handlers(s_window, (WindowHandlers) { diff --git a/include/clay.h b/include/clay.h index cbb6951..f35f2a4 100644 --- a/include/clay.h +++ b/include/clay.h @@ -8,7 +8,9 @@ typedef struct ClayCallbacks { ClayUpdatedCallback settings_updated; } ClayCallbacks; -void clay_init(uint32_t inbox_size, const ClayCallbacks *callbacks, void *context); +void clay_init(uint32_t inbox_size, bool open_app_message); + +void clay_register_callbacks(const ClayCallbacks *callbacks, void *context); void clay_remove(const char *key); diff --git a/src/c/clay.c b/src/c/clay.c index 7167fa3..3f94c4c 100644 --- a/src/c/clay.c +++ b/src/c/clay.c @@ -9,9 +9,16 @@ static uint32_t prv_hash_key(const char *key) { return hash((uint8_t *)key, strlen(key)); } +static bool prv_read(const char *key, void *value_out, size_t size) { + uint32_t persist_key = prv_hash_key(key); + int bytes_read = persist_read_data(persist_key, value_out, size); + return bytes_read >= 0 && (size_t)bytes_read == size; +} + static bool prv_write(const char *key, const SimpleDictDataType type, const void *data, size_t size, void *context) { uint32_t persist_key = prv_hash_key(key); - return persist_write_data(persist_key, data, size) == size; + int bytes_written = persist_write_data(persist_key, data, size); + return bytes_written >= 0 && (size_t)bytes_written == size; } static void prv_simple_app_message_received_callback(const SimpleDict *message, void *context) { @@ -24,14 +31,24 @@ static void prv_simple_app_message_received_callback(const SimpleDict *message, s_callbacks.settings_updated(context); } -static bool prv_read(const char *key, void *value_out, size_t size) { - uint32_t persist_key = prv_hash_key(key); - return persist_read_data(persist_key, value_out, size) == size; -} - // ----- PUBLIC API ----- -void clay_init(uint32_t inbox_size, const ClayCallbacks *callbacks, void *context) { +void clay_init(uint32_t inbox_size, bool open_app_message) { + const bool request_inbox_size_success = simple_app_message_request_inbox_size(inbox_size); + if (!request_inbox_size_success) { + APP_LOG(APP_LOG_LEVEL_ERROR, "CLAY: Failed to request inbox size of %d", (int)inbox_size); + return; + } + + if (!open_app_message) return; + + const AppMessageResult open_state = simple_app_message_open(); + if (open_state != APP_MSG_OK) { + APP_LOG(APP_LOG_LEVEL_ERROR, "CLAY: Failed to open appmessage inbox. Code: %d", open_state); + } +} + +void clay_register_callbacks(const ClayCallbacks *callbacks, void *context) { s_callbacks = *callbacks; const SimpleAppMessageCallbacks simple_app_message_callbacks = { @@ -41,17 +58,8 @@ void clay_init(uint32_t inbox_size, const ClayCallbacks *callbacks, void *contex SIMPLE_APP_MESSAGE_NAMESPACE, &simple_app_message_callbacks, context); if (!register_success) { - APP_LOG(APP_LOG_LEVEL_ERROR, "Failed to register callbacks for namespace %s", SIMPLE_APP_MESSAGE_NAMESPACE); - return; + APP_LOG(APP_LOG_LEVEL_ERROR, "CLAY: Failed to register callbacks for namespace %s", SIMPLE_APP_MESSAGE_NAMESPACE); } - - const bool request_inbox_size_success = simple_app_message_request_inbox_size(inbox_size); - if (!request_inbox_size_success) { - APP_LOG(APP_LOG_LEVEL_ERROR, "Failed to request inbox size of %d", (int)inbox_size); - return; - } - - simple_app_message_open(); } bool clay_get_int(const char *key, int *value_out) {