Passio Nutrition-AI
  • Nutrition-AI SDK Overview
  • Guides
    • Nutrition AI SDK
      • SDK Key and minimum requirements
      • Installation
      • Configure the SDK
      • Use Cases
        • Food recognition
        • Nutrition data
        • Barcode scanning
        • Nutrition Facts scanning
        • Search, Food Icons, RefCode
        • Speech recognition
        • Nutrition Advisor
        • Suggestions and Meal Plans
        • User created foods and reports
    • iOS SDK Docs
      • Before getting started
      • Getting the ml models to the device
      • Run the demos first
      • Adding Passio SDK into your project
      • Initialize and configure the SDK
      • Start/Stop food detection
      • Food Recognition Delegate
      • Migration from SDK 1.4.X to 2.x
      • SDK API
      • Quick Start Guide
        • Installation
        • Configure the SDK (UIKit)
        • Recognise food using image
        • Food Details
    • Android SDK Docs
      • Getting started
        • Include the library
        • Camera
        • SDK Initialization and Configuration
        • Food detection session
        • Visual, Barcode and Packaged Food detection
        • Nutritional Database
        • Sandbox app
      • Troubleshooting on Android
      • Migration from SDK version 1.4.x to 2.x
      • Quick Start Guide
        • Importing the Android SDK to a project
        • Configure SDK and handle the result
        • RecognizeImageRemote
        • Food Details
    • React Native SDK Docs
      • Getting Started
        • Installation
        • SDK Initialization and Configuration
      • API Reference
        • configure & requestCameraAuthorization
        • recognizeImageRemote
        • searchForFood & searchForFoodSemantic
        • startFoodDetection
          • FoodDetectionEvent
        • fetchFoodItemForProductCode
        • fetchFoodItemForRefCode
        • fetchFoodItemForDataInfo
        • startNutritionFactsDetection
        • fetchFoodItemForPassioID
        • recognizeSpeechRemote
        • Fetch Nutrients
        • onDowloadingPassioModelCallBacks
        • detectFoodFromImageURI
        • addToPersonalization
        • updateLanguage
        • fetchHiddenIngredients
        • fetchVisualAlternatives
        • fetchPossibleIngredients
      • Properties
        • PassioFoodItem
        • PassioFoodDataInfo
        • PassioFoodAmount
        • PassioIngredient
        • PassioNutrients
        • UnitMass
        • ServingUnit
        • ServingSize
        • PassioSearchResult
        • NutritionFacts
        • PassioNutritionPreview
        • PassioSpeechRecognitionModel
        • PassioLogAction
        • PassioAdvisorFoodInfo
        • FoodCandidates
        • FoodDetectionEvent
        • NutritionDetectionEvent
        • DetectedCandidate
        • BarcodeCandidate
        • PackagedFoodCode
        • AmountEstimate
        • ImagesInfo
        • PassioStatus
        • FoodDetectionEvent
        • NutritionFacts
        • PassioMealPlan
        • PassioMealPlanItem
      • Nutriton Advisor
        • initConversation
        • sendMessage
        • sendImage
        • Guide
          • useNutritionAdvisor
        • Properties
          • PassioAdvisorResponse
          • PassioAdvisorMessageResultStatus
          • PassioAdvisorResultStatus
      • Quick Start Guide
        • Installation
        • Configure the SDK
        • RecognizeImageRemote
        • Food Detail
      • Guide
        • Integrate SDK Setup
        • Integrate Quick Scan
        • Integrate Food Search
        • Integrate Food Editor with Recipe
        • Integrate MealPlan
        • Integrate Suggestions
        • Integrate recognizeImageRemote
      • Components
        • DetectionCameraView
        • PassioIconView
      • Changelog
      • More
        • Display Macro Chart
          • MockFood
          • Utils PassioNutrient
        • Display Micro Progress
        • Getting nutrition value for a FoodItem
        • Getting nutrition value for a Recipe
        • Recalculate the nutrition value based on serving size options.
      • Migrations
        • Migrating from SDK 2.X to version 3.X
        • Structure Migrations From SDK 3.X to SDK 2.X
        • [Deprecated] Migrating from SDK 1.X to version 2.X
        • [Deprecated] Getting Started (v1)
          • Installation
          • SDK Initialization and Configuration
          • Start food detection
          • FoodDetectionEvent
          • Nutritional Database
      • Troubleshooting on RN
      • V2
        • Getting Started
          • Installation
          • SDK Initialization and Configuration
          • Start food detection
          • FoodDetectionEvent
        • RN SDK API
          • Properties
            • PersonalizedAlternative
            • FoodSearchResult
            • PassioNutrient
            • FoodDetectionEvent
            • DownloadModelCallBack
            • UPCProduct
            • ServingUnit
            • PassioStatus
            • PassioIDAttributes
            • PassioIDEntityType
            • PassioFoodItem
            • PassioRecipe
            • ServingSize
            • Measurement
            • UnitMass
            • NutritionFacts
        • Food Image
        • Search Food Item
        • Quick Scan
        • Multi Scan
        • MealPlan
        • Recipe
      • How do I Integrate a Passio SDK in EXPO?
    • Flutter SDK Docs
      • Getting Started
    • Before You Continue
    • Setup For Android
    • Initialize and configure the SDK
  • Fundamentals
    • Nutrition-AI Developer FAQ
      • Nutrition Data
      • Supported Phones
      • Security
      • Testing Volume Estimation
      • Testing Nutrition-AI SDK
      • Nutrition-AI Test Methodology
    • Nutrition API - Mobile SDK interoperability
      • JSON Response parsing
  • Versions
    • 3.2.4
      • SDK Key and minimum requirements
      • Installation
      • Configure the SDK
      • Use Cases
        • Food recognition
        • Nutrition data
        • Barcode scanning
        • Nutrition Facts scanning
        • Search, Food Icons, RefCode
        • Speech recognition
        • Nutrition Advisor
        • Suggestions and Meal Plans
        • User created foods and reports
    • 3.2.2
      • SDK Key and minimum requirements
      • Installation
      • Configure the SDK
      • Use Cases
        • Food recognition
        • Nutrition data
        • Barcode scanning
        • Nutrition Facts scanning
        • Search, Food Icons, RefCode
        • Speech recognition
        • Nutrition Advisor
        • Suggestions and Meal Plans
        • User created foods and reports
    • 3.2.0
      • SDK Key and minimum requirements
      • Installation
      • Configure the SDK
      • Use Cases
        • Food recognition
        • Nutrition data
        • Barcode scanning
        • Nutrition Facts scanning
        • Search, Food Icons, RefCode
        • Speech recognition
        • Nutrition Advisor
        • Suggestions and Meal Plans
    • 3.1.4
      • SDK Key and minimum requirements
      • Installation
      • Configure the SDK
      • Use Cases
        • Food recognition
        • Nutrition data
        • Barcode scanning
        • Search, Food Icons, RefCode
        • Speech recognition
        • Nutrition Advisor
        • Suggestions and Meal Plans
