mirror of
https://github.com/HASwitchPlate/openHASP.git
synced 2025-07-27 05:06:44 +00:00
Backwards compatibility for swipe property #432
This commit is contained in:
parent
42cd5f91e2
commit
43f36422d6
@ -207,54 +207,77 @@ const char* my_obj_get_action(lv_obj_t* obj)
|
|||||||
return ext->action;
|
return ext->action;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// the swipe data is stored as SERIALIZED JSON data
|
// the swipe data is stored as SERIALIZED JSON data
|
||||||
void my_obj_set_swipe(lv_obj_t* obj, const char* payload)
|
void my_obj_set_swipe(lv_obj_t* obj, const char* payload)
|
||||||
{
|
{
|
||||||
hasp_ext_user_data_t* ext = (hasp_ext_user_data_t*)obj->user_data.ext;
|
hasp_ext_user_data_t* ext = (hasp_ext_user_data_t*)obj->user_data.ext;
|
||||||
|
static const char* _swipejson = R"({"down":"page back","left":"page next","right":"page prev"})";
|
||||||
|
|
||||||
// extended tag exists, free old tag
|
// extended tag exists, free old tag if it's not the const _swipejson
|
||||||
if(ext && ext->swipe) {
|
if(ext) {
|
||||||
hasp_free(ext->swipe);
|
if(ext->swipe != _swipejson) hasp_free((void*)ext->swipe);
|
||||||
ext->swipe = NULL;
|
ext->swipe = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// new tag is blank
|
// new tag is blank
|
||||||
if(payload == NULL || payload[0] == '\0') {
|
if(payload == NULL || payload[0] == '\0') goto prune;
|
||||||
// my_prune_ext_tag(obj); // delete extended data if all extended properties are NULL
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// create new extended tags
|
// create new extended tags
|
||||||
if(!ext) {
|
if(!ext) {
|
||||||
ext = my_create_ext_tag(obj);
|
ext = my_create_ext_tag(obj);
|
||||||
|
if(!ext) goto error;
|
||||||
}
|
}
|
||||||
|
|
||||||
// create new action
|
if(Parser::is_true(payload)) {
|
||||||
{
|
ext->swipe = _swipejson; // backwards compatibility: use static action
|
||||||
|
return; // no error & no prune
|
||||||
|
} else {
|
||||||
|
|
||||||
|
// create new action
|
||||||
StaticJsonDocument<512> doc;
|
StaticJsonDocument<512> doc;
|
||||||
size_t len = payload ? strlen(payload) : 0;
|
size_t len = payload ? strlen(payload) : 0;
|
||||||
|
|
||||||
// check if it is a proper JSON object
|
// check if it is a proper JSON object
|
||||||
DeserializationError error = deserializeJson(doc, payload, len);
|
DeserializationError res = deserializeJson(doc, payload, len);
|
||||||
if(error != DeserializationError::Ok) doc.set(payload); // use tag as-is
|
if(res != DeserializationError::Ok) {
|
||||||
|
LOG_WARNING(TAG_ATTR, "Invalid parameter");
|
||||||
const size_t size = measureJson(doc) + 1;
|
ext->swipe = NULL;
|
||||||
if(char* str = (char*)hasp_malloc(size)) {
|
goto prune;
|
||||||
size_t len = serializeJson(doc, str, size); // tidy-up the json object
|
} else if(doc.isNull()) {
|
||||||
ext->swipe = str;
|
ext->swipe = NULL;
|
||||||
LOG_VERBOSE(TAG_ATTR, "new json: %s", str);
|
goto prune;
|
||||||
return;
|
} else if(doc.is<bool>() || doc.is<uint8_t>()) {
|
||||||
|
if(doc.as<bool>()) { // backwards compatibility: use static action
|
||||||
|
ext->swipe = _swipejson;
|
||||||
|
return; // no error & no prune
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const size_t size = measureJson(doc) + 1;
|
||||||
|
if(char* str = (char*)hasp_malloc(size)) {
|
||||||
|
size_t len = serializeJson(doc, str, size); // tidy-up the json object
|
||||||
|
ext->swipe = str;
|
||||||
|
LOG_VERBOSE(TAG_ATTR, "new json: %s", str);
|
||||||
|
return; // no error & no prune
|
||||||
|
} else {
|
||||||
|
ext->swipe = NULL;
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
goto prune;
|
||||||
|
|
||||||
// ext or str was NULL
|
error:
|
||||||
LOG_WARNING(TAG_ATTR, D_ERROR_OUT_OF_MEMORY);
|
LOG_WARNING(TAG_ATTR, D_ERROR_OUT_OF_MEMORY); // ext or str was NULL
|
||||||
|
|
||||||
|
prune:
|
||||||
|
// my_prune_ext_tag(obj); // delete extended data if all extended properties are NULL
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// the tag data is stored as SERIALIZED JSON data
|
// the tag data is stored as SERIALIZED JSON data
|
||||||
const char* my_obj_get_swipe(lv_obj_t* obj)
|
const char* my_obj_get_swipe(lv_obj_t* obj)
|
||||||
{
|
{
|
||||||
|
if(!obj) return NULL;
|
||||||
hasp_ext_user_data_t* ext = (hasp_ext_user_data_t*)obj->user_data.ext;
|
hasp_ext_user_data_t* ext = (hasp_ext_user_data_t*)obj->user_data.ext;
|
||||||
if(!ext) return NULL;
|
if(!ext) return NULL;
|
||||||
return ext->swipe;
|
return ext->swipe;
|
||||||
|
@ -390,7 +390,7 @@ void first_touch_event_handler(lv_obj_t* obj, lv_event_t event)
|
|||||||
|
|
||||||
void swipe_event_handler(lv_obj_t* obj, lv_event_t event)
|
void swipe_event_handler(lv_obj_t* obj, lv_event_t event)
|
||||||
{
|
{
|
||||||
if(event != LV_EVENT_GESTURE || !obj || !obj->user_data.ext) return;
|
if(event != LV_EVENT_GESTURE) return;
|
||||||
|
|
||||||
if(const char* swipe = my_obj_get_swipe(obj)) {
|
if(const char* swipe = my_obj_get_swipe(obj)) {
|
||||||
lv_gesture_dir_t dir = lv_indev_get_gesture_dir(lv_indev_get_act());
|
lv_gesture_dir_t dir = lv_indev_get_gesture_dir(lv_indev_get_act());
|
||||||
|
@ -17,8 +17,8 @@ const char FP_GROUPID[] PROGMEM = "groupid";
|
|||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
char* action;
|
char* action;
|
||||||
char* swipe;
|
|
||||||
char* tag;
|
char* tag;
|
||||||
|
const char* swipe;
|
||||||
} hasp_ext_user_data_t;
|
} hasp_ext_user_data_t;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
|
Loading…
x
Reference in New Issue
Block a user