Integrate SDK Setup

The API below is used for integration setup

/**
   * Configure the SDK with the given options.
   *
   * @param options - The configuration options
   * @returns A `Promise` resolving with a `PassioStatus` indicating the current state of the SDK.
   */
  configure(options: ConfigurationOptions): Promise<PassioStatus>

  /**
   * Prompt the user for camera authorization if not already granted.
   * @remarks Your app's Info.plist must inclue an `NSCameraUsageDescription` value or this method will crash.
   * @returns A `Promise` resolving to `true` if authorization has been granted or `false` if not.
   */
  requestCameraAuthorization(): Promise<boolean>

/**
   * This method indicating downloading file status if model download from passio server.
   * @param callback - A callback to receive for downloading file lefts from queue.
   * @param callback - A callback to receive download file failed for some reason.
   * @returns A `Callback` that should be retained by the caller while downloading is running. Call `remove` on the callback to terminate listeners and relase from memory.
   */
  onDownloadingPassioModelCallBacks: (
    downloadModelCallBack: DownloadModelCallBack
  ) => Callback

Example

usePassioSDK

import { useEffect, useState } from 'react'

import {
  CompletedDownloadingFile,
  DownloadingError,
  PassioSDK,
} from '@passiolife/nutritionai-react-native-sdk-v3'

export type SDKStatus = 'init' | 'downloading' | 'error' | 'ready'

export const usePassioSDK = ({
  key,
  debugMode = false,
  autoUpdate = false,
}: {
  key: string
  debugMode?: boolean
  autoUpdate?: boolean
}) => {
  const [loadingState, setLoadingState] = useState<SDKStatus>('init')
  const [leftFile, setDownloadingLeft] = useState<number | null>(null)

  useEffect(() => {
    async function configure() {
      try {
        const status = await PassioSDK.configure({
          key: key,
          debugMode: debugMode,
          autoUpdate: autoUpdate,
        })
        switch (status.mode) {
          case 'notReady':
            return
          case 'isReadyForDetection':
            setLoadingState('ready')
            return
          case 'error':
            console.error(`PassioSDK Error ${status.errorMessage}`)
            setLoadingState('error')
            return
        }
      } catch (err) {
        console.error(`PassioSDK Error ${err}`)
        setLoadingState('error')
      }
    }
    configure()
  }, [key, debugMode, autoUpdate])

  useEffect(() => {
    const callBacks = PassioSDK.onDownloadingPassioModelCallBacks({
      completedDownloadingFile: ({ filesLeft }: CompletedDownloadingFile) => {
        setDownloadingLeft(filesLeft)
      },
      downloadingError: ({ message }: DownloadingError) => {
        console.log('DownloadingError ===>', message)
      },
    })
    return () => callBacks.remove()
  }, [])

  return {
    loadingState,
    leftFile,
  }
}

export const useCameraAuthorization = () => {
  const [authorized, setAuthorized] = useState(false)

  useEffect(() => {
    async function getAuth() {
      const isAuthorized = await PassioSDK.requestCameraAuthorization()
      setAuthorized(isAuthorized)
    }
    getAuth()
  }, [])

  return authorized
}

Last updated