Skip to content

Commit f55f3cf

Browse files
Commit-La-Grenouillere-schneider
authored andcommitted
#3 | APP: extending updateImage() and Utils.loadImage() to accomodate the showing data from response option
1 parent 8a50d16 commit f55f3cf

File tree

1 file changed

+53
-15
lines changed

1 file changed

+53
-15
lines changed

Sources/app.js

+53-15
Original file line numberDiff line numberDiff line change
@@ -172,39 +172,77 @@ function APIRequest(jsonObj) {
172172
}
173173

174174
async function updateImage(resp, do_status_poll) {
175-
if (!settings.advanced_settings || !settings.response_parse || !settings.image_matched || !settings.image_unmatched)
175+
/*
176+
* Making sure we run only in one of the 2 relevant cases:
177+
* (1) when asked to parse and match to define the background image
178+
* (2) when asked to parse and display the data from the response on the key
179+
*/
180+
181+
// Common / top-level options
182+
if (!settings.advanced_settings || (!settings.response_parse && !settings.response_data))
183+
return;
184+
185+
// Case 1 missing config detection
186+
if (settings.response_parse && (!settings.image_matched || !settings.image_unmatched))
187+
return;
188+
189+
// Case 2 missing config detection (could be commented if we decide that the background image is optional)
190+
if (settings.response_data && !settings.background_image)
176191
return;
177192

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

181198
const prefix = (do_status_poll && settings.poll_status && settings.poll_status_parse) ? 'poll_status' : 'response';
182-
const field = Utils.getProp(settings, `${prefix}_parse_field`, undefined);
199+
const field = Utils.getProp(settings, `${prefix}_${field_name}_field`, undefined);
183200
const value = Utils.getProp(settings, `${prefix}_parse_value`, undefined);
184-
185-
if (field !== undefined && value !== undefined) {
186-
json = await resp.json();
187-
new_key_state = (Utils.getProperty(json, field) == value);
188-
} else if (field !== undefined) {
189-
json = await resp.json();
190-
new_key_state = !(['false', '0', '', 'undefined'].indexOf(String(Utils.getProperty(json, field)).toLowerCase().trim()) + 1);
191-
} else if (value !== undefined) {
192-
body = await resp.text();
193-
new_key_state = body.includes(value);
201+
// The value will always be undef in Case 2...
202+
203+
if (want_data) {
204+
if (field !== undefined) {
205+
json = await resp.json();
206+
new_key_state = Utils.getProperty(json, field);
207+
} else {
208+
new_key_state = '?????';
209+
}
210+
} else {
211+
if (field !== undefined && value !== undefined) {
212+
json = await resp.json();
213+
new_key_state = (Utils.getProperty(json, field) == value);
214+
} else if (field !== undefined) {
215+
json = await resp.json();
216+
new_key_state = !(['false', '0', '', 'undefined'].indexOf(String(Utils.getProperty(json, field)).toLowerCase().trim()) + 1);
217+
} else if (value !== undefined) {
218+
body = await resp.text();
219+
new_key_state = body.includes(value);
220+
}
194221
}
195222

196223
if (new_key_state == key_state) return;
197224

198225
key_state = new_key_state;
199226

200-
path = key_state
201-
? settings.image_matched
202-
: settings.image_unmatched;
227+
// adapting the background image to the Case we are working for
228+
if (want_data) {
229+
path = settings.background_image;
230+
} else {
231+
path = key_state
232+
? settings.image_matched
233+
: settings.image_unmatched;
234+
}
203235

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

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

240+
// Defining the text that must be rendered over the image
241+
if (want_data) {
242+
var name = (settings.response_data_name) ? `${settings.response_data_name}\n\n` : '';
243+
$SD.api.setTitle(context, `${name}${new_key_state} tickets`, null);
244+
}
245+
208246
return resp;
209247
}
210248

0 commit comments

Comments
 (0)