Skip to content

Commit

Permalink
#3 | APP: extending updateImage() and Utils.loadImage() to accomodate…
Browse files Browse the repository at this point in the history
… the showing data from response option
  • Loading branch information
Commit-La-Grenouille authored and re-schneider committed Apr 24, 2022
1 parent 8a50d16 commit f55f3cf
Showing 1 changed file with 53 additions and 15 deletions.
68 changes: 53 additions & 15 deletions Sources/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,39 +172,77 @@ function APIRequest(jsonObj) {
}

async function updateImage(resp, do_status_poll) {
if (!settings.advanced_settings || !settings.response_parse || !settings.image_matched || !settings.image_unmatched)
/*
* Making sure we run only in one of the 2 relevant cases:
* (1) when asked to parse and match to define the background image
* (2) when asked to parse and display the data from the response on the key
*/

// Common / top-level options
if (!settings.advanced_settings || (!settings.response_parse && !settings.response_data))
return;

// Case 1 missing config detection
if (settings.response_parse && (!settings.image_matched || !settings.image_unmatched))
return;

// Case 2 missing config detection (could be commented if we decide that the background image is optional)
if (settings.response_data && !settings.background_image)
return;

let json, body;
var new_key_state = key_state;
const want_data = (settings.response_data) ? true : false;
const field_name = (want_data) ? 'data' : 'parse';

const prefix = (do_status_poll && settings.poll_status && settings.poll_status_parse) ? 'poll_status' : 'response';
const field = Utils.getProp(settings, `${prefix}_parse_field`, undefined);
const field = Utils.getProp(settings, `${prefix}_${field_name}_field`, undefined);
const value = Utils.getProp(settings, `${prefix}_parse_value`, undefined);

if (field !== undefined && value !== undefined) {
json = await resp.json();
new_key_state = (Utils.getProperty(json, field) == value);
} else if (field !== undefined) {
json = await resp.json();
new_key_state = !(['false', '0', '', 'undefined'].indexOf(String(Utils.getProperty(json, field)).toLowerCase().trim()) + 1);
} else if (value !== undefined) {
body = await resp.text();
new_key_state = body.includes(value);
// The value will always be undef in Case 2...

if (want_data) {
if (field !== undefined) {
json = await resp.json();
new_key_state = Utils.getProperty(json, field);
} else {
new_key_state = '?????';
}
} else {
if (field !== undefined && value !== undefined) {
json = await resp.json();
new_key_state = (Utils.getProperty(json, field) == value);
} else if (field !== undefined) {
json = await resp.json();
new_key_state = !(['false', '0', '', 'undefined'].indexOf(String(Utils.getProperty(json, field)).toLowerCase().trim()) + 1);
} else if (value !== undefined) {
body = await resp.text();
new_key_state = body.includes(value);
}
}

if (new_key_state == key_state) return;

key_state = new_key_state;

path = key_state
? settings.image_matched
: settings.image_unmatched;
// adapting the background image to the Case we are working for
if (want_data) {
path = settings.background_image;
} else {
path = key_state
? settings.image_matched
: settings.image_unmatched;
}

log('updateImage(): FILE:', path, 'JSON:', json, 'BODY:', body);

Utils.loadImage(path, img => $SD.api.setImage(context, img));

// Defining the text that must be rendered over the image
if (want_data) {
var name = (settings.response_data_name) ? `${settings.response_data_name}\n\n` : '';
$SD.api.setTitle(context, `${name}${new_key_state} tickets`, null);
}

return resp;
}

Expand Down

0 comments on commit f55f3cf

Please sign in to comment.