mirror of
https://github.com/pebble-dev/clay.git
synced 2026-08-28 04:46:57 -04:00
[WIP] - Simplify reading and writing of settings
This commit is contained in:
+6
-56
@@ -11,31 +11,7 @@ static uint32_t prv_hash_key(const char *key) {
|
|||||||
|
|
||||||
static bool prv_write(const char *key, const SimpleDictDataType type, const void *data, size_t data_size, void *context) {
|
static bool prv_write(const char *key, const SimpleDictDataType type, const void *data, size_t data_size, void *context) {
|
||||||
uint32_t persist_key = prv_hash_key(key);
|
uint32_t persist_key = prv_hash_key(key);
|
||||||
|
return persist_write_data(persist_key, data, data_size) != E_DOES_NOT_EXIST;
|
||||||
switch (type) {
|
|
||||||
case SimpleDictDataType_Raw:
|
|
||||||
persist_write_data(persist_key, data, data_size);
|
|
||||||
return true;
|
|
||||||
case SimpleDictDataType_Bool: {
|
|
||||||
const bool value = *((bool *) data);
|
|
||||||
persist_write_bool(persist_key, value);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
case SimpleDictDataType_Int: {
|
|
||||||
const int value = *((int *) data);
|
|
||||||
persist_write_int(persist_key, value);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
case SimpleDictDataType_String: {
|
|
||||||
const char *value = data;
|
|
||||||
persist_write_string(persist_key, value);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
case SimpleDictDataTypeCount:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
APP_LOG(APP_LOG_LEVEL_ERROR, "Unexpected type %d", type);
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void prv_simple_app_message_received_callback(const SimpleDict *message, void *context) {
|
static void prv_simple_app_message_received_callback(const SimpleDict *message, void *context) {
|
||||||
@@ -48,35 +24,9 @@ static void prv_simple_app_message_received_callback(const SimpleDict *message,
|
|||||||
s_callbacks.settings_updated(context);
|
s_callbacks.settings_updated(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool prv_read(SimpleDictDataType type, const char *key, void *value_out, size_t size) {
|
static bool prv_read(const char *key, void *value_out, size_t size) {
|
||||||
uint32_t persist_key = prv_hash_key(key);
|
uint32_t persist_key = prv_hash_key(key);
|
||||||
|
return persist_read_data(persist_key, value_out, size) != E_DOES_NOT_EXIST;
|
||||||
if (persist_exists(persist_key)) {
|
|
||||||
|
|
||||||
switch (type) {
|
|
||||||
case SimpleDictDataType_Raw:
|
|
||||||
// persist_write_data(persist_key, data, data_size);
|
|
||||||
return true;
|
|
||||||
case SimpleDictDataType_Bool: {
|
|
||||||
const bool value = persist_read_bool(persist_key);
|
|
||||||
memcpy(value_out, &value, size);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
case SimpleDictDataType_Int: {
|
|
||||||
const int value = persist_read_int(persist_key);
|
|
||||||
memcpy(value_out, &value, size);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
case SimpleDictDataType_String: {
|
|
||||||
persist_read_string(persist_key, value_out, size);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
case SimpleDictDataTypeCount:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----- PUBLIC API -----
|
// ----- PUBLIC API -----
|
||||||
@@ -105,13 +55,13 @@ void clay_init(uint32_t inbox_size, const ClayCallbacks *callbacks, void *contex
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool clay_get_int(const char *key, int *value_out) {
|
bool clay_get_int(const char *key, int *value_out) {
|
||||||
return prv_read(SimpleDictDataType_Int, key, value_out, sizeof(&value_out));
|
return prv_read(key, value_out, sizeof(*value_out));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool clay_get_bool(const char *key, bool *value_out) {
|
bool clay_get_bool(const char *key, bool *value_out) {
|
||||||
return prv_read(SimpleDictDataType_Bool, key, value_out, sizeof(&value_out));
|
return prv_read(key, value_out, sizeof(*value_out));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool clay_get_string(const char *key, char *value_out, size_t size) {
|
bool clay_get_string(const char *key, char *value_out, size_t size) {
|
||||||
return prv_read(SimpleDictDataType_String, key, value_out, size);
|
return prv_read(key, value_out, size);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user