Fetching PassioFoodItem from PassioAdvisorFoodInfo
To display the food details, PassioFoodItem is required.
Fetch PassioFoodItem from PassioFoodDataInfo
// We already have PassioAdvisorFoodInfo var food: PassioAdvisorFoodInfo?// We will fetch PassioFoodItem from PassioAdvisorFoodInfovar foodItem: PassioFoodItem?funcfetchPassioFoodItem() {guardlet food = food else { return }iflet packagedFoodItem = food.packagedFoodItem { self.foodItem = packagedFoodItem tableView.reloadData() }else {iflet foodDataInfo = food.foodDataInfo {let servingQuantity = foodDataInfo.nutritionPreview?.servingQuantitylet servingUnit = foodDataInfo.nutritionPreview?.servingUnitupdateLoader(show:true) PassioNutritionAI.shared.fetchFoodItemFor(foodDataInfo: foodDataInfo, servingQuantity: servingQuantity, servingUnit: servingUnit) { passioFoodItem in DispatchQueue.main.async { self.updateLoader(show:false)iflet foodItem = passioFoodItem { self.foodItem = foodItem tableView.reloadData() } } } } }}
Showing the Food Detail and Macro-Nutrients
We build a UI component to display macro-nutrients like calories, carbohydrates, protein, and fat. We get these macro nutrients for the selected size of the food.
The setSelectedQuantity() function handles changes to the serving quantity. It calculates the corresponding weight based on the selected serving unit and updates the passioFoodItem object accordingly.
For example, once user changes the quantity from a textfield or from a slider, we can call setSelectedQuantity() function on passioFoodItem.
let quantity =Double(textField.text)self.foodItem?.setSelectedQuantity(quantity)// Refresh datatableView.reloadData()
Edit Serving Unit
The setSelectedUnit() function handles changes to the serving unit.
For example, once user changes the unit (e.g. Packet, Cup, Gram) from a list of units, we can call setSelectedQuantity() function on passioFoodItem.
let unit =// User selected unitself.foodItem?.setSelectedUnit(unit)// Refresh datatableView.reloadData()