Skip to content

Commit

Permalink
fix: "No nutrition facts" switch not working as intended (openfoodfac…
Browse files Browse the repository at this point in the history
…ts#4583)

* Nutritional facts editor: if the switch on "no nutrition data" is ON, the rest of the UI shouldn't be displayed + no other field sent to the API

* When we have nutrition data, the `no_nutrition_data` field sent to the API is null

* Force an "on" or "off" value to no nutrition data field

* Use constants where possible

* No nutrition data "off" value is actually ""
  • Loading branch information
g123k committed Mar 23, 2022
1 parent 8b50123 commit 0c26ad1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ class ProductEditNutritionFactsFragment : ProductEditFragment() {
}
}

binding.checkboxNoNutritionData.setOnCheckedChangeListener { _, isChecked ->
binding.nutritionFactsLayout.visibility = if (isChecked) View.GONE else View.VISIBLE
}

binding.btnAddANutrient.setOnClickListener { displayAddNutrientDialog() }

binding.salt.doAfterTextChanged { updateSodiumValue() }
Expand Down Expand Up @@ -328,7 +332,7 @@ class ProductEditNutritionFactsFragment : ProductEditFragment() {
loadNutritionImage(path)
}

if (productDetails[ApiFields.Keys.NO_NUTRITION_DATA] != null) {
if (productDetails[ApiFields.Keys.NO_NUTRITION_DATA]?.trim()?.lowercase() == ApiFields.Defaults.NO_NUTRITION_DATA_ON) {
binding.checkboxNoNutritionData.isChecked = true
binding.nutritionFactsLayout.visibility = View.GONE
}
Expand Down Expand Up @@ -550,11 +554,12 @@ class ProductEditNutritionFactsFragment : ProductEditFragment() {

// Add no nutrition data entry to map
if (binding.checkboxNoNutritionData.isChecked) {
targetMap[ApiFields.Keys.NO_NUTRITION_DATA] = "on"
} else {
targetMap += getNutrientsModeMap()
targetMap[ApiFields.Keys.NO_NUTRITION_DATA] = ApiFields.Defaults.NO_NUTRITION_DATA_ON
return targetMap
}

targetMap += getNutrientsModeMap()

// Add serving size entry to map if it has been changed
if (binding.servingSize.isNotEmpty()) {
@Suppress("USELESS_ELVIS") val servingSizeValue = binding.servingSize.getContent() +
Expand All @@ -570,6 +575,10 @@ class ProductEditNutritionFactsFragment : ProductEditFragment() {
targetMap += getNutrientMapIfUpdated(view)
}

if (targetMap.containsKey(ApiFields.Keys.NO_NUTRITION_DATA)) {
targetMap[ApiFields.Keys.NO_NUTRITION_DATA] = ApiFields.Defaults.NO_NUTRITION_DATA_OFF
}

return targetMap
}

Expand All @@ -580,7 +589,7 @@ class ProductEditNutritionFactsFragment : ProductEditFragment() {
if (activity !is ProductEditActivity) return emptyMap()

if (binding.checkboxNoNutritionData.isChecked) {
return mapOf(ApiFields.Keys.NO_NUTRITION_DATA to "on")
return mapOf(ApiFields.Keys.NO_NUTRITION_DATA to ApiFields.Defaults.NO_NUTRITION_DATA_ON)
}

val targetMap = mutableMapOf<String, String?>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ object ApiFields {
* Default values for some fields
*/
object Defaults {
const val NO_NUTRITION_DATA_ON = "on"
const val NO_NUTRITION_DATA_OFF = ""
const val NUTRITION_DATA_PER_100G = "100g"
const val NUTRITION_DATA_PER_SERVING = "serving"
const val DEBUG_BARCODE = "1"
Expand Down

0 comments on commit 0c26ad1

Please sign in to comment.