mirror of
https://github.com/pebble-dev/clay.git
synced 2026-08-27 20:36:33 -04:00
[WIP] - Implement bool and string getter
This commit is contained in:
@@ -3,26 +3,53 @@
|
|||||||
#include <pebble-clay/clay.h>
|
#include <pebble-clay/clay.h>
|
||||||
|
|
||||||
#define CLAY_INBOX_SIZE (64)
|
#define CLAY_INBOX_SIZE (64)
|
||||||
|
#define CHAR_SIZE (10)
|
||||||
|
|
||||||
static Window *s_window;
|
static Window *s_window;
|
||||||
static int s_background_color = 0xff0000;
|
static int s_background_color = 0xff0000;
|
||||||
|
static char s_label_char_value[CHAR_SIZE] = "initial";
|
||||||
|
static bool s_label_bool_value = false;
|
||||||
|
static TextLayer *s_label_char;
|
||||||
|
static TextLayer *s_label_bool;
|
||||||
|
|
||||||
static void prv_update_layer(Layer *layer, GContext *ctx) {
|
static void prv_update_display(Layer *layer, GContext *ctx) {
|
||||||
graphics_context_set_fill_color(ctx, GColorFromHEX(s_background_color));
|
graphics_context_set_fill_color(ctx, GColorFromHEX(s_background_color));
|
||||||
graphics_fill_rect(ctx, layer_get_bounds(layer), 0, GCornerNone);
|
graphics_fill_rect(ctx, layer_get_bounds(layer), 0, GCornerNone);
|
||||||
|
|
||||||
|
text_layer_set_text(s_label_char, s_label_char_value);
|
||||||
|
text_layer_set_text(s_label_bool, s_label_bool_value ? "true" : "false");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void prv_clay_updated_handler(void *context) {
|
static void prv_clay_updated_handler(void *context) {
|
||||||
clay_get_int("test_int", &s_background_color);
|
clay_get_int("test_int", &s_background_color);
|
||||||
|
clay_get_bool("test_bool", &s_label_bool_value);
|
||||||
|
clay_get_string("test_char", s_label_char_value, CHAR_SIZE);
|
||||||
|
|
||||||
layer_mark_dirty(window_get_root_layer(s_window));
|
layer_mark_dirty(window_get_root_layer(s_window));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void prv_window_unload(Window *window) {
|
static void prv_window_unload(Window *window) {
|
||||||
|
layer_destroy(text_layer_get_layer(s_label_char));
|
||||||
window_destroy(window);
|
window_destroy(window);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void prv_window_load(Window *window) {
|
static void prv_window_load(Window *window) {
|
||||||
layer_set_update_proc(window_get_root_layer(s_window), prv_update_layer);
|
Layer *window_layer = window_get_root_layer(s_window);
|
||||||
|
|
||||||
|
layer_set_update_proc(window_layer, prv_update_display);
|
||||||
|
|
||||||
|
GRect bounds = layer_get_bounds(window_layer);
|
||||||
|
|
||||||
|
s_label_char = text_layer_create(GRect(0, bounds.size.h/4, bounds.size.w, 30));
|
||||||
|
text_layer_set_text_alignment(s_label_char, GTextAlignmentCenter);
|
||||||
|
text_layer_set_background_color(s_label_char, GColorClear);
|
||||||
|
layer_add_child(window_layer, text_layer_get_layer(s_label_char));
|
||||||
|
|
||||||
|
s_label_bool = text_layer_create(GRect(0, bounds.size.h/2, bounds.size.w, 30));
|
||||||
|
text_layer_set_text_alignment(s_label_bool, GTextAlignmentCenter);
|
||||||
|
text_layer_set_background_color(s_label_bool, GColorClear);
|
||||||
|
layer_add_child(window_layer, text_layer_get_layer(s_label_bool));
|
||||||
|
|
||||||
prv_clay_updated_handler(NULL);
|
prv_clay_updated_handler(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+28
-7
@@ -3,13 +3,34 @@ var Clay = require('pebble-clay');
|
|||||||
var clay = new Clay({
|
var clay = new Clay({
|
||||||
config: [
|
config: [
|
||||||
{
|
{
|
||||||
label: 'test',
|
type: 'section',
|
||||||
type: 'color',
|
items: [
|
||||||
messageKey: 'test_int'
|
{
|
||||||
},
|
label: 'int',
|
||||||
{
|
type: 'color',
|
||||||
type: 'submit',
|
messageKey: 'test_int'
|
||||||
defaultValue: 'submit'
|
},
|
||||||
|
{
|
||||||
|
label: 'char',
|
||||||
|
type: 'input',
|
||||||
|
messageKey: 'test_char'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'bool',
|
||||||
|
type: 'toggle',
|
||||||
|
messageKey: 'test_bool'
|
||||||
|
},
|
||||||
|
// {
|
||||||
|
// label: 'raw',
|
||||||
|
// type: 'checkboxgroup',
|
||||||
|
// messageKey: 'test_raw',
|
||||||
|
// options: ['one', 'two', 'three']
|
||||||
|
// },
|
||||||
|
{
|
||||||
|
type: 'submit',
|
||||||
|
defaultValue: 'submit'
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
closedCallback: function(response) {
|
closedCallback: function(response) {
|
||||||
|
|||||||
+1
-1
@@ -28,4 +28,4 @@ bool clay_get_data(const char *key, void *value_out, size_t n);
|
|||||||
|
|
||||||
bool clay_get_int(const char *key, int *value_out);
|
bool clay_get_int(const char *key, int *value_out);
|
||||||
|
|
||||||
bool clay_get_string(const char *key, char *value_out, size_t n);
|
bool clay_get_string(const char *key, char *value_out, size_t size);
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ function Clay(options) {
|
|||||||
|
|
||||||
if (!e || !e.response) { return; }
|
if (!e || !e.response) { return; }
|
||||||
|
|
||||||
var settings = self.getSettings(e.response);
|
var settings = self.getSettings(e.response, false);
|
||||||
|
|
||||||
if (options.closedCallback) {
|
if (options.closedCallback) {
|
||||||
options.closedCallback(settings);
|
options.closedCallback(settings);
|
||||||
@@ -207,7 +207,7 @@ Clay.prototype.getSettings = function(response, convert) {
|
|||||||
|
|
||||||
localStorage.setItem('clay-settings', JSON.stringify(settingsStorage));
|
localStorage.setItem('clay-settings', JSON.stringify(settingsStorage));
|
||||||
|
|
||||||
return convert === false ? settings : Clay.prepareSettingsForAppMessage(settings);
|
return convert === false ? settingsStorage : Clay.prepareSettingsForAppMessage(settings);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -330,14 +330,14 @@ Clay.prepareSettingsForAppMessage = function(settings) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// validate the settings
|
// validate the settings
|
||||||
Object.keys(result).forEach(function(key) {
|
// Object.keys(result).forEach(function(key) {
|
||||||
if (Array.isArray(result[key])) {
|
// if (Array.isArray(result[key])) {
|
||||||
throw new Error('Clay does not support 2 dimensional arrays for item ' +
|
// throw new Error('Clay does not support 2 dimensional arrays for item ' +
|
||||||
'values. Make sure you are not attempting to use array ' +
|
// 'values. Make sure you are not attempting to use array ' +
|
||||||
'syntax (eg: "myMessageKey[2]") in the messageKey for ' +
|
// 'syntax (eg: "myMessageKey[2]") in the messageKey for ' +
|
||||||
'components that return an array, such as a checkboxgroup');
|
// 'components that return an array, such as a checkboxgroup');
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|||||||
+47
-17
@@ -5,9 +5,12 @@
|
|||||||
|
|
||||||
static ClayCallbacks s_callbacks;
|
static ClayCallbacks s_callbacks;
|
||||||
|
|
||||||
static bool prv_store_settings(
|
static uint32_t prv_hash_key(const char *key) {
|
||||||
const char *key, SimpleDictDataType type, const void *data, size_t data_size, void *context) {
|
return hash((uint8_t *)key, strlen(key));
|
||||||
uint32_t persist_key = hash((uint8_t *) key, strlen(key));
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case SimpleDictDataType_Raw:
|
case SimpleDictDataType_Raw:
|
||||||
@@ -20,8 +23,6 @@ static bool prv_store_settings(
|
|||||||
}
|
}
|
||||||
case SimpleDictDataType_Int: {
|
case SimpleDictDataType_Int: {
|
||||||
const int value = *((int *) data);
|
const int value = *((int *) data);
|
||||||
APP_LOG(APP_LOG_LEVEL_INFO, "CLAY: writing %d - %d to storage",
|
|
||||||
(int) persist_key, value);
|
|
||||||
persist_write_int(persist_key, value);
|
persist_write_int(persist_key, value);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -43,10 +44,43 @@ static void prv_simple_app_message_received_callback(const SimpleDict *message,
|
|||||||
}
|
}
|
||||||
|
|
||||||
APP_LOG(APP_LOG_LEVEL_INFO, "CLAY: Received SimpleAppMessage");
|
APP_LOG(APP_LOG_LEVEL_INFO, "CLAY: Received SimpleAppMessage");
|
||||||
simple_dict_foreach(message, prv_store_settings, NULL);
|
simple_dict_foreach(message, prv_write, NULL);
|
||||||
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) {
|
||||||
|
uint32_t persist_key = prv_hash_key(key);
|
||||||
|
|
||||||
|
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 -----
|
||||||
|
|
||||||
void clay_init(uint32_t inbox_size, const ClayCallbacks *callbacks, void *context) {
|
void clay_init(uint32_t inbox_size, const ClayCallbacks *callbacks, void *context) {
|
||||||
s_callbacks = *callbacks;
|
s_callbacks = *callbacks;
|
||||||
|
|
||||||
@@ -71,17 +105,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) {
|
||||||
uint32_t persist_key = hash((uint8_t *)key, strlen(key));
|
return prv_read(SimpleDictDataType_Int, key, value_out, sizeof(&value_out));
|
||||||
|
|
||||||
if (persist_exists(persist_key)) {
|
|
||||||
int value = persist_read_int(persist_key);
|
|
||||||
APP_LOG(APP_LOG_LEVEL_INFO, "CLAY: reading %d - %d from storage", (int)persist_key, value);
|
|
||||||
memcpy(value_out, &value, sizeof(&value));
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool clay_get_bool(const char *key, bool *value_out) {
|
||||||
|
return prv_read(SimpleDictDataType_Bool, key, value_out, sizeof(&value_out));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool clay_get_string(const char *key, char *value_out, size_t size) {
|
||||||
|
return prv_read(SimpleDictDataType_String, key, value_out, size);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user