Powered by GitBook
On this page
  • Fetching PassioFoodItem from PassioAdvisorFoodInfo
  • Showing the Food Detail and Macro-Nutrients
  • Edit Serving Size
  • Edit Serving Unit
Export as PDF
  1. Guides
  2. iOS SDK Docs
  3. Quick Start Guide

Food Details

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 PassioAdvisorFoodInfo
var foodItem: PassioFoodItem?

func fetchPassioFoodItem() {
    
    guard let food = food else { return }
    
    if let packagedFoodItem = food.packagedFoodItem {
        self.foodItem = packagedFoodItem
        tableView.reloadData()
    }
    else {
        if let foodDataInfo = food.foodDataInfo {
            let servingQuantity = foodDataInfo.nutritionPreview?.servingQuantity
            let servingUnit = foodDataInfo.nutritionPreview?.servingUnit
            
            updateLoader(show: true)
            PassioNutritionAI.shared.fetchFoodItemFor(foodDataInfo: foodDataInfo,
                                                      servingQuantity: servingQuantity,
                                                      servingUnit: servingUnit) { passioFoodItem in
                DispatchQueue.main.async {
                    self.updateLoader(show: false)
                    if let 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.

func setup(foodItem: PassioFoodItem) {

    // 1. Name & short name
    labelName.text = foodItem.name
    labelShortName.text = foodItem.details
    let entityType: PassioIDEntityType = foodItem.ingredients.count > 1 ? .recipe : .item
    
    if labelName.text?.lowercased() == self.labelShortName.text?.lowercased()
        || entityType == .recipe {
        labelShortName.isHidden = true
    } else {
        labelShortName.isHidden = false
    }
    
    // 2. Food image
    imageFood.loadIcon(id: foodItem.iconId)
    
    // 3. Calories
    let calories = foodItem.nutrientsSelectedSize().calories()?.value ?? 0
    let carbs = foodItem.nutrientsSelectedSize().carbs()?.value ?? 0
    let protein = foodItem.nutrientsSelectedSize().protein()?.value ?? 0
    let fat = foodItem.nutrientsSelectedSize().fat()?.value ?? 0

    caloriesLabel.text = calories.noDigit
    carbsLabel.text = carbs.oneDigit + " \(UnitsTexts.g)"
    protienLabel.text = protein.oneDigit + " \(UnitsTexts.g)"
    fatLabel.text = fat.oneDigit + " \(UnitsTexts.g)"
    
    // 4. Percentage
    let total = [c,p,f].reduce(0, { $0 + $1.percent })
    if total > 0 {
        fatPercentLabel.text = "(\(f.percent.oneDigit)%)"
        protienPercentLabel.text = "(\(p.percent.oneDigit)%)"
        carbsPercentLabel.text = "(\(c.percent.oneDigit)%)"
    } else {
        fatPercentLabel.text = "(0%)"
        protienPercentLabel.text = "(0%)"
        carbsPercentLabel.text = "(0%)"
    }
    
    // 5. Chart
    let percents = macronutrientPercentages(carbsG: carbs,
                                            fatG: fat,
                                            proteinG: protein,
                                            totalCalories: calories)
    let c = DonutChartView.ChartDatasource(color: .lightBlue,
                                         percent: percents.carbPercentage)
    let p = DonutChartView.ChartDatasource(color: .green500,
                                         percent: percents.proteinPercentage)
    let f = DonutChartView.ChartDatasource(color: .purple500,
                                         percent: percents.fatPercentage)
    nutritionView.updateData(data: [c,p,f])
}

func macronutrientPercentages(carbsG: Double,
                              fatG: Double,
                              proteinG: Double,
                              totalCalories: Double) -> (carbPercentage: Double,
                                                         fatPercentage: Double,
                                                         proteinPercentage: Double)
{
    // Calculate calories contributed by each macronutrient
    let carbCalories = carbsG * 4
    let fatCalories = fatG * 9
    let proteinCalories = proteinG * 4
    
    // Calculate total calories from macronutrients
    let totalMacronutrientCalories = carbCalories + fatCalories + proteinCalories
    
    // Calculate percentages
    let carbPercentage = (carbCalories / totalMacronutrientCalories) * 100
    let fatPercentage = (fatCalories / totalMacronutrientCalories) * 100
    let proteinPercentage = (proteinCalories / totalMacronutrientCalories) * 100
    return (carbPercentage: carbPercentage,
            fatPercentage: fatPercentage,
            proteinPercentage: proteinPercentage)
}

Edit Serving Size

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 data
tableView.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 unit
self.foodItem?.setSelectedUnit(unit)
// Refresh data
tableView.reloadData()

To get list of all units for a food item:

guard let servingUnits = foodItem?.amount.servingUnits else { return }
let items = servingUnits.map { $0.unitName }
PreviousRecognise food using imageNextAndroid SDK Docs

Last updated 6 months ago