Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

fix: Use standard units for nutrients #764

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ enum NutritiveUnits {
case alcohol
case none

func unitsValues() -> [String] {
public var unitsValues: [String] {
switch self {
case .kcal:
return [
Expand Down Expand Up @@ -105,7 +105,7 @@ class EditNutritiveValueView: UIView {
singleUnitLabel.isHidden = true
unitButton.isHidden = true

let values = displayedUnit.unitsValues()
let values = displayedUnit.unitsValues
selectedUnit = selectedValue ?? values.first

if values.count == 1 {
Expand Down Expand Up @@ -137,7 +137,7 @@ class EditNutritiveValueView: UIView {
}

@IBAction func unitButtonTapped(_ sender: Any) {
let values = displayedUnit.unitsValues()
let values = displayedUnit.unitsValues

if values.count < 2 {
// button should not be visible anyway ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,20 @@ class ProductAddViewController: TakePictureViewController {
}
}

private func unit(for nutriment: String) -> NutritiveUnits {
switch nutriment {
case OFFJson.EnergyKey:
return .kJoule
case OFFJson.EnergyKcalKey:
return .kcal
case OFFJson.AlcoholKey:
return .alcohol
case OFFJson.PhKey:
return .none
default: return .units
}
}

private func configureLanguageField() {
let languages = dataManager.getLanguages()

Expand Down Expand Up @@ -723,7 +737,10 @@ class ProductAddViewController: TakePictureViewController {
} else {
view.inputTextField.text = nil
}
view.selectedUnit = nutriment.unit
// do not use the nutriment values as provided by the json,
// these are the original input values
// Use the standard values coresponding to the Nutriment.
view.selectedUnit = unit(for: nutriment.nameKey).unitsValues.first
}
}
}
Expand Down