Skip to content

Commit

Permalink
Merge pull request #817 from EmilJunker/select-download-fields
Browse files Browse the repository at this point in the history
Ask user which fields to replace in re-download
  • Loading branch information
davidhealey authored May 2, 2024
2 parents a3c4980 + 3e39e9e commit 268539b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 17 deletions.
105 changes: 88 additions & 17 deletions www/activities/foods-meals-recipes/js/food-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -654,20 +654,71 @@ app.FoodEditor = {
let title = app.strings.dialogs["download-title"] || "Retrieve latest information";
let text = app.strings.dialogs["download-text"] || "Your local values will be replaced by the latest information available for this item";

let div = document.createElement("div");
div.className = "dialog-text";

let p = document.createElement("p");
p.innerText = text;
div.appendChild(p);

// Create dialog inputs
let inputs = document.createElement("form");
inputs.className = "list no-hairlines no-hairlines-between no-margin";
div.appendChild(inputs);

let ul = document.createElement("ul");
inputs.appendChild(ul);

["image", "name-and-brand", "nutriments-per-serving", "nutriments-per-100"].forEach((field) => {
let li = document.createElement("li");
ul.appendChild(li);

let label = document.createElement("label");
label.className = "item-checkbox item-content";
li.appendChild(label);

let input = document.createElement("input");
input.type = "checkbox";
input.id = field;
if (field === "image" && app.FoodEditor.item.barcode.startsWith("fdcId_")) {
// USDA database has no images
label.classList.add("disabled");
input.setAttribute("disabled", "");
} else if (field !== "nutriments-per-100") {
// All fields are selected by default, except nutriments-per-100
input.setAttribute("checked", "");
}
label.appendChild(input);

let i = document.createElement("i");
i.className = "icon icon-checkbox";
label.appendChild(i);

let div = document.createElement("div");
div.className = "item-inner";
div.innerText = app.strings["food-editor"][field] || field;
label.appendChild(div);
});

let dialog = app.f7.dialog.create({
title: title,
content: app.Utils.getDialogTextDiv(text),
verticalButtons: true,
content: div.outerHTML,
on: {
opened: () => {
["nutriments-per-serving", "nutriments-per-100"].forEach((field) => {
// At most one nutriments field can be selected at a time
let otherNutrimentFieldId = field === "nutriments-per-serving" ? "nutriments-per-100" : "nutriments-per-serving";
document.getElementById(field).addEventListener("change", (e) => {
if (e.target.checked) document.getElementById(otherNutrimentFieldId).checked = false;
});
});
}
},
buttons: [{
text: app.strings["food-editor"]["per-serving"] || "Per Serving",
onClick: async () => {
app.FoodEditor.downloadItemInfo(false);
}
},
{
text: app.strings["food-editor"]["per-100"] || "Per 100g/100ml",
onClick: async () => {
app.FoodEditor.downloadItemInfo(true);
text: app.strings.dialogs.ok || "OK",
onClick: async (dialog) => {
let valuesToGet = Array.from(dialog.el.getElementsByTagName("input")).map((input) => input.checked);
app.FoodEditor.downloadItemInfo(valuesToGet);
}
},
{
Expand All @@ -678,7 +729,10 @@ app.FoodEditor = {
}).open();
},

downloadItemInfo: async function(preferDataPer100g) {
downloadItemInfo: async function(valuesToGet) {
let [getImage, getNameAndBrand, getNutrimentsServing, getNutriments100] = valuesToGet;
let getNutriments = getNutrimentsServing || getNutriments100;

if (app.Utils.isInternetConnected()) {
let barcode = app.FoodEditor.item.barcode;
let result;
Expand All @@ -687,20 +741,31 @@ app.FoodEditor = {

if (barcode !== undefined) {
if (barcode.startsWith("fdcId_"))
result = await app.USDA.search(barcode.replace("fdcId_", ""), preferDataPer100g);
result = await app.USDA.search(barcode.replace("fdcId_", ""), getNutriments100);
else
result = await app.OpenFoodFacts.search(barcode, preferDataPer100g);
result = await app.OpenFoodFacts.search(barcode, getNutriments100);
}

app.f7.preloader.hide();

if (result !== undefined && result.length > 0) {
item = result[0];
item.notes = app.FoodEditor.el.notes.value; // Keep local notes, do not overwrite
item.notes = app.FoodEditor.el.notes.value; // Always keep local notes, do not overwrite
if (!getNameAndBrand) {
item.name = app.FoodEditor.el.name.value; // Keep local name
item.brand = app.FoodEditor.el.brand.value; // Keep local brand
}
if (!getNutriments) {
item.portion = app.FoodEditor.el.portion.value; // Keep local serving size
item.unit = app.FoodEditor.el.unit.value; // Keep local unit
}
app.FoodEditor.populateFields(item);
if (!barcode.startsWith("fdcId_"))
if (getImage) {
app.FoodEditor.populateMainImage(item);
app.FoodEditor.renderNutritionFields(item);
}
if (getNutriments) {
app.FoodEditor.renderNutritionFields(item);
}
} else {
let msg = app.strings.dialogs["no-results"] || "No matching results";
app.Utils.toast(msg);
Expand Down Expand Up @@ -776,4 +841,10 @@ document.addEventListener("page:init", function(event) {
app.data.context = undefined;
app.FoodEditor.init(context);
}
});

document.addEventListener("page:beforeout", function(event) {
if (event.target.matches(".page[data-name='food-editor']")) {
app.TTS.stop();
}
});
4 changes: 4 additions & 0 deletions www/assets/locales/locale-en.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@
"nutrition": "Nutrition",
"per-100": "Per 100g/100ml",
"per-serving": "Per Serving",
"image": "Image",
"name-and-brand": "Name and Brand",
"nutriments-per-serving": "Nutrients per serving",
"nutriments-per-100": "Nutrients per 100g/100ml",
"additional-photos": "Additional Photos",
"ingredients": "Ingredients list",
"packaging": "Recycling instructions / packaging information",
Expand Down

0 comments on commit 268539b

Please sign in to comment.